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
Show 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.
This Python program prompts the user for two numbers and an operation to perform on them, such as addition (+), subtraction (-), multiplication (*), or divisio...
This Python program calculates the perimeter, area, and diagonal of a rectangle, based on the given width and height. The perimeter is cal...
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, ...
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 ...
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:...
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...