Python 你将如何编程一个循环,循环某个问题直到正确答案?我如何对消息进行编码以表明他们回答错误

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 有

我一个月前刚开始学习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
有几件事我注意到了,但不知道如何解决

  • 我无法设定加入该党的人数上限,如果没有这个上限,无论发生什么情况,它都会不断循环输入消息
  • 我无法停止负输入
  • 我无法输入“请在下面输入一个数字” 很抱歉,如果这是愚蠢的,我只是来到一个网站,通常用python修复我的问题,但找不到确切修复我的问题的东西

  • 它给了我一个关于“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