2017-02-11 25 views
1

好吧,我编码的口袋妖怪文本冒险类游戏,我需要while循环的帮助。我已经完成了while循环部分。但不工作的部分是:你可以在选择两个raw_inputs run,battle之间进行选择。当您按下其中任何一个时,它不会显示消息。它所做的就是重复我编写它的问题。问题问:“你想跑或战Yveltal?”。您可以在ipython会话中输入“运行”或“对战”。当你键入战斗时,它应该说“你挑战Yveltal参加一场战斗!”。当你键入run时,它应该说“你不能跑你胆小鬼”,但如果你输入任何东西,它所要做的就是问同样的问题“你想跑步还是战斗Yveltal?”。我想要帮助的是离开while循环,当你输入run或battle时,它会显示该命令的消息。这是代码,我可以使用任何人的帮助,谢谢!Python口袋妖怪游戏虽然循环

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!") 
+0

那么,这是什么问题? – Arman

回答

0

while循环为百病之错误或失误。试试这个:

while user_input.lower() != "battle" or user_input.lower() != "run": 使用.lower()函数可以让你不必计划“RuN”或“baTTle”。它将字符串转换为小写,以便您可以检查单词。此外,你应该使用的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() 

你可能有这样的代码更好的运气。

+0

我很抱歉请求另一个帮忙,但我希望它再次询问“跑步或战斗”消息,如果你输入跑步,如果你键入“跑步”它会说“你不能跑你胆小鬼”,然后显示每次进入跑步时都会出现“跑步或战斗”字符串,但这只会在您只输入“跑步”的情况下发生,但其余的确很有帮助!此外,我使用raw_input,因为您不必输入'run',只需键入run即可。如果你没有回答“跑步”或“战斗”这个问题,它也不会重新提出这个问题,它只是说它没有被定义,而不是询问等待有效回答的相同问题。 – Adal

+0

input()返回一个字符串。用户可以定期输入单词。另外,你是否尝试过这段代码?它适用于我,我理解你想要它。 – SH7890

+0

在下面的回答中,else语句的缩进是错误的。这就是为什么它不能正确执行。缩进一次,你应该很好。 – SH7890