Jacquelynne Heiman
Jacquelynne Heiman

Reputation: 39

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"?

Upvotes: 3

Views: 7771

Answers (3)

wizzwizz4
wizzwizz4

Reputation: 6426

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!

Upvotes: 4

Redacted
Redacted

Reputation: 633

  1. Your Raw input function (I'm assuming it works correctly) is never assigned to a variable. Instead you call it in your print statement, print the result of it and then you call it again in your while loop condition.

  2. You never actually satisfy the while loop condition because your input isn't assigned to a variable. Assign Raw_input("Are you ready to begin? >") to a variable to store the input. Then while loop with the variable. Make sure in your while loop when the condition is met you reset the variable to something else.

  3. Your program flow is wrong too, you need to call your raw input function inside the while loop. This will change the while loop condition so that when the condition is met (user types "yes") it won't loop infinitely. 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

Upvotes: 1

rahlf23
rahlf23

Reputation: 9019

Python 3 Solution

You should not be calling raw_input() multiple times. Simply instantiate x and then wait until the user inputs Y to call your start_adventure function. 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()

Upvotes: 3

Related Questions