RICHERGIRL commited on
Commit
c87a8cd
·
verified ·
1 Parent(s): 47de747

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -66
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import random
3
 
4
  # ------------------------
5
- # 🎮 1. GUESS THE OUTPUT QUIZ
6
  # ------------------------
7
 
8
  quiz_questions = [
@@ -16,79 +16,73 @@ quiz_questions = [
16
  ]
17
 
18
  # ------------------------
19
- # 🎮 2. MATH CHALLENGE
20
  # ------------------------
21
 
22
- math_questions = [
23
- {"question": "What is 5 + 3?", "options": ["8", "7", "9"], "answer": "8", "hint": "Simple addition."},
24
- {"question": "What is 6 * 4?", "options": ["24", "26", "23"], "answer": "24", "hint": "Multiplication."},
25
- {"question": "What is 9 - 2?", "options": ["7", "6", "5"], "answer": "7", "hint": "Simple subtraction."},
26
- {"question": "What is 12 / 3?", "options": ["3", "4", "5"], "answer": "4", "hint": "Division."},
27
- {"question": "What is 15 % 4?", "options": ["3", "4", "5"], "answer": "3", "hint": "Modulo operation."},
28
- {"question": "What is 8 * 7?", "options": ["56", "54", "58"], "answer": "56", "hint": "Multiplication."},
29
- {"question": "What is 5 + 9?", "options": ["14", "13", "12"], "answer": "14", "hint": "Addition."}
30
- ]
31
 
32
- # ------------------------
33
- # 🎮 3. PYTHON BASICS QUIZ
34
- # ------------------------
 
 
 
 
 
 
35
 
36
- python_basics_questions = [
37
- {"question": "What does 'int' represent in Python?", "options": ["Integer", "Index", "Iterator"], "answer": "Integer", "hint": "Type of number."},
38
- {"question": "What does 'def' keyword do?", "options": ["Defines a function", "Defines a variable", "Defines a class"], "answer": "Defines a function", "hint": "Function definition."},
39
- {"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."},
40
- {"question": "What does 'print()' do?", "options": ["Prints a string", "Prints an integer", "Prints a value"], "answer": "Prints a value", "hint": "Output function."},
41
- {"question": "What is the symbol for power in Python?", "options": ["**", "*", "^"], "answer": "**", "hint": "Exponentiation."},
42
- {"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."},
43
- {"question": "What is a tuple?", "options": ["Immutable list", "Mutable list", "String"], "answer": "Immutable list", "hint": "Cannot be changed after creation."}
44
- ]
45
 
46
- # ------------------------
47
- # GAME LOGIC
48
- # ------------------------
49
 
50
- # For simplicity, let's use a general function to handle quiz games
51
- def quiz_game(questions, game_type):
52
- score = 0
53
- current_question = random.choice(questions)
54
- question_text = current_question['question'] if 'question' in current_question else current_question['code']
55
- options = current_question['options']
56
- answer = current_question['answer']
57
- hint = current_question['hint']
58
-
59
- def check_answer(user_answer):
60
- nonlocal score
61
- if user_answer == answer:
62
- score += 1
63
- return score, f"Hint: {hint} - Next Question"
64
-
65
- return gr.Interface(
66
- fn=check_answer,
67
- inputs=[gr.Dropdown(choices=options, label=f"{game_type}: {question_text}")],
68
- outputs=[gr.Textbox(label="Your Score"), gr.Textbox(label="Hint")],
69
- live=True
70
  )
71
 
72
  # ------------------------
73
- # GRADIO APP
74
  # ------------------------
75
 
76
- # Create a Gradio interface to show multiple games
77
- def launch_game():
78
- game_choices = ["Guess the Output Quiz", "Math Challenge", "Python Basics Quiz"]
79
- game_type = gr.Dropdown(choices=game_choices, label="Select a Game")
80
- game = gr.Button("Start Game")
81
-
82
- def start_game(choice):
83
- if choice == "Guess the Output Quiz":
84
- return quiz_game(quiz_questions, choice)
85
- elif choice == "Math Challenge":
86
- return quiz_game(math_questions, choice)
87
- elif choice == "Python Basics Quiz":
88
- return quiz_game(python_basics_questions, choice)
89
-
90
- game.click(start_game, inputs=[game_type], outputs=["game_window"])
91
- return gr.Interface(fn=start_game, inputs=[game_type, game], outputs=["game_window"], live=True)
92
-
93
- # Launch the Gradio interface
94
- launch_game().launch()
 
 
 
 
 
 
 
 
 
2
  import random
3
 
4
  # ------------------------
5
+ # 🎮 GUESS THE OUTPUT QUIZ
6
  # ------------------------
7
 
8
  quiz_questions = [
 
16
  ]
17
 
18
  # ------------------------
19
+ # GAME LOGIC
20
  # ------------------------
21
 
22
+ def get_new_question():
23
+ q = random.choice(quiz_questions)
24
+ return q
 
 
 
 
 
 
25
 
26
+ def start_game():
27
+ question = get_new_question()
28
+ return (
29
+ question["code"],
30
+ gr.update(choices=question["options"], value=None),
31
+ "", "", # Clear result and hint
32
+ question, # Save question in state
33
+ 0 # Initial score
34
+ )
35
 
36
+ def submit_answer(user_answer, current_question, score):
37
+ if user_answer == current_question["answer"]:
38
+ score += 1
39
+ result = f" Correct! Your score is now {score}"
40
+ else:
41
+ result = f" Wrong! The correct answer was '{current_question['answer']}'"
 
 
 
42
 
43
+ # Show hint
44
+ hint = f"💡 Hint: {current_question['hint']}"
 
45
 
46
+ # Load next question
47
+ next_question = get_new_question()
48
+
49
+ return (
50
+ next_question["code"],
51
+ gr.update(choices=next_question["options"], value=None),
52
+ result,
53
+ hint,
54
+ next_question,
55
+ score
 
 
 
 
 
 
 
 
 
 
56
  )
57
 
58
  # ------------------------
59
+ # GRADIO UI
60
  # ------------------------
61
 
62
+ with gr.Blocks() as demo:
63
+ gr.Markdown("## 🧠 Learn Python Through Games - Guess the Output Edition")
64
+
65
+ start_btn = gr.Button("🎲 Start Quiz")
66
+
67
+ question_display = gr.Textbox(label="🧩 Code Snippet", interactive=False)
68
+ options_dropdown = gr.Dropdown(choices=[], label="Choose the Output")
69
+ submit_btn = gr.Button("Submit Answer")
70
+
71
+ result_text = gr.Textbox(label="Result", interactive=False)
72
+ hint_text = gr.Textbox(label="Hint", interactive=False)
73
+
74
+ # Hidden states to store session data
75
+ question_state = gr.State()
76
+ score_state = gr.State()
77
+
78
+ # Link start button to load first question
79
+ start_btn.click(start_game,
80
+ inputs=[],
81
+ outputs=[question_display, options_dropdown, result_text, hint_text, question_state, score_state])
82
+
83
+ # Link submit button to check answer and load next
84
+ submit_btn.click(submit_answer,
85
+ inputs=[options_dropdown, question_state, score_state],
86
+ outputs=[question_display, options_dropdown, result_text, hint_text, question_state, score_state])
87
+
88
+ demo.launch()