简体   繁体   English

在使用while循环,输入和python中的字符串时遇到麻烦

[英]Having trouble with while loops, input and strings in python

I am learning python and practicing my skills my making a simple text based adventure game. 我正在学习python,并通过制作基于文本的简单冒险游戏来练习技能。

In the game, I want to ask the player if they are ready to begin. 在游戏中,我想问玩家是否准备开始。 I did this by creating a begin() function: 我通过创建一个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())

below this in my code is the function start_adventure() 在我的代码下面是函数start_adventure()

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

When I run the program it starts up and I get to the point where it asks if I am ready to begin. 当我运行程序时,它会启动,并提示我是否准备开始。 Then it just loops infinitely and I can only exit the program if I completely close Powershell and restart Powershell. 然后它无限循环,只有完全关闭Powershell并重新启动Powershell时,我才能退出程序。 What am I doing wrong? 我究竟做错了什么? How can I get the loop to stop once the player inputs "yes"? 玩家输入“是”后,如何使循环停止?

Python 3 Solution Python 3解决方案

You should not be calling raw_input() multiple times. 您不应多次调用raw_input() Simply instantiate x and then wait until the user inputs Y to call your start_adventure function. 只需实例化x ,然后等到用户输入Y来调用start_adventure函数。 This should get you started: 这应该使您开始:

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()

What do you expect this to do? 您期望这做什么? The solution to your problem is to try to understand what the code does, instead of just throwing stuff together. 解决您的问题的方法是尝试理解代码的作用,而不是将所有内容混在一起。 (Don't worry; at least 80% of us were at that stage at one point!) (不用担心;我们中至少有80%处于这一点!)

As an aside, I strongly recommend using Python 3 instead of Python 2; 顺便说一句,我强烈建议您使用Python 3而不是Python 2。 they made a new version of Python because Python 2 was full of really strange, confusing stuff like input causing security vulnerabilities and 10 / 4 equalling 2 . 他们提出的Python的新版本,因为Python 2充满真的很奇怪的,扑朔迷离的东西,如input造成的安全漏洞和10 / 4等于2


What do you want this to do? 你想做什么?

  • Repeatedly ask the user whether they are ready to begin until they answer "yes" . 反复询问用户是否准备好开始,直到他们回答"yes"
  • Call start_adventure() . 调用start_adventure()

Ok. 好。 Let's put what we've got so far into a function: 让我们将到目前为止的内容放入一个函数中:

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

    start_adventure()

There are a lot of gaps in here, but it's a start. 这里有很多差距,但这是一个开始。 Currently, we're getting the user's input and throwing it away, because we're not storing it anywhere. 当前,我们正在获取用户的输入并将其丢弃,因为我们没有将其存储在任何地方。 Let's fix that. 让我们修复它。

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

    start_adventure()

This is starting to take shape. 这开始成形。 We only want to keep looping while answer != "yes" ... 我们只想while answer != "yes"保持循环...

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

    start_adventure()

Hooray! 万岁! Let's see if this works! 让我们看看这是否有效!

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

Hmm... We haven't set a value for answer yet. 嗯...我们尚未设置answer的值。 In order to make the loop run, it has to be something that isn't equal to "yes" . 为了使循环运行,它必须不等于"yes" Let's go with "no" : 让我们使用"no"

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

    start_adventure()

This will work! 这将起作用!

  1. Your Raw input function (I'm assuming it works correctly) is never assigned to a variable. 您的Raw输入函数(我假设它正常工作)永远不会分配给变量。 Instead you call it in your print statement, print the result of it and then you call it again in your while loop condition. 而是在打印语句中调用它,打印它的结果,然后在while循环条件下再次调用它。

  2. You never actually satisfy the while loop condition because your input isn't assigned to a variable. 实际上,您永远不会满足while循环条件,因为您的输入没有分配给变量。 Assign Raw_input("Are you ready to begin? >") to a variable to store the input. 将Raw_input(“您准备好开始了吗?>”)分配给变量以存储输入。 Then while loop with the variable. 然后在while循环中使用变量。 Make sure in your while loop when the condition is met you reset the variable to something else. 确保在满足条件的while循环中,将变量重置为其他值。

  3. Your program flow is wrong too, you need to call your raw input function inside the while loop. 您的程序流程也是错误的,您需要在while循环内调用原始输入函数。 This will change the while loop condition so that when the condition is met (user types "yes") it won't loop infinitely. 这将更改while循环条件,以便在满足条件(用户键入“是”)时,它不会无限循环。 Hope this helps! 希望这可以帮助!

Example of what you need in code form: 您需要的代码形式示例:

//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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM