首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Pokemon游戏While循环

Python Pokemon游戏While循环
EN

Stack Overflow用户
提问于 2017-02-12 02:12:37
回答 1查看 642关注 0票数 2

好的,我正在编写一个精灵宝可梦文字冒险类游戏,我需要帮助处理while循环。我已经创建了while循环。但不起作用的部分是:你可以选择两个raw_inputs,奔跑或战斗。当您选择其中任何一个时,它都不会显示消息。它所做的就是重复我编程要问的问题。

问题是:“你是想跑还是想和伊夫塔尔人作战?”您可以在iPython会话中键入"Run“或"Battle”。当你输入Battle时,它应该说“你向伊夫塔尔人发起了一场战斗!”当你输入Run时,它应该说“你不能跑你这个胆小鬼”,但如果你输入任何东西,它所做的就是问同样的问题:“你想跑还是和伊夫塔尔战斗?”

我需要的帮助是离开while循环,当你输入run或battle时,它将显示该命令的消息。这是代码,我需要任何人的帮助,谢谢!

代码语言:javascript
复制
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!")
EN

回答 1

Stack Overflow用户

发布于 2017-02-12 02:27:22

您的while循环充满了错误或错误。试试这个:

使用.lower()函数进行while user_input.lower() != "battle" or user_input.lower() != "run":,这样您就不必为"RuN“或"baTTle”做计划了。它将字符串转换为小写,以便您可以只检查单词。另外,你应该使用input()而不是raw_input()。老实说,我会这样组织你的代码:

代码语言:javascript
复制
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()

您可能会更幸运地使用这样的代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42179436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档