Arslan17121 commited on
Commit
f12d9c2
Β·
verified Β·
1 Parent(s): fabc9ea

Update app.py

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