1

I am a beginner in python and I wanted to repeat my code from the Answer =raw_input("Do you want to get better?") if the user enters anything other than no or yes, because after saying "you can only choose yes or no " the code ends and it doesn't ask again

choosing_options = ["Yes","No"] 


 Answer = raw_input("Do you want to become Better?")    
 if Answer == 'Yes' :
   print 'Great We Will Start Tommorow, meet me at Jhon\'s backyard at 3 AM  '
 elif Answer == 'No' :
     print "Well too bad, meet me again if you change your mind " 
 elif Answer != choosing_options :
      print "You can only choose yes or no!!"  
Saurav Jamwal
  • 399
  • 3
  • 12
Cheeseu
  • 13
  • 4
  • Also note that `Answer != choosing_options` will always evaluate to True, because you are comparing a string with a list! – Priyank Jun 26 '17 at 06:03

3 Answers3

2

You just need a while loop :)

choosing_options = ["Yes","No"] 
answer = None

while (answer not in choosing_options):
    answer = raw_input("Do you want to become better?")

    if answer == 'Yes' :
        print 'Great We Will Start Tommorow, meet me at Jhon\'s backyard at 3 AM  '
    elif answer == 'No' :
        print "Well too bad, meet me again if you change your mind " 
havanagrawal
  • 1,039
  • 6
  • 12
  • 2
    If you're interested in learning more, a [tutorial](https://docs.python.org/3/tutorial/controlflow.html) about control flow :) – Priyank Jun 26 '17 at 05:56
0

To repeat a block of code indefinitely, use while 1:. For example:

choosing_options = ["Yes","No"] 

while 1:
  Answer = raw_input("Do you want to become Better?")    
  if Answer == 'Yes' :
    print 'Great We Will Start Tommorow, meet me at Jhon\'s backyard at 3 AM  '
  elif Answer == 'No' :
    print "Well too bad, meet me again if you change your mind " 
  elif Answer != choosing_options :
    print "You can only choose yes or no!!"  
Cisplatin
  • 2,860
  • 3
  • 36
  • 56
0

as per my understanding you want to run your code till Yes and end it at No.

choosing_options = ["Yes","No"] 
Answer = "Yes"

while Answer == "Yes":
  Answer = raw_input("Do you want to become Better?")    
  if Answer == 'Yes' :
    print 'Great We Will Start Tommorow, meet me at Jhon\'s backyard at 3 AM  '
  elif Answer == 'No' :
    print "Well too bad, meet me again if you change your mind " 
  elif Answer != choosing_options :
    print "You can only choose yes or no!!"  

It it is not your requirement, please reply.

pramesh
  • 1,914
  • 1
  • 19
  • 30
  • No i want to make the code keep repeating until the user answers Yes or No.if he said something like "Hell yeah" it would tell him that he can only choose yes or no but the problem is that the code stops running after saying that it doesn't repeat the question so the user can't answer again with yes or no but @Havan Agrawal has already helped me solve this problem but thanks anyway :D – Cheeseu Jun 26 '17 at 06:18