Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中使代码循环3次,并在其周围进行while循环_Python - Fatal编程技术网

如何在Python中使代码循环3次,并在其周围进行while循环

如何在Python中使代码循环3次,并在其周围进行while循环,python,Python,在使用python方面非常有限,而且完全卡住了,我已经设法在下面的代码上运行了一个while循环,这样用户就可以一直输入代码,直到输入正确的代码为止 我现在要做的是添加一个for循环,这样它只要求用户输入代码(4个错误的数字)3次,然后将其锁定。同时,它需要一个while循环,以确保如果用户输入的数字大于或小于4位,它将继续运行,并且不会将其锁定 我就是不能让for循环和while循环同时工作,不知道我做错了什么 user = ("1234") valid = False while not

在使用python方面非常有限,而且完全卡住了,我已经设法在下面的代码上运行了一个while循环,这样用户就可以一直输入代码,直到输入正确的代码为止

我现在要做的是添加一个for循环,这样它只要求用户输入代码(4个错误的数字)3次,然后将其锁定。同时,它需要一个while循环,以确保如果用户输入的数字大于或小于4位,它将继续运行,并且不会将其锁定

我就是不能让for循环和while循环同时工作,不知道我做错了什么

user = ("1234")

valid = False

while not valid:

   #for i in range (3):  

        user = input("Hello, welcome! Please enter a four digit passcode to open the safe: ")
        user = user.upper()          

        if user == ("1234") :
            print("You have cracked the code, well done")
            valid = True
            break

        if user != ("1234") :
            print ("that is incorrect, please try again")
            valid = False



        elif len(user) > 4:
            print ("That is incorrect, please try again")
            valid = False

        elif len(user) < 4:
            print ("That is incorrect, please try again")
            valid = False
        else:
            print ("You have been locked out!! Alarm!!!!!!")
user=(“1234”)
有效=错误
虽然无效:
#对于范围(3)中的i:
user=input(“您好,欢迎!请输入四位密码以打开保险箱:”)
user=user.upper()
如果用户==(“1234”):
打印(“您破解了代码,做得好”)
有效=真
打破
如果用户!=("1234") :
打印(“不正确,请重试”)
有效=错误
elif len(用户)>4:
打印(“不正确,请重试”)
有效=错误
elif len(用户)<4:
打印(“不正确,请重试”)
有效=错误
其他:
打印(“您已被锁定!!报警!!!”)
用户=(“1234”)
计数器=0
当计数器<3时:
user=input(“您好,欢迎!请输入四位密码以打开保险箱:”)
user=user.upper()
如果len(用户)!=4:
打印(“我说的是4位数字!”)
持续
如果用户==(“1234”):
打印(“您破解了代码,做得好”)
打破
打印(“不正确,请重试”)
计数器+=1
如果计数器==3:
打印(“您已被锁定!!报警!!!”)
其他:
打印(“一切正常”)

我稍微修改了您的代码,并添加了一个计数器变量

user = ("1234")

valid = False
counter = 0

while not valid:

        user = input("Hello, welcome! Please enter a four digit passcode to open the safe: ")
        user = user.upper()          

        if user == ("1234") :
            print("You have cracked the code, well done")
            valid = True
            break

        elif len(user) != 4:
            print ("That is incorrect, please try again")

        else:
            print ("that is incorrect, please try again")
            counter = counter + 1

            if counter == 3:
                print ("You have been locked out!! Alarm!!!!!!")

                #Do stuff to actually abort here...

如果现在有一个错误的答案,它将计数。

内部循环仅用于有效输入:

user = '1234'
locked = False
miss_cnt = 0
while True:
    while True:
        ans = raw_input('User -> ')
        if len(ans) == 4 and ans.isdigit():
            break

    if ans != user:
        miss_cnt += 1
        if miss_cnt >= 3:
            locked = True
            break
    else:
        break

为了清晰起见,我省略了打印内容

以下代码应该适合您

answer = "1234"

valid = False
count = 0

while not valid and count < 3:
    user = input("Hello, welcome! Please enter a four digit passcode to open the safe: ")
    user = user.upper()          

    if user == answer:
        print("You have cracked the code, well done")
        valid = True

    elif count < 2:
        print ("that is incorrect, please try again")

    else:
        print ("You have been locked out")

    count += 1
answer=“1234”
有效=错误
计数=0
无效且计数小于3时:
user=input(“您好,欢迎!请输入四位密码以打开保险箱:”)
user=user.upper()
如果用户==回答:
打印(“您破解了代码,做得好”)
有效=真
elif计数<2:
打印(“不正确,请重试”)
其他:
打印(“您已被锁定”)
计数+=1

我从字符串中去掉了
()
,因为这会使它们成为集合,所以if语句永远不会为真,因为集合不等于字符串输入。

对于某些赋值,它是否需要是for循环,或者在无效时添加一个简单的计数器和
,而计数器<3
起作用?它确实说是for循环和while循环。它还说了一个我不知道是什么的案例陈述:(谢谢你!非常好的帮助:)谢谢你!!这帮了大忙!
answer = "1234"

valid = False
count = 0

while not valid and count < 3:
    user = input("Hello, welcome! Please enter a four digit passcode to open the safe: ")
    user = user.upper()          

    if user == answer:
        print("You have cracked the code, well done")
        valid = True

    elif count < 2:
        print ("that is incorrect, please try again")

    else:
        print ("You have been locked out")

    count += 1