Spaces:
Build error
Build error
File size: 4,955 Bytes
8e6f243 5bbe733 8e6f243 96f5111 47de747 5bbe733 96f5111 47de747 96f5111 47de747 cb21542 96f5111 47de747 96f5111 47de747 96f5111 cb21542 47de747 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | import gradio as gr
import random
# ------------------------
# 🎮 1. GUESS THE OUTPUT QUIZ
# ------------------------
quiz_questions = [
{"code": 'print("Hello" + "World")', "options": ["HelloWorld", "Hello World", "Hello+World"], "answer": "HelloWorld", "hint": "What happens when you add two strings?"},
{"code": 'print(2 * 3)', "options": ["6", "23", "5"], "answer": "6", "hint": "Multiplication or joining?"},
{"code": 'x = 5\nprint(x + 2)', "options": ["7", "10", "5"], "answer": "7", "hint": "Adding a number to a variable."},
{"code": 'print(3 + 5 * 2)', "options": ["13", "16", "8"], "answer": "13", "hint": "Order of operations."},
{"code": 'print("Python"[0])', "options": ["P", "Y", "p"], "answer": "P", "hint": "What does the index 0 in a string return?"},
{"code": 'x = [1, 2, 3]\nprint(x[1])', "options": ["2", "3", "1"], "answer": "2", "hint": "Indexing a list."},
{"code": 'print("5" + "5")', "options": ["10", "55", "50"], "answer": "55", "hint": "What happens when you add strings?"}
]
# ------------------------
# 🎮 2. MATH CHALLENGE
# ------------------------
math_questions = [
{"question": "What is 5 + 3?", "options": ["8", "7", "9"], "answer": "8", "hint": "Simple addition."},
{"question": "What is 6 * 4?", "options": ["24", "26", "23"], "answer": "24", "hint": "Multiplication."},
{"question": "What is 9 - 2?", "options": ["7", "6", "5"], "answer": "7", "hint": "Simple subtraction."},
{"question": "What is 12 / 3?", "options": ["3", "4", "5"], "answer": "4", "hint": "Division."},
{"question": "What is 15 % 4?", "options": ["3", "4", "5"], "answer": "3", "hint": "Modulo operation."},
{"question": "What is 8 * 7?", "options": ["56", "54", "58"], "answer": "56", "hint": "Multiplication."},
{"question": "What is 5 + 9?", "options": ["14", "13", "12"], "answer": "14", "hint": "Addition."}
]
# ------------------------
# 🎮 3. PYTHON BASICS QUIZ
# ------------------------
python_basics_questions = [
{"question": "What does 'int' represent in Python?", "options": ["Integer", "Index", "Iterator"], "answer": "Integer", "hint": "Type of number."},
{"question": "What does 'def' keyword do?", "options": ["Defines a function", "Defines a variable", "Defines a class"], "answer": "Defines a function", "hint": "Function definition."},
{"question": "What is a list in Python?", "options": ["A collection of items", "A single item", "An integer"], "answer": "A collection of items", "hint": "Data structure."},
{"question": "What does 'print()' do?", "options": ["Prints a string", "Prints an integer", "Prints a value"], "answer": "Prints a value", "hint": "Output function."},
{"question": "What is the symbol for power in Python?", "options": ["**", "*", "^"], "answer": "**", "hint": "Exponentiation."},
{"question": "What is the use of 'input()'?", "options": ["To take user input", "To print a value", "To define a function"], "answer": "To take user input", "hint": "User interaction."},
{"question": "What is a tuple?", "options": ["Immutable list", "Mutable list", "String"], "answer": "Immutable list", "hint": "Cannot be changed after creation."}
]
# ------------------------
# GAME LOGIC
# ------------------------
# For simplicity, let's use a general function to handle quiz games
def quiz_game(questions, game_type):
score = 0
current_question = random.choice(questions)
question_text = current_question['question'] if 'question' in current_question else current_question['code']
options = current_question['options']
answer = current_question['answer']
hint = current_question['hint']
def check_answer(user_answer):
nonlocal score
if user_answer == answer:
score += 1
return score, f"Hint: {hint} - Next Question"
return gr.Interface(
fn=check_answer,
inputs=[gr.Dropdown(choices=options, label=f"{game_type}: {question_text}")],
outputs=[gr.Textbox(label="Your Score"), gr.Textbox(label="Hint")],
live=True
)
# ------------------------
# GRADIO APP
# ------------------------
# Create a Gradio interface to show multiple games
def launch_game():
game_choices = ["Guess the Output Quiz", "Math Challenge", "Python Basics Quiz"]
game_type = gr.Dropdown(choices=game_choices, label="Select a Game")
game = gr.Button("Start Game")
def start_game(choice):
if choice == "Guess the Output Quiz":
return quiz_game(quiz_questions, choice)
elif choice == "Math Challenge":
return quiz_game(math_questions, choice)
elif choice == "Python Basics Quiz":
return quiz_game(python_basics_questions, choice)
game.click(start_game, inputs=[game_type], outputs=["game_window"])
return gr.Interface(fn=start_game, inputs=[game_type, game], outputs=["game_window"], live=True)
# Launch the Gradio interface
launch_game().launch()
|