Objective
Develop a Python program that prompts the user for their login and password (both must be integers) and continues to prompt until the correct login "12" and password "1234" are entered. The user will have a maximum of three attempts.
Example Python Exercise
Show Python Code
# Initialize the correct login and password
correct_login = 12
correct_password = 1234
# Initialize the attempt counter
attempts = 0
# Use a while loop to prompt the user for login and password until they are correct or attempts are exhausted
while attempts < 3:
# Prompt the user to enter their login
login = int(input("Please enter your login: "))
# Prompt the user to enter their password
password = int(input("Please enter your password: "))
# Check if the entered login and password are correct
if login == correct_login and password == correct_password:
print("Access granted")
break
else:
print("Incorrect login or password. Please try again.")
attempts += 1
# Check if the maximum number of attempts has been reached
if attempts == 3:
print("Maximum attempts reached. Access denied.")
Output
Case 1:
Please enter your login: 12 Please enter your password: 1234
Access granted
Case 2:
Please enter your login: 11
Please enter your password: 123
Incorrect login or password. Please try again.
Please enter your login: 11
Please enter your password: 123
Incorrect login or password. Please try again.
Please enter your login: 11
Please enter your password: 123
Incorrect login or password. Please try again.
Maximum attempts reached. Access denied.
Share this Python Exercise
Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.
In this exercise, you will develop a Python program that prompts the user for two numbers and displays their division and remainder. If 0 is entered as ...
In this exercise, you will develop a Python program that displays multiplication tables from 2 to 6 using nested "while" loops. The program will iterate throug...
In this exercise, you will develop a Python program that prompts the user to enter a number and a width, and then displays a square of that width using ...
In this exercise, you will develop a Python program that writes the even numbers from 10 to 20, both inclusive, except 16. The program will implement three different methods...
In this Python exercise, you will develop a program that prompts the user to input a number, width, and height and displays a rectangle of the specified...
This Python program allows the user to input two numbers and displays all the numbers between them (inclusive) three times using different types of loops: "for"...