Step-by-step explanation:
The correct password and number are defined and stored in their respective variables. While loop is used to keep on taking input from the user until the user inputs correct password and number or user runs out of attempts. The number of attempts is set to 3 but can be changed to any number as you like.
The user is asked to input password first then we check if the user wants to quit? if true terminate program else continue to get the input number from the user.
Now we have password and number from the user, check if both are correct? if yes then grant the access else wrong credentials add one to the attempt, display the number of attempts left and repeat the whole loop again.
Python Code:
password = "milkyway"
number = 1847
attempts=0
while attempts < 3:
pas=str(input("Please Enter the Password\\"))
if pas=="quit":
print("You quit!")
break
num=int(input("Please Enter the Number\\"))
if pas==password and num==number:
print("Access Granted")
else:
print("Wrong credentials")
attempts+=1
print("Attempts left: ",3-attempts)
Output:
Test 1:
Please Enter the Password
waysnf
Please Enter the Number
1847
Wrong credentials
Attempts left: 2
Please Enter the Password
milkyway
Please Enter the Number
1942
Wrong credentials
Attempts left: 1
Please Enter the Password
milkyway
Please Enter the Number
1847
Access Granted
Test 2:
Please Enter the Password
menas
Please Enter the Number
4235
Wrong credentials
Attempts left: 2
Please Enter the Password
quit
You quit!
Note: feel free to ask in comments if you dont understand anything!