问题
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