如何重复代码

时间:2017-06-26 05:48:19

标签: python

我是python的初学者,我想重复我的代码来自Answer = raw_input("你想变得更好吗?")如果用户输入的内容不是no或yes,因为在说"你只能选择是或否"代码结束,它不会再问一次

Extract

3 个答案:

答案 0 :(得分:2)

你只需要while循环:)

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 " 

答案 1 :(得分:0)

要无限期重复代码块,请使用while 1:。例如:

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!!"  

答案 2 :(得分:0)

根据我的理解,您希望将代码运行到是,并将其结束为否。

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!!"  

这不是您的要求,请回复。