好的,我正在编写一款 Pokemon 文字冒险类游戏,我需要有关 while 循环的帮助。我已经创建了 while 循环。但是不起作用的部分是:您可以选择两个 raw_inputs,run 或 battle。当您选择其中任何一个时,它不会显示消息。它所做的只是重复我为其编程的问题。
问题是“你想逃跑还是与伊维尔塔尔战斗?”您可以在 iPython session 中输入“Run”或“Battle”。当您键入 Battle 时,它应该会说“您向 Yveltal 发起了一场战斗挑战!”当你输入 Run 时,它应该说“你这个胆小鬼不能跑”,但如果你输入任何东西,它所做的只是问同样的问题“你想跑还是打伊维尔塔尔?”
我需要帮助的是离开 while 循环,当您键入 run 或 battle 时,它会显示该命令的消息。这是代码,我可以使用任何人的帮助,谢谢!
from time import sleep
def start():
sleep(2)
print "Hello there, what is your name?"
name = raw_input ()
print "Oh.. So your name is %s!" % (name)
sleep(3)
print"\nWatch out %s a wild Yveltal appeared!" % (name)
sleep(4)
user_input = raw_input("Do you want to Run or Battle the Yveltal?" "\n")
while raw_input() != 'Battle' or user_input == 'battle' != 'Run' or user_input == 'run':
print("Do you want to Run or Battle the Yveltal? ")
if user_input == 'Battle' or user_input == 'battle':
print("You challenged Yveltal to a battle!")
elif user_input == 'Run' or user_input == 'run':
print("You can't run you coward!")
请您参考如下方法:
您的 while 循环充满了错误或失误。试试这个:
while user_input.lower() != "battle"或 user_input.lower() != "run": 使用 .lower() 函数可以让您不必为“RuN”或“baTTle”做计划。它将字符串转换为小写,以便您可以只检查单词。此外,您应该使用 input() 而不是 raw_input() 老实说,我会像这样构建您的代码:
user_input = input("Run or battle?\n") #or whatever you want your question
user_input = user_input.lower()
while True:
if user_input == "battle":
print("You challenged Yveltal to a battle!")
break
elif user_input == "run":
print("You can't run you coward!")
user_input = input("Run or battle?\n")
user_input = user_input.lower()
break
else:
user_input = input("Run or battle?\n")
user_input = user_input.lower()
使用这样的代码您可能会更幸运。
