【问题标题】:Having trouble with while loops, input and strings in pythonpython中的while循环,输入和字符串有问题
【发布时间】:2023-03-14 11:59:02
【问题描述】:

我正在学习 python 并练习我制作一个简单的基于文本的冒险游戏的技能。

在游戏中,我想问玩家是否准备好开始。我通过创建一个 begin() 函数来做到这一点:

def begin():

     print(raw_input("Are you ready to begin? > "))

     while raw_input() != "yes":
         if raw_input() == "yes":
            break
            print(start_adventure())
        else: 
            print("Are you ready to begin? > ")

print(begin())

在我的代码下面是函数 start_adventure()

def start_adventure():
     print("Test, Test, Test")

当我运行程序时,它会启动,然后我会询问我是否准备好开始。然后它只是无限循环,如果我完全关闭 Powershell 并重新启动 Powershell,我只能退出程序。我究竟做错了什么?一旦玩家输入“是”,我怎样才能让循环停止?

【问题讨论】:

  • 如果您使用 raw_input() 两次,它将寻找两个不同的输入。您需要调用一次 raw_input() 然后将其分配给一个变量。
  • 你为什么要连续打三个电话raw_input?那么所有额外的print 电话是怎么回事?
  • 我补充说:x = raw_input() while x != 'yes': if x == 'yes': break print(start_adventure()) else: print(raw_input("你准备好了吗? begin? > ")) 它仍然只是无限循环。我不确定为什么当 x == 'yes' 时它没有中断
  • @melpomene 我正在尝试以字符串的形式从用户那里获取输入。是还是不是。所以我需要循环来获取该信息并使用它来停止循环或继续循环。一旦循环中断,我希望它继续执行下一个函数。
  • 你不需要break语句,把它拿出来。条件更改已经使代码“中断”出 while 循环。 while 循环的全部意义在于不需要 break 语句。这应该由 while 循环条件控制,而不是显式的中断调用。

标签: python string input while-loop user-input


【解决方案1】:

Python 3 解决方案

您不应多次致电raw_input()。只需实例化x,然后等到用户输入Y 来调用您的start_adventure 函数。这应该可以帮助您开始:

def start_adventure():

    print('We have started!')
    #do something here


def begin():

    x = None

    while x!='Y':
        x = input('Are you ready to begin (Y/N)?')
        if x=='Y':
            start_adventure()

begin()

【讨论】:

  • 这里使用python3而不是python2(只是提到op)
【解决方案2】:
  1. 您的原始输入函数(我假设它可以正常工作)从未分配给变量。相反,您在 print 语句中调用它,打印它的结果,然后在 while 循环条件中再次调用它。

  2. 您实际上永远不会满足 while 循环条件,因为您的输入未分配给变量。将 Raw_input("Are you ready to begin? >") 分配给一个变量来存储输入。然后使用变量进行while循环。确保在满足条件时在 while 循环中将变量重置为其他值。

  3. 您的程序流程也是错误的,您需要在 while 循环内调用原始输入函数。这将更改 while 循环条件,以便在满足条件时(用户键入“yes”)不会无限循环。希望这可以帮助!

您需要的代码形式示例:

//initialize the condition to no value
condition = None;
#check the condition
while condition != "yes"
    #change the condition here based on user input **inside the loop**
    condition = raw_input("are you ready to begin? >")
    if condition == "yes":
        #condition is met do what you need
    else:
        #condition not met loop again 
        #nothing needs to go here to print the message again

【讨论】:

    【解决方案3】:

    你希望这会做什么?您的问题的解决方案是尝试了解代码的作用,而不是仅仅将东西放在一起。 (别担心;我们中至少有 80% 的人曾经处于那个阶段!)

    顺便说一句,我强烈建议使用 Python 3 而不是 Python 2;他们制作了一个新版本的 Python,因为 Python 2 充满了非常奇怪、令人困惑的东西,例如导致安全漏洞的 input 和等于 210 / 4


    你想要这个做什么?

    • 反复询问用户是否准备好开始,直到他们回答 "yes"
    • 致电start_adventure()

    好的。让我们将目前为止的内容放入一个函数中:

    def begin():
        while something:
            raw_input("Are you ready to begin? > ")
    
        start_adventure()
    

    这里有很多空白,但这是一个开始。目前,我们正在获取用户的输入并将其丢弃,因为我们没有将其存储在任何地方。让我们解决这个问题。

    def begin():
        while something:
            answer = raw_input("Are you ready to begin? > ")
    
        start_adventure()
    

    这开始形成。我们只想继续循环while answer != "yes"...

    def begin():
        while answer != "yes":
            answer = raw_input("Are you ready to begin? > ")
    
        start_adventure()
    

    万岁!让我们看看这是否有效!

    Traceback (most recent call last):
      File "example", line 2, in <module>
        while answer != "yes":
    NameError: name 'answer' is not defined
    

    嗯...我们还没有为answer 设置值。为了使循环运行,它必须不等于"yes"。让我们一起去"no"

    def begin():
        answer = "no"
        while answer != "yes":
            answer = raw_input("Are you ready to begin? > ")
    
        start_adventure()
    

    这行得通!

    【讨论】:

    • 对于像 OP 这样的人,第一次尝试解决这个问题并理顺逻辑,我认为这是一个很好的分步演练!
    • @rahlf23 我​​决定开始回答“简单”的问题,并给出真正能帮助提问者的答案,而不是教他们 Stack Overflow 是一个神奇的小盒子,可以给他们所有他们想要的代码。
    • 非常感谢!!!这真的很有帮助,它将帮助我在未来解决其他问题!!!非常感谢!!!
    • @JacquelynneHeiman 如果这回答了您的问题,您可以单击此答案左上角投票按钮下的灰色复选标记。如果它变成绿色勾号,则表示它被标记为已接受的答案。如果你这样做,它会给我 15 点声望。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 2015-01-12
    • 2014-04-26
    • 2019-03-26
    • 2020-07-07
    • 2012-03-09
    相关资源
    最近更新 更多