By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,001 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,001 IT Pros & Developers. It's quick & easy.

If statement trouble

FlamingoRider
P: 11
I wrote a short program that stores user input into a list, asks if there's anything else that needs to be entered, and eventually selects an item out of the list.

The problem I have is that if the user puts anything other than y or n when asked if there is anything else, it'll exit out of the if block and continue. Is there any way that I can make it loop back without using a while statement?

Expand|Select|Wrap|Line Numbers
  1.  
  2. toDo = []
  3.  
  4. import time
  5. import os
  6. import random
  7.  
  8. def welcome():
  9.     print "What is something you wanna do?"
  10.     firstToDo = raw_input()
  11.     toDo.append(firstToDo)
  12.  
  13. def otherChoices():
  14.     Running = True
  15.     print "Do you have any other choices?"
  16.     nextThing = str.upper(raw_input("Y or N: "))
  17.     if nextThing == "Y":
  18.         while Running == True:
  19.             print "What else do you want to add?"
  20.             print
  21.             addThing = raw_input()
  22.             toDo.append(addThing)
  23.             print addThing, "has been added to the list."
  24.             print "Is there anything else?"
  25.             print
  26.             moreChoices = str.upper(raw_input("Y or N: "))
  27.             if moreChoices == "Y":
  28.                 Running = True
  29.             elif moreChoices == "N":
  30.                 Running = False
  31.                 nextThing = "N"
  32.             else:
  33.                 Running = True
  34.     elif nextThing == "N":
  35.         Running = False
  36.     else:
  37.         pass
  38.  
  39.  
  40.  
  41. def choose():
  42.     print "Now we'll decide what to do."
  43.     print "..."
  44.     time.sleep(2)
  45.     print "..."
  46.     time.sleep(2)
  47.     print random.choice(toDo)
  48.     raw_input()
  49.  
  50. welcome()
  51. if len(toDo) > 0:
  52.     otherChoices()
  53. choose()
  54.  
  55.  
Feb 24 '09 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Actually, a while loop is a good way to obtain a valid response. Example:
Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     choice = raw_input("Other choices (Y or N)?")
  3.     if choice.upper() == "Y":
  4.         print "Y"
  5.         break
  6.     elif choice.upper() == "N":
  7.         print "N"
  8.         break
  9.     else:
  10.         print "Please enter 'Y' or 'N'"
Feb 24 '09 #2

FlamingoRider
P: 11
I ended up putting the while statement in there but now whenever they type something other than Y or N it ends up looping the print statement under the else:.

I thought putting a return afterwards would solve it but that just causes the program to move to the next function.

Any suggestions?

Current code:

Expand|Select|Wrap|Line Numbers
  1. toDo = []
  2.  
  3. import time
  4. import os
  5. import random
  6.  
  7. def welcome():
  8.     print "What is something you wanna do?"
  9.     firstToDo = raw_input()
  10.     toDo.append(firstToDo)
  11.  
  12. def otherChoices():
  13.     Running = True
  14.     print "Do you have any other choices?"
  15.     nextThing = str.upper(raw_input("Y or N: "))
  16.     while True:
  17.         if nextThing == "Y":
  18.             print "What else do you want to add?"
  19.             print
  20.             addThing = raw_input()
  21.             toDo.append(addThing)
  22.             print addThing, "has been added to the list."
  23.             print "Is there anything else?"
  24.             print
  25.             moreChoices = str.upper(raw_input("Y or N: "))
  26.             if moreChoices == "Y":
  27.                 Running = True
  28.             elif moreChoices == "N":
  29.                 Running = False
  30.                 nextThing = " "
  31.             else:
  32.                 print "Please enter Y or N: "
  33.                 return
  34.         elif nextThing == "N":
  35.             break
  36.         else:
  37.             print "Please Enter Y or N: "
  38.             return
  39.  
  40.  
Feb 24 '09 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Fixing your code to work properly seems to be more work than it should be. Consider obtaining user input with a function. Something like this:
Expand|Select|Wrap|Line Numbers
  1. def validate_input(prompt, valid=[]):
  2.     valid = [s.upper() for s in valid]
  3.     while True:
  4.         user_input = raw_input(prompt).upper()
  5.         if valid:
  6.             if user_input in valid:
  7.                 return user_input
  8.             else:
  9.                 print "You entered: %s. Please enter a valid response." % (user_input)
  10.                 print "Valid responses are: %s" % ", ".join(['%s' % s for s in valid])
  11.         else:
  12.             return user_input
Example:
Expand|Select|Wrap|Line Numbers
  1. >>> validate_input("Enter Y or N", ['Y', 'N'])
  2. You entered: GGG. Please enter a valid response.
  3. Valid responses are: Y, N
  4. You entered: YY. Please enter a valid response.
  5. Valid responses are: Y, N
  6. 'Y'
  7. >>> validate_input("Enter something")
  8. 'ANYTHING'
  9. >>> 
Feb 24 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.