0
#python : I want the correction of this exercise
Loop Write a program that asks the user for a 4-digit code and leaves him 3 attempts to find the correct password (the password to find is password = “2021"
11 Réponses
+ 3
isnt an attempt required to be corrected?
+ 2
attempts = 0
start = True
while start:
attempts += 1
in = int(input())
if in == 2021:
print("correct")
break
if attempts == 3:
start = False
+ 1
Mamadou Drame I saw this same question with a code attempt. Is this a duplicate?
EDIT: 👍 maybe it was someone else that has a similar similar class. In addition to ∆BH∆Y comment a counter variable to test and branch elsewhere after 3 attempts.
+ 1
Mamadou Drame
I'm not writing the code to improve yourself. I'm just hinting.😉
1. Use loop
2. Ask Password
3. Compare input with Password
4. Before loop, define a variable that holds the number of attempts
5. Increase this variable by 1 each time and compare with 3 attempts
6. If the variable is 3, print a warning and end the loop.
I wish you success😊
0
where is the code to correct?
0
I have an exam that our teacher gave us
0
what did u tried so far?
0
Hello ! well I have tried beautiful and there is no rush
0
There is a small modification
print ('password combo to continue')
count = 0
while count <3:
password = input ('Enter password:')
if password == '2021'
print ('Access granted')
break
0
#this is an advanced code, hopefully you will understand it and learn smthg from it
import re
trials = 0
pwd = 2021
pat = "\d{4}"
while trials <= 2:
try:
t = int(input("Enter password: "))
if t == pwd:
print("access granted")
break
elif re.search(pat, str(t)) == None:
print("only 4 digits allowed!")
trials += 1
print("Trial N°", trials)
else:
print("Access denied")
trials += 1
print("Trial N°", trials)
except ValueError:
print("Invalid input! only digits allowed!")
trials += 1
print("Trial N°", trials)
0
for i in "21":
if input() == "2021":
print("Access Granted!")
exit()
else: print("Attempt Failed!\n{} attempt{} left".format(i, "s" * (i > "1")))
raise ValueError("INTRUDER ALERT!")
# Hope this helps