我正在为一个类(class)项目设计一个角色扮演游戏,正在研究一个可以配置为为现有类(class)提供某些奖励的项目的噱头想法。
但是由于某种原因,让 While
循环正确循环并且不重复 print("invalid choice")
亿万年并且让函数本身正常工作,是超越我。
感谢任何帮助!!
#PowerTreadFunction
def askPowerTread():
choice = none
while choice not in ['A','B','C']:
print("Invalid choice.")
choice = input("""Your Power Treads offer the following configurations which each give distinct bonuses, choose now:\n
a) DAMAGE\nb) DEFENSE\nc) ARMOR CLASS\n""")
if choice == 'A':
printSlow("Configuring Power Treads for Damage, you now recieve a +5 damage bonus")
player[DAM] += 5
elif choice == 'B':
printSlow("Configuring Power Treads for Defense, you now recieve a +5 defense bonus")
player[DEF] +=5
elif choice == 'C':
printSlow("Configuring Power Treads for Armor Class, you now recieve a +5 armor class bonus")
player[AC] +=5
最佳答案
你的问题是缩进:
def askPowerTread():
choice = None
while choice not in ['A','B','C']:
print("Invalid choice.")
choice = input(...)
这里您正在循环打印语句,但不要求做出新的选择,这是在 while block 之外的。
def askPowerTread():
choice = None
while choice not in ['A','B','C']:
print("Invalid choice.")
choice = input(...)
应该可以解决你的问题。解决后,您粘贴的代码对我来说看起来很好。
编辑:@IanAuld,你是对的,解决这个问题:
PROMPT="""\
Your Power Treads offer the following configurations which each
give distinct bonuses, choose now:
a) DAMAGE
b) DEFENSE
c) ARMOR CLASS
“”“
def askPowerTread():
choice = input(PROMPT)
while choice not in ['A','B','C']:
print("Invalid choice.")
choice = input(PROMPT)
关于python - 寻求在 Python 中设计 While 循环的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23596704/