Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的while循环、输入和字符串有问题_Python_String_Input_While Loop_User Input - Fatal编程技术网

python中的while循环、输入和字符串有问题

python中的while循环、输入和字符串有问题,python,string,input,while-loop,user-input,Python,String,Input,While Loop,User Input,我正在学习python并练习制作一个简单的基于文本的冒险游戏的技能 在游戏中,我想问玩家他们是否准备好开始了。为此,我创建了一个begin()函数: def begin(): print(raw_input("Are you ready to begin? > ")) while raw_input() != "yes": if raw_input() == "yes": break print(st

我正在学习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())
def begin():
    while something:
        raw_input("Are you ready to begin? > ")

    start_adventure()
下面是我的代码中的函数start\u advention()


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

Python 3解决方案

您不应该多次调用
raw\u input()
。只需实例化
x
,然后等待用户输入
Y
即可调用
start\u冒险
函数。这应该让你开始:

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()
  • 您的原始输入函数(我假设它工作正常)从未分配给变量。而是在print语句中调用它,打印它的结果,然后在while循环条件中再次调用它

  • 您永远不会真正满足while循环条件,因为您的输入没有分配给变量。将原始输入(“准备好开始了吗?>”)分配给变量以存储输入。然后,while循环变量。确保在while循环中,当条件满足时,将变量重置为其他变量

  • 您的程序流也是错误的,您需要在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
    

    你希望这能做什么?解决问题的办法是试着理解代码的作用,而不是把东西拼凑在一起。(别担心,我们中至少有80%的人曾经处于这个阶段!)

    另外,我强烈建议使用Python3而不是Python2;他们制作了一个新版本的Python,因为Python 2充满了非常奇怪、令人困惑的东西,比如导致安全漏洞的
    input
    ,以及
    10/4
    等于
    2


    你想让它做什么

    • 反复询问用户是否准备好开始,直到他们回答“是”
    • 调用
      开始冒险()
    嗯。让我们把我们目前所得到的东西放到一个函数中:

    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())
    
    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()
    
    这已经开始成形。我们只想在应答时继续循环
    “是”

    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
    

    这会有用的

    如果两次使用raw_input(),它将查找两个不同的输入。您需要调用raw_input()一次,然后将其分配给一个变量。为什么要连续调用
    raw_input
    三次?所有额外的
    print
    调用是怎么回事?我添加了:x=raw\u input(),而x!='yes':如果x='yes':中断打印(start_adventure())否则:打印(原始输入(“准备好开始了吗?”),它仍然无限循环。我不确定为什么当x=='yes'@melpomene我试图以字符串的形式从用户那里获取输入时,它没有中断。是或否。所以我需要循环获取该信息,并使用它停止循环或继续循环。一旦循环中断,我希望它继续到下一个函数。你不需要break语句,把它去掉。条件更改将使代码“中断”出while循环。while循环的全部要点是不需要break语句。这应该由while循环条件控制,而不是显式的中断调用。对于像op这样的人来说,这使用了python3而不是python2(只是提到op),他们第一次尝试解决这个问题并理清逻辑,我认为这是一个很好的逐步演练@rahlf23我决定开始回答“简单”的问题,回答的答案实际上会帮助提问者,而不是教他们堆栈溢出是一个神奇的小盒子,可以给他们想要的所有代码。非常感谢!!!这真的很有帮助,它将帮助我有一个在未来的其他问题去尝试!!!非常感谢@JacquelynneHeiman如果这回答了您的问题,您可以单击此答案左上角投票按钮下的灰色复选标记。如果它变成绿色的勾号,这意味着它被标记为已接受的答案。如果你那样做,我会得到15分的声誉。