首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Python中执行while循环问题

在Python中执行while循环问题
EN

Stack Overflow用户
提问于 2019-02-12 21:55:22
回答 4查看 199关注 0票数 1

我希望用户输入"right“或"left”,根据输入,将发生一些操作。

如果用户在前两次输入"right“,笑脸就会变得悲伤,不能走出森林。如果用户多次输入“正确”,笑脸总是会变得沮丧,砍掉一些树,做一张桌子,然后翻出来,因为它不能走出森林。

只要用户输入“左”,笑脸就会离开森林。

以下是我的Python代码:

代码语言:javascript
复制
n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
i = 1
while n == "right" or n == "Right" and i < 3:
    n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
    i = i + 1
while n == "right" or n == "Right" and i >= 3:
    n = input("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
print("\nYou got out of the Lost Forest!\n\o/")

问题是,即使用户输入第三次、第四次或第五次“正确”,“翻转”动作也不会发生。笑脸只会变得悲伤,它不会从第一个循环中走出来。

我在这里做错了什么?

EN

回答 4

Stack Overflow用户

发布于 2019-02-12 21:59:31

while语句中缺少方括号。以下内容应该会给你想要的东西:

代码语言:javascript
复制
n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
i = 1
while (n == "right" or n == "Right") and i < 3:
    n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
    i = i + 1
while (n == "right" or n == "Right") and i >= 3:
    n = input("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
print("\nYou got out of the Lost Forest!\n\o/")
票数 2
EN

Stack Overflow用户

发布于 2019-02-12 22:00:17

您的if条件没有按预期工作,因为该条件是按顺序计算的。因此,如果n=="right"为真,则i的值无关紧要。相反,您应该将其更改为:

代码语言:javascript
复制
n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
i = 1
while (n == "right" or n == "Right") and i < 3:
    n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
    i = i + 1
while (n == "right" or n == "Right") and i >= 3:
    n = input("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
print("\nYou got out of the Lost Forest!\n\o/")
票数 2
EN

Stack Overflow用户

发布于 2019-02-12 22:01:33

您不需要多个while循环,只需要在循环中使用一个简单的if-elif

代码语言:javascript
复制
n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
i = 1
while (n.lower() == "right"):
    if i < 3:
        n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
    elif i >= 3:
        n = input("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
    i = i + 1
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54651672

复制
相关文章

相似问题

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