Python 你将如何编程一个循环,循环某个问题直到正确答案?我如何对消息进行编码以表明他们回答错误
我一个月前刚开始学习python,我一直想创建一个choose your story提示符 看起来像这样Python 你将如何编程一个循环,循环某个问题直到正确答案?我如何对消息进行编码以表明他们回答错误,python,python-3.x,loops,Python,Python 3.x,Loops,我一个月前刚开始学习python,我一直想创建一个choose your story提示符 看起来像这样 while True: try: party = int(input("How many people joined the party ")) except ValueError: print("Sorry, I didn't understand that.") continue 有
while True:
try:
party = int(input("How many people joined the party "))
except ValueError:
print("Sorry, I didn't understand that.")
continue
有几件事我注意到了,但不知道如何解决
它给了我一个关于“print(f{party}people joined”)”的错误,你提供了什么输入?f-strings是从python 3.6+开始的,所以如果你有一个较低的版本,你必须使用:
print({:d}people joined.format(party))
。我更新了答案,使之与python 3.6兼容
while True:
try:
party = int(input("How many people joined the party (1-5)?:"))
except ValueError:
print("\nSorry, I didn't understand that.")
continue
if 0 < party <= 5:
print(f"{party} people joined")
break
print(f"\nInvalid answer, please insert a number between 1 and 5")
How many people joined the party (1-5)?: 0
Invalid answer, please insert a number between 1 and 5
How many people joined the party (1-5)?: I and my best friend
Sorry, I didn't understand that.
How many people joined the party (1-5)?: 3
3 people joined