String Password - Python Programming Exercise

This Python program prompts the user for a username and a password (both should be strings) and repeats the process until the correct credentials are entered. The program is designed to ask the user for their input and validate that both the username and password match predefined values, which are "username" and "password" respectively. It uses a while loop to continuously prompt the user until the correct input is provided, ensuring that the program only proceeds when the correct credentials are entered. This exercise demonstrates the use of input validation and loops in Python to control program flow based on user input. By utilizing a while loop, the program keeps asking for the username and password until the conditions are satisfied, which is a practical way to ensure secure access to certain parts of an application. This approach also offers a clear example of managing user input and implementing basic authentication logic within a Python program.

 Category

Python Data Types

 Exercise

String Password

 Objective

Develop a Python program to prompt the user for their username and password (both should be strings) and repeat the prompt as many times as necessary until the entered name is "username" and the password is "password".

 Example Python Exercise

 Copy Python Code
# Prompt the user for their username and password and keep asking until correct
while True:
    username = input("Enter your username: ")
    password = input("Enter your password: ")

    # Check if the entered username and password are correct
    if username == "username" and password == "password":
        print("Access granted")
        break
    else:
        print("Invalid username or password. Please try again.")

 Output

Enter your username: admin
Enter your password: 1234
Invalid username or password. Please try again.
Enter your username: username
Enter your password: 1234
Invalid username or password. Please try again.
Enter your username: username
Enter your password: password
Access granted

 Share this Python Exercise

 More Python Programming Exercises of Python Data Types

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.

  •  Arithmetic - Conditional

    This Python program prompts the user for two numbers and an operation to perform on them, such as addition (+), subtraction (-), multiplication (*), or divisio...

  •  Double Value

    This Python program calculates the perimeter, area, and diagonal of a rectangle, based on the given width and height. The perimeter is cal...

  •  Evaluate Function Outputs

    This Python program displays the values of the function y = x² - 2x + 1 for integer values of x ranging from -10 to +10. The function is a quadratic equation, ...

  •  Show Function

    This Python program "draws" the graph of the function y = (x-4)² by displaying a series of asterisks on the screen. For each x value ranging from -1 to 8, the ...

  •  Floating Point, Speed Units

    This Python program prompts the user to input a distance in meters and the time taken in hours, minutes, and seconds, and then calculates the speed in three different units:...

  •  Floating Point Sphere

    This Python program calculates both the surface area and volume of a sphere based on its radius. It uses the formulas for surface area (4 * pi * radius squared) and v...