0% found this document useful (0 votes)
3 views2 pages

123

The document contains a Python program that implements a user authentication system with sign-up and login functionalities. It also includes a math quiz generator that creates questions based on user-defined difficulty levels and evaluates answers. Additionally, there is a maze-solving algorithm using breadth-first search (BFS) to find a path from an entrance to an exit in a given maze.

Uploaded by

thyra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

123

The document contains a Python program that implements a user authentication system with sign-up and login functionalities. It also includes a math quiz generator that creates questions based on user-defined difficulty levels and evaluates answers. Additionally, there is a maze-solving algorithm using breadth-first search (BFS) to find a path from an entrance to an exit in a given maze.

Uploaded by

thyra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

4.. cred = {} print("Password incorrect.

")
while True: print()
print("1. Sign up") elif choice == 3:
print("2. Login") break
print("3. Quit") else:
choice = int(input("Enter Your Choice: ")) print("Invalid choice. Please try again.")
if choice == 1:
key = str(input("Enter the User Name: ")) 5… import random
if key in cred: class MathQuizGenerator:
print() def __init__(self):
print("Username already exists.") self.difficulty_ranges = {
print() 'easy': (1, 10),
else: 'medium': (10, 50),
cred[key] = str(input("Enter the password: ")) 'hard': (50, 100)
print() }
print("Sign up successful!") self.opera ons = {
print() '+': self.add,
elif choice == 2: '-': self.subtract,
key = str(input("Enter the User Name: ")) '*': self.mul ply,
if key not in cred: '/': self.divide
print() }
print("User not found.") def add(self, num1, num2):
print() return num1 + num2
else: def subtract(self, num1, num2):
password = str(input("Enter password: ")) return num1 - num2
if cred[key] == password: def mul ply(self, num1, num2):
print() return num1 * num2
print("Welcome!") def divide(self, num1, num2):
print() return num1 / num2 if num2 != 0 else None
else: def generate_ques on(self, difficulty):
print() num1 = random.randint(*self.difficulty_ranges[difficulty])

num2 = random.randint(*self.difficulty_ranges[difficulty]) 6.. from collec ons import deque


opera on = random.choice(list(self.opera ons.keys())) def is_valid(maze, x, y):
# Special handling for division to ensure no division by zero return 0 <= x < len(maze) and 0 <= y < len(maze[0]) and maze[x][y] != 1
if opera on == '/': def bfs(maze, entrance, exit):
num1 *= num2 direc ons = [(0, 1), (0, -1), (1, 0), (-1, 0)] # right, le , down, up
ques on = f"{num1} {opera on} {num2}" queue = deque([(entrance, [entrance])])
answer = self.opera ons[opera on](num1, num2) visited = set([entrance])
return ques on, answer while queue:
def start_quiz(self, num_ques ons=5, difficulty='easy'): (x, y), path = queue.pople ()
score = 0 if (x, y) == exit:
for _ in range(num_ques ons): return path
ques on, answer = self.generate_ques on(difficulty) for dx, dy in direc ons:
user_answer = input(f"What is {ques on}? ") nx, ny = x + dx, y + dy
# Simple input valida on if is_valid(maze, nx, ny) and (nx, ny) not in visited:
try: queue.append(((nx, ny), path + [(nx, ny)]))
if float(user_answer) == answer: visited.add((nx, ny))
print("Correct!") return None
score += 1 def get_coordinates(prompt):
else: while True:
print(f"Incorrect! The correct answer is {answer:.2f}") try:
except ValueError: x, y = map(int, input(prompt).split(','))
print("Invalid input. Please enter a number.") return (x, y)
print(f"Your score: {score}/{num_ques ons}") except ValueError:
if __name__ == "__main__": print("Invalid input. Please enter coordinates as 'x,y'.")
print("Welcome to the Math Quiz!") def main():
difficulty = input("Choose difficulty (easy, medium, hard): ").lower() maze = [
num_ques ons = 5 [0, 0, 1, 0, 0],
quiz = MathQuizGenerator() [0, 0, 1, 0, 0],
quiz.start_quiz(num_ques ons, difficulty) [0, 0, 0, 0, 1],
[1, 1, 1, 0, 0],
[0, 0, 0, 0, 0]
]
print("Maze:")
for row in maze:
print(row)
entrance = get_coordinates("Enter entrance coordinates (x,y): ")
exit = get_coordinates("Enter exit coordinates (x,y): ")
path = bfs(maze, entrance, exit)
if path:
print("Path found:", path)
else:
print("No path found")
if __name__ == "__main__":
main()

You might also like