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

AI-Powered Mycoprotein: Optimizing Conditions for Growing Sustainable Protein from Fungi.

Introduction: Can Fungi Really Feed the Future? Mycoprotein When people first hear about protein made…

3 weeks ago

Creating a Peer-to-Piece Tech Tool Library for a Neighborhood or Community.

Introduction Imagine this: your neighbor needs a 3D printer for a weekend project, while you’ve…

3 weeks ago

Becoming a “Sensor Network” Installer for Small Farms or Environmental Monitoring.

Introduction Imagine walking onto a small farm or a nature reserve, where tiny devices quietly…

3 weeks ago

The Personalized Children’s Book Creator: Using AI to Generate Stories Featuring the Child as the Hero.

Introduction Imagine handing your child an Interactive Personalized Storybook where they are the hero of…

3 weeks ago

Offering a “Disaster Recovery Drill” Service to Test a Company’s backups and restore processes.

Introduction Imagine waking up one morning to find your company’s entire IT infrastructure compromised—servers down,…

3 weeks ago

AI Tools for Creating Courses & Tutorials

Introduction: Can AI Really Help You Create a Course? Let’s start with the question almost…

3 weeks ago

This website uses cookies.