Complete 26-30
This commit is contained in:
parent
9f435ffa79
commit
7c6b44f414
@ -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]
|
||||||
59
27 Nested Dictionary/exercise-01.py
Normal file
59
27 Nested Dictionary/exercise-01.py
Normal file
@ -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()
|
||||||
BIN
27 Nested Dictionary/task.png
Normal file
BIN
27 Nested Dictionary/task.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
37
28 File handling/exercise-01.py
Normal file
37
28 File handling/exercise-01.py
Normal file
@ -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")
|
||||||
13
28 File handling/exercise-02.py
Normal file
13
28 File handling/exercise-02.py
Normal file
@ -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)
|
||||||
BIN
28 File handling/task.png
Normal file
BIN
28 File handling/task.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 66 KiB |
42
29 Validation/exercise-01.py
Normal file
42
29 Validation/exercise-01.py
Normal file
@ -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()
|
||||||
BIN
29 Validation/task.png
Normal file
BIN
29 Validation/task.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 119 KiB |
32
30 Authentication/exercise-01.py
Normal file
32
30 Authentication/exercise-01.py
Normal file
@ -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()
|
||||||
63
30 Authentication/exercise-02.py
Normal file
63
30 Authentication/exercise-02.py
Normal file
@ -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()
|
||||||
BIN
30 Authentication/task.png
Normal file
BIN
30 Authentication/task.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 75 KiB |
Loading…
Reference in New Issue
Block a user