Make python repeat a string until Yes or yes is inputted

橙三吉。 提交于 2019-12-12 02:08:18

问题


Yes I know that there is already a post covering this, but when I read it, it didn't help so please don't mark this as a duplicate.

I want to write a program that asks the user if they want advice and if they input "No" or "no" I want it to repeat the question and if they input "Yes" or "yes" I want it to print the advice. I want it to include a while loop

I have tried to write it myself but I can't get it to work correctly.

Anyone know?

Code from the comment for 3.4 -

def doQuestion(question, advice):
reply = ("no")

while reply  == "no":


print (question)





reply = input("Do you need some advice? ").lower()





if (reply == "yes"):


print ("Always listen to your IT teachers") 



 doQuestion("Do you want some advice?","Always listen to your IT teachers")

回答1:


The following will keep on checking up on the user to see whether or not they need advice regarding a question:

def doTheyNeedAdvice(advice):
    while raw_input("Do you need advice? ").lower() == "no":
        pass
    print (advice)

print "How old am I?"

doTheyNeedAdvice("I am old enough")


来源:https://stackoverflow.com/questions/42884392/make-python-repeat-a-string-until-yes-or-yes-is-inputted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!