Access Code - Python Programming Exercise

In this exercise, you will develop a Python program that prompts the user to enter their login and password (both must be integer numbers). The program will repeat the prompt as many times as necessary until the entered login is "12" and the password is "1234". This task will help you practice using while loops and conditional statements to validate user input in a Python program. By completing this exercise, you will enhance your ability to handle input validation in Python and ensure that the user provides the correct login and password before continuing. This is an essential skill for developing interactive programs that require secure or specific user authentication in real-world applications.

 Category

Mastering Flow Control

 Exercise

Access Code

 Objective

Develop a Python program that prompts the user to enter their login and password (both must be integer numbers) and repeats the prompt as many times as necessary until the entered login is "12" and the password is "1234".

 Example Python Exercise

 Copy Python Code
# Initialize the correct login and password
correct_login = 12
correct_password = 1234

# Use a while loop to prompt the user for login and password until they are correct
while True:
    # 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.")

 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.

 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.

  •  Access Code V2

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

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