如何在Python中制作代码循环3次以及围绕它循环

时间:2015-12-02 15:31:27

标签: python

使用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!!!!!!")

4 个答案:

答案 0 :(得分:2)

user = ("1234")

counter = 0

while counter < 3:

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

    if len(user) != 4:
        print("I said 4 digits!")
        continue

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

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

if counter == 3:
    print ("You have been locked out!! Alarm!!!!!!")
else:
    print ("Everything is fine.")

答案 1 :(得分:0)

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

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...

如果现在答案错误,它会计算在内

答案 2 :(得分:0)

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

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

为了清晰的流动,我省略了印刷品

答案 3 :(得分:0)

以下代码应该适合您。

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

我从字符串中取出()因为这会使它们成立,所以if语句永远不会成立,因为set不等于字符串输入。