Access Code V2 - Python Programming Exercise

In this exercise, you will develop a Python program that prompts the user for their login and password, both of which must be integers. The program will continue to prompt the user until the correct login ("12") and password ("1234") are entered, but the user will have a maximum of three attempts. This task will help you practice using loops and conditional statements to limit the number of user input attempts. By completing this exercise, you will improve your skills in managing input attempts and handling user authentication in Python programs. You'll also strengthen your understanding of how to implement restrictions and enforce rules, such as limiting the number of login attempts before the program stops prompting the user.

 Category

Mastering Flow Control

 Exercise

Access Code V2

 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

 Copy 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

 More Python Programming Exercises of Mastering Flow Control

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.

  •  Multiple Divisions

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

  •  Multiple Times Tables (Using While)

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

  •  Quadrilateral

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

  •  Pause & Proceed

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

  •  Quadrilateral V2

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

  •  Iterative Patterns

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