Python 巨蟒;“中断”;断开外部循环的语句

Python 巨蟒;“中断”;断开外部循环的语句,python,loops,Python,Loops,当用户完成游戏(回答所有问题)时,游戏应该中断游戏循环并返回密码提示,但它会中断整个程序循环和游戏while循环并结束程序。 有什么想法吗 access = 1 score = 0 print("Hello, user.\n") while i < 5: input1 = input("Enter Password:\n") if input1 == password: print("Access Granted\n\n---------------

当用户完成游戏(回答所有问题)时,游戏应该中断游戏循环并返回密码提示,但它会中断整个程序循环和游戏while循环并结束程序。 有什么想法吗

access = 1
score = 0

print("Hello, user.\n")

while i < 5:

    input1 = input("Enter Password:\n")

    if input1 == password:
        print("Access Granted\n\n-----------------------------\n")


        r2 = input("Enter 1: Play 20 Questions\nEnter 2: Change your password\nEnter 3: Decrypt a message\nEnter 4: Encrypt a Message\nEnter 5: Exit Terminal\n\n")

        #20 Questions
        while r2 == "1" and access == 1:


            questions = ["Who was the first president of the United States?", "what is the USA's biggest state?", "What USA city has the most tourism?", "Where are most tech companies located?"
                     , "how many branches of the military are there?", "how many states are in the US", "When was the Decleration of Independence signed?", 
                     "What form of government is the USA?", "", "", ""]
            answers = ["george washington", "alaska", "new york", "silicon valley", "5", "50", "1777", "Constitutional Republic"]


            print(questions[i])
            answer = input("")

            #If answer is correct
            if answer == answers[i]:
                score += 1
                print("Correct! your score is now")
                print(score)
                i += 1

                if i == 7:
                    break
            else:
                print("Wrong, your score is still")
                print(score)
                i += 1

                if i == 7:
                    break

    else:
        print("\nAccess Denied\n")
        i += 1

        if i == 5:
            print("You have run out of attempts.")
            access = 2
        continue
access=1
分数=0
打印(“你好,用户。\n”)
当我<5时:
input1=输入(“输入密码:\n”)
如果input1==密码:
打印(“已授予访问权限\n\n------------------------------------\n”)
r2=输入(“输入1:播放20个问题\n输入2:更改密码\n输入3:解密消息\n输入4:加密消息\n输入5:退出终端\n\n”)
#20个问题
而r2==“1”和access==1:
问题=[“谁是美国第一任总统?”,“美国最大的州是什么?”,“美国哪个城市的旅游业最多?”,“大多数科技公司位于哪里?”
“有多少个军种?”,“美国有多少个州?”,“废除独立是什么时候签署的?”,
“美国是一个什么样的政府?”
答案=[“乔治·华盛顿”、“阿拉斯加”、“纽约”、“硅谷”、“5”、“50”、“1777”、“宪法共和国”]
打印(问题[i])
答案=输入(“”)
#如果答案是正确的
如果答案==答案[i]:
分数+=1
打印(“正确!您的分数现在为”)
打印(分数)
i+=1
如果i==7:
打破
其他:
打印(“错误,您的分数仍然为”)
打印(分数)
i+=1
如果i==7:
打破
其他:
打印(“\n访问被拒绝\n”)
i+=1
如果i==5:
打印(“尝试次数已用完。”)
访问权限=2
持续

当代码到达break语句时,您将得到
i=7
,因此当Python重新计算主循环的条件时,它将为false。似乎您应该让它成为一个简单的
while True
循环,以便break语句达到预期效果


在第二次运行游戏之前,请确保适当地重置
i

当i<5时,您的外部while循环具有条件
,但当
i
7
时,您的中断语句发生。。。
break
关键字正在从内部循环中断,但由于
i
不是
<5
,外部循环也将完成。

如何使用函数?