diff --git a/26 Dictionaries/exercise-01.py b/26 Dictionaries/exercise-01.py index e69de29..d7f2fef 100644 --- a/26 Dictionaries/exercise-01.py +++ b/26 Dictionaries/exercise-01.py @@ -0,0 +1,27 @@ +animals = { + "cat": "meow", + "dog": "woof", + "bird": "chirp", + "fish": "swim", + "snake": "slither", + "horse": "neigh", +} + +score = [0, 0] # correct, total + +run = True + +while run: + for i in animals: + print(f"What animal goes {animals[i]}?") + answer = input("> ") + + if answer.lower().strip() == i.lower(): + print(f"Correct! {i} goes {animals[i]}.") + score[0] += 1 + score[1] += 1 + print(f"Score: {round(score[0]/score[1]*100)}%") + + if not round(score[0]/score[1]*100) < 60: + run = True + score = [0, 0] \ No newline at end of file diff --git a/27 Nested Dictionary/exercise-01.py b/27 Nested Dictionary/exercise-01.py new file mode 100644 index 0000000..3e26ef5 --- /dev/null +++ b/27 Nested Dictionary/exercise-01.py @@ -0,0 +1,59 @@ +student_names = {} + +# data structure +# student_names = { +# "john": { +# "HW1": 15 +# "HW2": 20 +# }, +# "jane": { +# "HW1": 10 +# "HW2": 15 +# } +# } + +def get_student_name(): + while True: + student_name = input("Enter student name: ").strip().lower() + if student_name == "!break": + return None + elif student_name in student_names: + return student_name + else: + return student_name + +def update_data(student_name, hw, score): + if student_name not in student_names: + student_names[student_name] = {} + + if hw not in student_names[student_name]: + student_names[student_name][hw] = score + else: + print("HW already exists.") + +def display_data(): + for student in student_names: + print(f"{student}:") + for hw in student_names[student]: + print(f" {hw}: {student_names[student][hw]}") + +def main(): + Run = True + + while Run: + student_name = get_student_name() + + if student_name is None: + Run = False + print("Outputting data...") + break + + hw = input("Enter homework name: ").strip().lower() + + score = int(input("Enter score: ")) + update_data(student_name, hw, score) + + display_data() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/27 Nested Dictionary/task.png b/27 Nested Dictionary/task.png new file mode 100644 index 0000000..f13b3cd Binary files /dev/null and b/27 Nested Dictionary/task.png differ diff --git a/28 File handling/exercise-01.py b/28 File handling/exercise-01.py new file mode 100644 index 0000000..3b36e40 --- /dev/null +++ b/28 File handling/exercise-01.py @@ -0,0 +1,37 @@ +students = [ + "john", + "jane", + "bob", + "alice", + "charlie", + "david", + "emily", + "frank", + "grace", + "harry", + "isabella", + "jessica", +] + +with open("students.txt", "w") as f: + for student in students: + f.write(f"{student}\n") + +students = [] + +with open("students.txt", "r") as f: + for line in f: + students.append(line.strip()) + +print("Unsorted: ") +print(students) + +students.sort() +print("") + +print("Sorted: ") +print(students) + +with open("students.txt", "w") as f: + for student in students: + f.write(f"{student}\n") \ No newline at end of file diff --git a/28 File handling/exercise-02.py b/28 File handling/exercise-02.py new file mode 100644 index 0000000..dd3cc7c --- /dev/null +++ b/28 File handling/exercise-02.py @@ -0,0 +1,13 @@ +groceries = [] + +with open("shopping.txt", "r") as f: + for line in f: + groceries.append(line.strip()) + +print(groceries) + +for i in range(3): + item = input("Enter item to add to shopping list: ").strip().lower() + groceries.append(item) + +print(groceries) \ No newline at end of file diff --git a/28 File handling/task.png b/28 File handling/task.png new file mode 100644 index 0000000..4a13482 Binary files /dev/null and b/28 File handling/task.png differ diff --git a/29 Validation/exercise-01.py b/29 Validation/exercise-01.py new file mode 100644 index 0000000..64fd29c --- /dev/null +++ b/29 Validation/exercise-01.py @@ -0,0 +1,42 @@ +def get_score(): + """ asks for test score """ + collect = True + + while collect: + test_score = input("Enter test score: ").strip() + if test_score.isnumeric(): + test_score = int(test_score) + if test_score >= 0 and test_score <= 100: + collect = False + else: + print("Invalid input. Please enter a number between 0 and 100.") + else: + print("Invalid input. Please enter a number.") + + return test_score + +def main(): + """ runs all functions """ + test_score = int(get_score()) + + print("These are the GSCE grade boundaries:") + if test_score >= 90: + print("Your grade is 9") + elif test_score >= 80: + print("Your grade is 8") + elif test_score >= 70: + print("Your grade is 7") + elif test_score >= 60: + print("Your grade is 6") + elif test_score >= 50: + print("Your grade is 5") + elif test_score >= 40: + print("Your grade is 4") + elif test_score >= 30: + print("Your grade is 3") + elif test_score >= 20: + print("Your grade is 2") + else: + print("Your grade is 1") + +main() \ No newline at end of file diff --git a/29 Validation/task.png b/29 Validation/task.png new file mode 100644 index 0000000..994c140 Binary files /dev/null and b/29 Validation/task.png differ diff --git a/30 Authentication/exercise-01.py b/30 Authentication/exercise-01.py new file mode 100644 index 0000000..a1be7bb --- /dev/null +++ b/30 Authentication/exercise-01.py @@ -0,0 +1,32 @@ +# 2d table of users and passwords + +users = [ + ["john", "password"], + ["jane", "1234"], + ["alice", "31234"], + ["charlie", "1863"], + ["david", "3958"], +] + +def login(): + for i in range(3): + username = input("Enter username: ").strip().lower() + password = input("Enter password: ").strip() + + for user in users: + if user[0] == username and user[1] == password: + return True + break + + return False + +def main(): + log = login() + + if log: + print("Login successful!") + else: + print("Invalid username or password.") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/30 Authentication/exercise-02.py b/30 Authentication/exercise-02.py new file mode 100644 index 0000000..e5bb045 --- /dev/null +++ b/30 Authentication/exercise-02.py @@ -0,0 +1,63 @@ +users = { + "john": "password", + "jane": "1234", + "alice": "31234", + "charlie": "1863", + "david": "3958", +} + +def login(): + for i in range(3): + username = input("Enter username: ").strip().lower() + password = input("Enter password: ").strip() + + if username in users and users[username] == password: + return True + + return False + +def add_user(): + username = input("Enter new username: ").strip().lower() + if username in users: + print(f"User '{username}' already exists.") + return + + password = input("Enter password: ").strip() + users[username] = password + print(f"User '{username}' added successfully!") + +def remove_user(): + username = input("Enter username to remove: ").strip().lower() + if username not in users: + print(f"User '{username}' not found.") + return + + del users[username] + print(f"User '{username}' removed successfully!") + +def list_users(): + print("Current users:", list(users.keys())) + +def main(): + while True: + print("\n1. Login\n2. Add user\n3. Remove user\n4. List users\n5. Exit") + choice = input("Enter choice: ").strip() + + if choice == '1': + if login(): + print("Login successful!") + else: + print("Invalid username or password.") + elif choice == '2': + add_user() + elif choice == '3': + remove_user() + elif choice == '4': + list_users() + elif choice == '5': + break + else: + print("Invalid choice.") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/30 Authentication/task.png b/30 Authentication/task.png new file mode 100644 index 0000000..9950d69 Binary files /dev/null and b/30 Authentication/task.png differ