11/20/2025
University of Science, VNU – HCM
Faculty of Physics and Engineering
Department of Physics and Computer Science
Engineering
Programming
MSc. Vo Hoang Thuy Tien
List, Tuple, Dictionaries
Week 5
1
11/20/2025
List, Tuple, Dictionaries
Exercises 1
Register and Login to an Account
* Description:
• The program has been updated to allow users to register and log in to three platforms:
Facebook, Instagram, and TikTok. Users can select a platform before performing registration
or login.
List, Tuple, Dictionaries
Exercises 1
Register and Login to an Account
* Features of the Code:
Registration:
o Ensures the username is at least 6 characters long.
o Ensures the password meets security criteria.
o Checks if the username already exists before registering.
Login:
o Validates the username and password against stored accounts.
o Allows retrying login if credentials are incorrect.
Menu System:
o Allows users to register, log in, or exit.
o Prevents login attempts if no accounts are registered.
2
11/20/2025
List, Tuple, Dictionaries
Exercises 1
DEFINE FUNCTION validate_password(password):
IF length(password) < 6:
RETURN False
DEFINE FUNCTION validate_username(username):
Initialize has_digit = False
IF length(username) >= 6:
Initialize has_upper = False
RETURN True Initialize has_special = False
ELSE: Define special_characters as "!@#$%^&*(),.?\":{}|<>"
RETURN False
FOR each character in password:
END IF
IF character is a digit:
END FUNCTION
has_digit = True
IF character is an uppercase letter:
has_upper = True
IF character is in special_characters:
has_special = True
RETURN has_digit AND has_upper AND has_special
END FUNCTION
DEFINE FUNCTION register(accounts, platform)
PRINT "Register an Account for " + platform
WHILE TRUE DO
PRINT "Enter a username (at least 6 characters): "
READ username
IF NOT validate_username(username) THEN
PRINT "Username must be at least 6 characters long."
CONTINUE
END IF
FOR EACH account IN accounts
IF [Link] = username AND [Link] = platform THEN
PRINT "Username already exists on this platform. Try another."
CONTINUE WHILE LOOP
END IF
END FOR
WHILE TRUE DO
PRINT "Enter a password (at least 6 characters, 1 digit, 1 uppercase letter, 1 special character):"
READ password
IF validate_password(password) THEN
ADD {"username": username, "password": password, "platform": platform} TO accounts
PRINT "Account registered successfully for " + platform + "!"
RETURN
ELSE
PRINT "Password must meet security requirements."
END IF
END WHILE
END WHILE
END FUNCTION
6
3
11/20/2025
DEFINE FUNCTION login(accounts, platform)
PRINT "Login to Your " + platform + " Account"
WHILE TRUE DO
PRINT "Enter your username: "
READ username
PRINT "Enter your password: "
READ password
FOR EACH account IN accounts
IF [Link] = username AND [Link] = password AND [Link] = platform THEN
PRINT "Login successful for " + platform + "!"
RETURN
END IF
END FOR
PRINT "Incorrect username or password! Try again."
END WHILE
END FUNCTION
IF choice IN ["1", "2", "3"] THEN
SET platform TO platforms[choice - 1]
PRINT "Choose an option for " + platform + ":"
PRINT "1. Register"
PRINT "2. Login"
PRINT "Enter your choice: "
READ sub_choice
DEFINE FUNCTION main()
SET accounts TO EMPTY LIST IF sub_choice = "1" THEN
SET platforms TO ["Facebook", "Instagram", "TikTok"] CALL register(accounts, platform)
ELSE IF sub_choice = "2" THEN
WHILE TRUE DO IF LENGTH(accounts) > 0 THEN
PRINT "Choose a platform:" CALL login(accounts, platform)
FOR i FROM 1 TO LENGTH(platforms) DO ELSE
PRINT i + ". " + platforms[i - 1] PRINT "No registered accounts. Please register first."
END FOR END IF
PRINT "4. Exit" ELSE
PRINT "Invalid choice. Please enter 1 or 2."
PRINT "Enter your choice: " END IF
READ choice ELSE IF choice = "4" THEN
PRINT "Exiting program."
BREAK
ELSE
PRINT "Invalid choice. Please enter a valid option."
END IF
END WHILE
END FUNCTION
4
11/20/2025
List, Tuple, Dictionaries
Exercises 2
Hangman Game
* Description:
In this exercise, we'll write a Python program that allows users to play the classic hangman
game.
* Task Details:
1. The program will choose a random word from a predefined list of words.
2. It will display a series of dashes representing the letters in the chosen word.
3. The player will have a limited number of attempts to guess the letters in the word.
4. After each guess, the program will reveal any correctly guessed letters and update the
display.
5. The game will continue until the player either guesses the word correctly or runs out of
attempts.
Lists, Tuples, Dictionaries
Exercises 2
import random
def choose_word():
words = ["apple", "banana", "cherry", "orange", "pear", "grape", "kiwi"]
return [Link](words)
def display_word(word, guessed_letters):
display = "“
...
return [Link]()
def hangman():
word = choose_word()
guessed_letters = []
attempts = 6
print("Welcome to Hangman!")
print("Try to guess the word.")
while attempts > 0:
...
if attempts == 0:
print("\nSorry, you've run out of attempts. The word was:", word)
10
10
5
11/20/2025
THANK YOU!
MSc. Vo Hoang Thuy Tien
Lecturer and Researcher
+84 937 64 99 14
vhttien@[Link]
University of Science, VNU – HCM
227 Nguyen Van Cu, Ward 4, District 5, Ho Chi Minh City
11