Skip to main content
 首页 » 编程设计

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

2024年10月25日10oomusou

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

我已经这样做了,但即使技能正在冷却并且用户按下了该法术的按钮,该回合仍然会被消耗并且该法术不起作用。

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

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 循环,但大多数人使用循环来重新提示用户输入。