Python-如何在继续代码之前重新提示不同的输入

标签 python if-statement input

我正在制作一个回合制文本战斗系统,并且我正在尝试为某些能力添加“冷却时间”。

我已经这样做了,但即使技能处于冷却状态并且用户按下咒语按钮,回合仍然会被消耗并且咒语不起作用。

我可以将异常(exception)值错误添加到不可用选项的内容中,但我很难将其添加到有时可以作为选项的内容中。

ab1 = 1 #This is tracker for cool down (if its 0 the spell can be used)
if(ab1 == 0):
   print("1)",ability1)
else:
    print("1)",ability1, "- This ability is not ready")
useSpell = input("")
if(useSpell == "1") and (ab1 == 0):
    #Do Spell Stuff
else:
    #This is where I believe I need to block the code from continuing

3个咒语,所以打印时看起来像这样:

选择一种能力。

1) 法术名称 - 能力尚未准备好(假设该法术处于冷却状态)

2) 法术名称

3) 法术名称

如果用户选择的拼写不可用,如何停止代码继续运行并重新提示用户选择另一个数字。 (我已经阻止了该咒语的工作,只是没有轮到新的输入)

最佳答案

如何停止代码继续运行并重新提示用户 如果他们选择的咒语不可用,请选择另一个?

如果用户选择了不可用的咒语,只需使用 while循环重新提示用户输入另一个咒语:

ab1 = 1 #This is tracker for cool down (if its 0 the spell can be used)
if(ab1 == 0):
   print("1)",ability1)
else:
    print("1)",ability1, "- This ability is not ready")

while True:           
    if(input("") == "1") and (ab1 == 0):
        #Do Spell Stuff
        break;    #Add a break statement when an available spell is inputted
#   else:
#       ab1=0

请记住,您必须在循环内观察 ab1 的值(将其从 1 更改为 0,否则最终会出现无限 while 循环)。

可以通过多种不同的方式在代码中实现 while 循环,但大多数人使用循环来重新提示用户输入。

关于Python-如何在继续代码之前重新提示不同的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29725509/

相关文章:

java - 我正在尝试比较字符串,但它无法识别

php - 基于 session 变量 php 运行 Correct Array 函数

if-statement - COBOL 程序中的语法错误,如果发生意外

python - 使用 python-evdev 将事件发送到 uinput 关于掩码键的键绑定(bind)问题

javascript - JQuery 中的明文字段值

python - 在 python 中以格式化字符串列出打印

python - 使用 Python 获取鼠标增量! (在 Linux 中)

python - 我想使用 numpy 分析一些数据,但是当我执行代码时不断弹出错误

python - tf.app.run() 是如何工作的?

c# - 使用 Unity 的新输入系统检测鼠标滚轮滚动输入