Arslan17121 commited on
Commit
f4ce3d9
ยท
verified ยท
1 Parent(s): 304cf94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -17
app.py CHANGED
@@ -4,34 +4,38 @@ import random
4
  # Global variables
5
  number = 0
6
  mode = ""
 
7
 
8
  def main_page():
9
  st.title("Before and After Game")
10
  st.write("Welcome to the Before and After Game! Click 'Play' to get started.")
11
 
12
  if st.button("Play", key="play_button"):
13
- select_mode()
 
14
 
15
  def select_mode():
16
  st.title("Choose a Mode")
17
  st.write("Select 'Before' to find the number before a given number or 'After' to find the number after it.")
18
 
19
  col1, col2 = st.columns(2)
 
20
  with col1:
21
  if st.button("Before", key="before_button"):
22
- start_game("before")
 
23
  with col2:
24
  if st.button("After", key="after_button"):
25
- start_game("after")
 
26
 
27
- def start_game(selected_mode):
28
- global mode, number
29
- mode = selected_mode
30
  number = random.randint(1, 100)
31
  ask_question()
32
 
33
  def ask_question():
34
- global number, mode
35
  st.title(f"What is {mode.upper()} {number}?")
36
  st.write("Choose the correct answer from the options below.")
37
 
@@ -50,20 +54,31 @@ def ask_question():
50
  if i % 2 == 0:
51
  with col1:
52
  if st.button(str(option), key=f"option_{option}"):
53
- check_answer(option, correct_answer)
 
 
 
 
 
54
  else:
55
  with col2:
56
  if st.button(str(option), key=f"option_{option}"):
57
- check_answer(option, correct_answer)
 
 
 
 
 
58
 
59
- def check_answer(selected, correct):
60
- if selected == correct:
61
- st.success("Great Job! ๐ŸŽ‰")
62
- st.balloons()
63
- st.button("Next Question", on_click=ask_question)
64
- else:
65
- st.error("Oops! Try again!")
 
66
 
67
  # Run the application
68
  if __name__ == "__main__":
69
- main_page()
 
4
  # Global variables
5
  number = 0
6
  mode = ""
7
+ current_page = "main"
8
 
9
  def main_page():
10
  st.title("Before and After Game")
11
  st.write("Welcome to the Before and After Game! Click 'Play' to get started.")
12
 
13
  if st.button("Play", key="play_button"):
14
+ global current_page
15
+ current_page = "select_mode"
16
 
17
  def select_mode():
18
  st.title("Choose a Mode")
19
  st.write("Select 'Before' to find the number before a given number or 'After' to find the number after it.")
20
 
21
  col1, col2 = st.columns(2)
22
+ global current_page, mode
23
  with col1:
24
  if st.button("Before", key="before_button"):
25
+ mode = "before"
26
+ current_page = "game"
27
  with col2:
28
  if st.button("After", key="after_button"):
29
+ mode = "after"
30
+ current_page = "game"
31
 
32
+ def start_game():
33
+ global number
 
34
  number = random.randint(1, 100)
35
  ask_question()
36
 
37
  def ask_question():
38
+ global number, mode, current_page
39
  st.title(f"What is {mode.upper()} {number}?")
40
  st.write("Choose the correct answer from the options below.")
41
 
 
54
  if i % 2 == 0:
55
  with col1:
56
  if st.button(str(option), key=f"option_{option}"):
57
+ if option == correct_answer:
58
+ st.success("Great Job! ๐ŸŽ‰")
59
+ st.balloons()
60
+ current_page = "game"
61
+ else:
62
+ st.error("Oops! Try again!")
63
  else:
64
  with col2:
65
  if st.button(str(option), key=f"option_{option}"):
66
+ if option == correct_answer:
67
+ st.success("Great Job! ๐ŸŽ‰")
68
+ st.balloons()
69
+ current_page = "game"
70
+ else:
71
+ st.error("Oops! Try again!")
72
 
73
+ def app():
74
+ global current_page
75
+ if current_page == "main":
76
+ main_page()
77
+ elif current_page == "select_mode":
78
+ select_mode()
79
+ elif current_page == "game":
80
+ start_game()
81
 
82
  # Run the application
83
  if __name__ == "__main__":
84
+ app()