我正在为一个类项目设计一个RPG,对一个项目的花哨想法进行工作,该项目可以配置为给现有类提供某些奖励。
但出于某种原因,让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
答案 0 :(得分:2)
你的问题是缩进:
def askPowerTread():
choice = None
while choice not in ['A','B','C']:
print("Invalid choice.")
choice = input(...)
这里你循环遍历print语句,但没有要求做出新的选择,这是在while块之外。
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)
答案 1 :(得分:0)
#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""")
您将选择分配给none
(我认为您的意思是None
),然后输入循环而不更改它。
choice = input(...)
在while循环之外,因此永远不会执行。尝试:
def askPowerTread():
while True:
choice = input(...)
if choice in [...]:
break
else:
print('{choice} is an invalid choice'.format(choice=choice))
要展示none
和None
之间的差异:
>>> type(none)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
type(none)
NameError: name 'none' is not defined
>>> type(None)
<class 'NoneType'>
>>>