Python Programming

Python Programming Exercises | Practice Questions

Python Programming Exercises: a login dashboard

Programming Exercise 1

In today’s session, we will try practicing some programming exercises in Python programming. We will take some practice questions; we will write a python code, by creating a list of 5 people called names. then ask the user to input their name. We will save the name in the list. Then as the user to input a password, save the password in another list called password. Then request the user to state their name, check in the list if their name is the list camed names. If it is, we will print, user found. Then ask them for their password, if it is there, tell them it is found and print access granted. If the person’s password is 12345, tell the person that the password is for a stupid person.

# Create a list of 5 people called names
names = ["Alice", "Bob", "Charlie", "David", "Eve"]
passwords = []

# Ask the user to input their name and save it in the list
user_name = input("Enter your name: ")
names.append(user_name)

# Ask the user to input a password and save it in the passwords list
user_password = input("Enter your password: ")
passwords.append(user_password)

# Request the user to state their name
check_name = input("State your name: ")

# Check if the name is in the list
if check_name in names:
    print("User found.")
    # Ask for the password
    check_password = input("Enter your password: ")
    # Check if the password is correct
    if check_password in passwords:
        if check_password == "12345":
            print("Password is for a stupid person.")
        else:
            print("Access granted.")
    else:
        print("Incorrect password.")
else:
    print("User not found.")

The above provided Python code simulates a basic user authentication system. It creates lists to store names and passwords, allows a user to register, and then checks their credentials for login.

The Result

Python Programming Exercises: The result of the program shown in the terminal console

We can also write the python program code as:

#create a list called names
names = ["Alice", "Bob", "Charlie", "David", "Eve"]
#create an empty list to contain the passwords
passwords = []
#ask users for their name and append it to the names
names.append(input('what is your name?'))
#ask user for their password and put in the password
passwords.append(input('And what is your password?'))
#now, check their password
checkUserName = input("What is your name?")
#use a conditional statement to check for the names and the password
if checkUserName in names:
    print(f'Welcome {checkUserName}, your name is found on the list')
    print('kindly input your password')
    checkPassword = input('please verify your password by inputting your password')
    if checkPassword in passwords:
        print(f"Well done {checkUserName}, password correct!")
    elif checkPassword == "1234":
        print("Wrong!, That is a stupid passsword")
    else:
        print("Wrong password")
else:
    print(f'{checkUserName} not found on the list')
    
The program code snippet

The Result

Python Programming Exercises: The result

Notice that we didn’t follow the instructions to the latter. It said that it should print “Access Granted” when the user has passed all the checks. However, ours is printing welcome. TO fix this, we can just go and modify the print syntax to do this.

Lastly, the user inputs are on the same line as the text commands or inquiries themselves. Let us try and fix this. We will try and add a next line or new line syntax to the string text in the input commands.

#create a list called names
names = ["Alice", "Bob", "Charlie", "David", "Eve"]
#create an empty list to contain the passwords
passwords = []
#ask users for their name and append it to the names
names.append(input('what is your name?\n'))
#ask user for their password and put in the password
passwords.append(input('And what is your password?\n'))
#now, check their password
checkUserName = input("Please kindly state your name?")
#use a conditional statement to check for the names and the password
if checkUserName in names:
    print(f'Welcome {checkUserName.title()}, your name is found on the list')
    checkPassword = input('please verify your password by inputting your password\n')
    if checkPassword in passwords:
        print(f"Well done {checkUserName.title()}, password correct, Access granted!")
    elif checkPassword == "1234":
        print("Wrong!, That is a stupid passsword")
    else:
        print("Wrong password")
else:
    print(f'{checkUserName} not found on the list')
Python Programming Exercises: the result

Read Also…

Programming Exercise 2

We will write a python code, by creating a list of that can contain ONLY 5 people called names. Then ask the user to input their name. We will save the name in the list. Then as the user to input a password, save the password in another list called password. Then request the user to state their name, check in the list if their name is the list named names. If it is, we will print, user found. Then ask them for their password, if it is there, tell them it is found and print access granted. If the person’s password is 12345 or password, tell the person that the password is for a stupid person.

smartechlabs

Recent Posts

Signs You’re Growing Apart: How to Reconnect with Your Partner

Signs You’re Growing Apart Introduction Every relationship goes through its ups and downs, but sometimes,…

16 hours ago

Capital Punishment: A Debate as Old as Time

Capital punishment, a phrase that sends shivers down the spines of some and a sense…

17 hours ago

A World on the Brink: A Look at Katie May’s “Burning Embers”

Burning Embers Introduction Katie May's "Burning Embers" transports readers to a seemingly ordinary town harboring…

18 hours ago

The Power of the Cross: How Jesus’ Sacrifice Transforms Lives

The story of Jesus Christ is not just a tale of a man who lived…

18 hours ago

The Impact of Social Media on Relationships: Tips for Healthy Boundaries

The Impact of Social Media on Relationships Introduction In today’s digital age, social media is…

18 hours ago

The Top 10 Earbuds for Sports in 2024

When it comes to sports, having the right earbuds can make a significant difference in…

20 hours ago