0% found this document useful (0 votes)
11 views3 pages

1

The document contains code snippets for a user authentication system, a math quiz generator, and a maze pathfinding algorithm. It includes functionalities for signing up, logging in, generating math questions based on difficulty, and finding a path in a maze using breadth-first search. Each section demonstrates different programming concepts and structures.

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)
11 views3 pages

1

The document contains code snippets for a user authentication system, a math quiz generator, and a maze pathfinding algorithm. It includes functionalities for signing up, logging in, generating math questions based on difficulty, and finding a path in a maze using breadth-first search. Each section demonstrates different programming concepts and structures.

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/ 3

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

You might also like