86 lines
3.1 KiB
Python
86 lines
3.1 KiB
Python
import random
|
|
import re
|
|
import os
|
|
|
|
TERMS = {
|
|
"ALGORITHM": "A set of instructions to solve a problem",
|
|
"MALWARE": "Software designed to cause harm to a system",
|
|
"PHISHING": "Fraudulent attempt to obtain sensitive information",
|
|
"SQL": "Language used to communicate with databases",
|
|
"ENCRYPTION": "Process of converting data to prevent unauthorized access",
|
|
"SERVER": "A computer that provides data to other computers",
|
|
"TESTING": "Checking code to ensure it meets requirements without errors"
|
|
}
|
|
|
|
def auth():
|
|
choice = input("Enter 'r' to register or 'l' to login: ").lower()
|
|
if choice == 'r':
|
|
name = input("Enter full name: ")
|
|
while True:
|
|
user = input("Choose username (4-8 characters): ")
|
|
if 4 <= len(user) <= 8: break
|
|
print("Username must be between 4 and 8 characters.")
|
|
while True:
|
|
pwd = input("Choose password (requires uppercase, lowercase, and a number): ")
|
|
if re.search(r'[A-Z]', pwd) and re.search(r'[a-z]', pwd) and re.search(r'\d', pwd): break
|
|
print("Password does not meet complexity requirements.")
|
|
|
|
with open("students.txt", "a") as f:
|
|
f.write(f"{user},{pwd},{name}\n")
|
|
return user
|
|
else:
|
|
u, p = input("Username: "), input("Password: ")
|
|
try:
|
|
with open("students.txt", "r") as f:
|
|
if any(line.strip().startswith(f"{u},{p},") for line in f): return u
|
|
except FileNotFoundError: pass
|
|
print("Login failed or user does not exist."); return None
|
|
|
|
def quiz(user):
|
|
keyword, correct_def = random.choice(list(TERMS.items()))
|
|
options = [correct_def] + random.sample([v for k, v in TERMS.items() if k != keyword], min(3, len(TERMS)-1))
|
|
random.shuffle(options)
|
|
|
|
print(f"\nKeyword: {keyword}")
|
|
for i, opt in enumerate(options, 1):
|
|
print(f"{i}. {opt}")
|
|
|
|
score = 0
|
|
for attempt, points in [(1, 3), (2, 1)]:
|
|
try:
|
|
ans = int(input(f"Attempt {attempt} - Choose 1-{len(options)}: "))
|
|
if options[ans-1] == correct_def:
|
|
print("Correct!")
|
|
score = points
|
|
break
|
|
print("Incorrect!")
|
|
except (ValueError, IndexError):
|
|
print("Invalid choice.")
|
|
else:
|
|
print(f"The correct definition was: {correct_def}")
|
|
|
|
with open("scores.txt", "a") as f:
|
|
f.write(f"{user},{score}\n")
|
|
|
|
def main():
|
|
user = None
|
|
while not user:
|
|
user = auth()
|
|
|
|
while True:
|
|
action = input("\nChoose: [1] Try Quiz [2] View Scores [3] Quit: ")
|
|
if action == '1':
|
|
quiz(user)
|
|
elif action == '2':
|
|
try:
|
|
with open("scores.txt", "r") as f:
|
|
scores = [l.strip().split(',')[1] for l in f if l.startswith(f"{user},")]
|
|
print("Your previous scores:" if scores else "No scores yet.")
|
|
for s in scores: print(f"- {s} points")
|
|
except FileNotFoundError:
|
|
print("No scores yet.")
|
|
elif action == '3':
|
|
break
|
|
|
|
if __name__ == "__main__":
|
|
main() |