如何重复输入直到预期值

时间:2018-01-28 05:06:31

标签: python

def n_g():
   ...:     first_input = input("are you okay?")
   ...:     if first_input == "good":
   ...:         if input("are you sure") == "Yes":
   ...:             return "Nice"
   ...:         return ???
   ...:     return n_g()

我希望这部分代码可以让客户一次又一次地回答这个问题,直到他们说“是”"。我应该把什么放在???

4 个答案:

答案 0 :(得分:2)

你可以学习迭代,它是递归思维

def n_g():
    first_input = input("are you okay?")
    if first_input == "yes":
        print("Nice")
    else:
        n_g()

答案 1 :(得分:1)

你写了#34;是"而不是"是"在你的代码....因此你需要在那里进行更改,你可以再次调用函数

 def n_g():
        first_input = input("are you okay?")
        if first_input == "good":
            if input("are you sure") == "yes":
                return "Nice"
            return n_g()
        return n_g()

答案 2 :(得分:0)

def n_g(check):
    if check == False:
        first_input = input("are you okay?")
    else:
        first_input = "good"
    if first_input == "good":
        if input("are you sure") == "yes":
            return "Nice"
        return n_g(True)
    return n_g(False)
  
    
      

n_g(假)

    
  

答案 3 :(得分:-1)

使用其他功能确认

def conf():
    if input("are you sure") == "Yes":
        return True
    return conf()

def n_g():
    first_input = input("are you okay?")
    if first_input == "good":
        # this method will return if you say only "yes"
        conf()
        return "Nice"
    else:
        return n_g()