Having trouble with while loops, input and strings in python
Clash Royale CLAN TAG#URR8PPP
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"?
Why are you calling
raw_input
three times in a row? And what's with all the extra print
calls?– melpomene
Aug 10 at 14:40
raw_input
print
i added: x = raw_input() while x != 'yes': if x == 'yes': break print(start_adventure()) else: print(raw_input("Are you ready to begin? > ")) It still just loops infinitely. I am unsure why it is not breaking when x == 'yes'
– Jacquelynne Heiman
Aug 10 at 14:49
@melpomene I am trying to get input from the user in the form of a string. yes or no. So i need the loop to take that information and use it to stop the loop or continue the loop. Once the loop is broken I want it to continue on to the next function.
– Jacquelynne Heiman
Aug 10 at 14:52
you don't need the break statement, take it out. The condition changing will already make the code 'break' out of the while loop. The whole point of while loops is to not need a break statement. That should be controlled by the while loop condition, not an explicit break call.
– Redacted
Aug 10 at 14:54
3 Answers
3
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:
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()
This uses python3 instead of python2 (just mentioning for the op)
– Overholt
Aug 10 at 14:46
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
.
input
10 / 4
2
What do you want this to do?
"yes"
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. In order to make the loop run, it has to be something that isn't equal to "yes"
. Let's go with "no"
:
answer
"yes"
"no"
def begin():
answer = "no"
while answer != "yes":
answer = raw_input("Are you ready to begin? > ")
start_adventure()
This will work!
For someone, like the OP, trying to work through this for the first time and get the logic straight, I thought this was a great step-by-step walkthrough!
– rahlf23
Aug 10 at 14:53
@rahlf23 I've decided to start answering the "easy" questions with answers that will actually help the asker, as opposed to teaching them that Stack Overflow is a magic little box that'll give them all of the code they want.
– wizzwizz4
Aug 10 at 14:54
Thanks sooo much!!! This is really help and it will help me have a go at other problems in the future!!! Thanks so much!!!
– Jacquelynne Heiman
Aug 10 at 15:18
@JacquelynneHeiman If this answered your question, you can click the grey checkmark under the voting buttons in the top left of this answer. If it turns into a green tick, that means that it's marked as the accepted answer. It gives me 15 reputation points if you do that.
– wizzwizz4
Aug 10 at 15:24
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.
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.
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
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
If you use raw_input() twice it will be looking for two different inputs. You need to call raw_input() once and then assign it to a variable.
– Zach
Aug 10 at 14:40