If statement trouble

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FlamingoRider
    New Member
    • Feb 2009
    • 11

    If statement trouble

    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?

    Code:
    toDo = []
    
    import time
    import os
    import random
    
    def welcome():
        print "What is something you wanna do?"
        firstToDo = raw_input()
        toDo.append(firstToDo)
    
    def otherChoices():
    	Running = True
    	print "Do you have any other choices?"
    	nextThing = str.upper(raw_input("Y or N: "))
    	if nextThing == "Y":
    		while Running == True:
    			print "What else do you want to add?"
    			print
    			addThing = raw_input()
    			toDo.append(addThing)
    			print addThing, "has been added to the list."
    			print "Is there anything else?"
    			print
    			moreChoices = str.upper(raw_input("Y or N: "))
    			if moreChoices == "Y":
    				Running = True
    			elif moreChoices == "N":
    				Running = False
    				nextThing = "N"
    			else:
    				Running = True
    	elif nextThing == "N":
    		Running = False
    	else:
    		pass
    			
    			
    
    def choose():
        print "Now we'll decide what to do."
        print "..."
        time.sleep(2)
        print "..."
        time.sleep(2)
        print random.choice(toDo)
        raw_input()
    
    welcome()
    if len(toDo) > 0:
        otherChoices()
    choose()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Actually, a while loop is a good way to obtain a valid response. Example:
    Code:
    while True:
        choice = raw_input("Other choices (Y or N)?")
        if choice.upper() == "Y":
            print "Y"
            break
        elif choice.upper() == "N":
            print "N"
            break
        else:
            print "Please enter 'Y' or 'N'"

    Comment

    • FlamingoRider
      New Member
      • Feb 2009
      • 11

      #3
      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:

      Code:
      toDo = []
      
      import time
      import os
      import random
      
      def welcome():
          print "What is something you wanna do?"
          firstToDo = raw_input()
          toDo.append(firstToDo)
      
      def otherChoices():
      	Running = True
      	print "Do you have any other choices?"
      	nextThing = str.upper(raw_input("Y or N: "))
      	while True:
      		if nextThing == "Y":
      			print "What else do you want to add?"
      			print
      			addThing = raw_input()
      			toDo.append(addThing)
      			print addThing, "has been added to the list."
      			print "Is there anything else?"
      			print
      			moreChoices = str.upper(raw_input("Y or N: "))
      			if moreChoices == "Y":
      				Running = True
      			elif moreChoices == "N":
      				Running = False
      				nextThing = " "
      			else:
      				print "Please enter Y or N: "
      				return
      		elif nextThing == "N":
      			break
      		else:
      			print "Please Enter Y or N: "
      			return

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        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:
        Code:
        def validate_input(prompt, valid=[]):
            valid = [s.upper() for s in valid]
            while True:
                user_input = raw_input(prompt).upper()
                if valid:
                    if user_input in valid:
                        return user_input
                    else:
                        print "You entered: %s. Please enter a valid response." % (user_input)
                        print "Valid responses are: %s" % ", ".join(['%s' % s for s in valid])
                else:
                    return user_input
        Example:
        Code:
        >>> validate_input("Enter Y or N", ['Y', 'N'])
        You entered: GGG. Please enter a valid response.
        Valid responses are: Y, N
        You entered: YY. Please enter a valid response.
        Valid responses are: Y, N
        'Y'
        >>> validate_input("Enter something")
        'ANYTHING'
        >>>

        Comment

        Working...