Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

In the game, I want to ask the player if they are ready to begin. I did this by creating a begin() function:

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

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. What am I doing wrong? How can I get the loop to stop once the player inputs "yes"?

like image 658
Jacquelynne Heiman Avatar asked Jul 23 '26 00:07

Jacquelynne Heiman


1 Answers

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

As an aside, I strongly recommend using Python 3 instead of 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.


What do you want this to do?

  • Repeatedly ask the user whether they are ready to begin until they answer "yes".
  • Call 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"...

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. In order to make the loop run, it has to be something that isn't equal to "yes". Let's go with "no":

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

    start_adventure()

This will work!

like image 191
wizzwizz4 Avatar answered Jul 25 '26 15:07

wizzwizz4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!