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"

1st Mar 2021, 9:30 PM
Mamadou Drame
Mamadou Drame - avatar
12 Answers
+ 3
isnt an attempt required to be corrected?
1st Mar 2021, 9:51 PM
Alexander Thiem
Alexander Thiem - avatar
+ 2
attempts = 0 start = True while start: attempts += 1 in = int(input()) if in == 2021: print("correct") break if attempts == 3: start = False
2nd Mar 2021, 12:48 PM
Nothing
Nothing - avatar
+ 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.
2nd Mar 2021, 12:31 AM
Paul K Sadler
Paul K Sadler - avatar
+ 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😊
2nd Mar 2021, 2:29 AM
Hasan HĂŒseyin Semiz
Hasan HĂŒseyin Semiz - avatar
0
where is the code to correct?
1st Mar 2021, 11:19 PM
iTech
iTech - avatar
0
I have an exam that our teacher gave us
2nd Mar 2021, 1:38 AM
Mamadou Drame
Mamadou Drame - avatar
0
what did u tried so far?
2nd Mar 2021, 2:00 AM
iTech
iTech - avatar
0
Hello ! well I have tried beautiful and there is no rush
2nd Mar 2021, 7:17 AM
Mamadou Drame
Mamadou Drame - avatar
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
2nd Mar 2021, 7:19 AM
Mamadou Drame
Mamadou Drame - avatar
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)
2nd Mar 2021, 1:22 PM
iTech
iTech - avatar
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
3rd Mar 2021, 5:13 AM
Calvin Thomas
Calvin Thomas - avatar