using while with multiple strings

This is the place for queries that don't fit in any of the other categories.

using while with multiple strings

Postby mckryall » Sat Nov 23, 2013 8:58 pm

I am attempting to make a prompt in which you can type 'yes' or 'y' to continue. I am getting the error
"Traceback (most recent call last):
File "text_adventure.py", line 7, in <module>
while (input("> ") != 'y' or 'Y' or 'yes' or 'Yes' or 'YES' or 'YEs' or 'YeS' or 'yeS' or 'yES'):
File "<string>", line 1, in <module>
NameError: name 'y' is not defined"

Here is my code:
Code: Select all
while (input("> ") != 'y' or 'Y' or 'yes' or 'Yes' or 'YES' or 'YEs' or 'YeS' or 'yeS' or 'yES'):
   print "Yes only."
print "Ah, Good. Let's continue."
Last edited by Mekire on Sat Nov 30, 2013 7:39 am, edited 1 time in total.
Reason: Locked.
Python 2.7
Mac Mini (Desktop/server)
Macbook Air
Laptop w/ Ubuntu 14.04
mckryall
 
Posts: 110
Joined: Sat Nov 23, 2013 6:13 pm

Re: using while with multiple strings

Postby stranac » Sat Nov 23, 2013 9:12 pm

You need to use raw_input() to get a string.

Also, your condition is not correct, it will always be True.
You can actually write it a lot simpler:
Code: Select all
while raw_input("> ").lower() not in ('y', 'yes'):
   print "Yes only."
print "Ah, Good. Let's continue."
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1244
Joined: Thu Feb 07, 2013 3:42 pm

Re: using while with multiple strings

Postby mckryall » Sat Nov 23, 2013 11:24 pm

I honestly had no idea how to use while, so I copied someone else's and spit it out with my own variables.
Last edited by Mekire on Sat Nov 30, 2013 7:40 am, edited 1 time in total.
Reason: Locked.
Python 2.7
Mac Mini (Desktop/server)
Macbook Air
Laptop w/ Ubuntu 14.04
mckryall
 
Posts: 110
Joined: Sat Nov 23, 2013 6:13 pm

Re: using while with multiple strings

Postby Somelauw » Sun Nov 24, 2013 12:07 am

stranac wrote:You need to use raw_input() to get a string.


Depends on whether he is using python2 (raw_input) or python3 (input).
In python2, there is also an input method, but that's an unsafe function.
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 75
Joined: Tue Feb 12, 2013 8:30 pm

Re: using while with multiple strings

Postby stranac » Sun Nov 24, 2013 12:23 am

Somelauw wrote:Depends on whether he is using python2 (raw_input) or python3 (input).

Well, that's pretty obvious from the traceback.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1244
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: chris0147, Google [Bot], ichabod801, pegasus, Yoriz and 5 guests