在用户在最后两行代码中键入“是”之后,我试图再次重新启动代码。有办法吗?我还想提到我刚刚开始使用python
我尝试使用while命令,但是我不完全确定该怎么做。除此之外,我还没有尝试过其他任何方法。 任何帮助将不胜感激。
score = 0
final = 0
adj = 0
adjectives = "remarkable, massive, unhappy, unusual"
name = input("Hi there whats you name?")
print("Welcome {}, this quiz is about what type of food you are. Enjoy!".format(name))
#ask the user what their favourite food is.
answer_1 = input("What is you favourite food?")
if answer_1 == "chocolate":
print("Nice!")
score = score + 1
elif answer_1 == "pizza":
print("mmm pizza!")
score = score + 2
elif answer_1 == "spaghetti":
print("Yum!")
score = score + 3
else:
print("I love that!!")
score = score + 4
#ask the user what their favourite flavour of icecream is
answer_2 = input("What is your favourite flavour of icecream?")
if answer_2 == "vanilla":
score = score + 1
print("Sweet!")
elif answer_2 == "chocolate":
score = score + 2
print("Smooth!")
elif answer_2 == "cookies and cream":
score = score + 3
print("Yum Yum!")
else:
score = score + 4
print("I like that too!")
#ask the user how many weeks ago they baked
answer_3 = float(input("How many weeks ago did you bake?"))
if answer_3 == 1:
score = score + 1
print("You're an expert baker!!")
elif answer_3 >= 2 and answer_3 <= 3:
score = score +2
print("Nice!")
elif answer_3 >= 4 and answer_3 <= 6:
score = score + 3
print("Thats a long time!")
else:
score = score + 4
print("You need to bake!")
#ask the user if they like avocado
answer_4 = input("Do you like avocado?")
if answer_4 == "yes":
score = score + 1
print("Nice!")
elif answer_4 == "no":
score = score + 2
print("Yuk!")
else:
score = score + 3
print("Oh really!")
#ask the user their weight
answer_5 = float(input("How much do you weigh in Kg"))
if answer_5 <= 30 and answer_5 >= 0:
score = score + 1
print("Skinny!")
elif answer_5 >= 31 and answer_5 <= 45:
score = score + 2
print("Wow!")
elif answer_5 >= 46 and answer_5 <= 60:
score = score + 3
print("Thats a comfortable weight!")
elif answer_5 >= 61 and answer_5 <= 80:
score = score + 4
print("Thats a comfortable weight!")
elif answer_5 >= 81:
score = score +5
print("You need to lose some weight")
if score <= 8 and score >= 5:
final = final + 0
elif score <= 12 and score >= 9:
final = final + 1
elif score >= 13 and score <= 16:
final = final + 2
elif score >= 17 and score <= 20:
final = final + 3
if score <= 8 and score >= 5:
adj = adj + 0
elif score <= 12 and score >= 9:
adj = adj + 1
elif score >= 13 and score <= 16:
adj = adj + 2
elif score >= 17 and score <= 20:
adj = adj + 3
food = ["chocolate bar", "pizza", "spaghetti bowl", "beetroot"]
adjectives = ["a remarkable", "a massive", "an unhappy", "an unusual"]
print("You are {} {}".format(adjectives[adj], food[final]))
rating = input("How was that?")
print("Well that was fun! Thank you for taking this quiz, we hope you return {}".format(name))
again = input("would you like to start the quiz again?")
if again == "yes":
elif again == "no":
print("Bye then!")
exit()
else:
print("Please enter: yes or no")
答案 0 :(得分:0)
您想将所有内容都包装在一个函数中,像这样,只要您需要某种功能,就可以简单地调用该函数并在这里拥有它!
这是您应该做的:
def runProgram():
while(True):
playGame() # after this is done we will ask the user if they want to go again
if doesUserWantToPlayAgain() == True:
continue
else:
break
def playGame():
# All your code here
def doesUserWantToPlayAgain():
yes_or_no = input("would you like to start the quiz again?")
if yes_or_no == "yes":
return True
return False
runProgram() # call this as an entry point for your program
希望您有主意,随时提出问题:)