SanaAdeel commited on
Commit
e703d58
·
verified ·
1 Parent(s): 63ff4f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -39
app.py CHANGED
@@ -1,47 +1,85 @@
1
  import streamlit as st
2
 
3
- def guess_the_thing():
4
- st.title("Guess the Thing Game")
5
- st.write("Think of something, and I'll try to guess it in 10 questions!")
6
-
7
- questions = [
8
- "Is it alive?",
9
- "Can it be found indoors?",
10
- "Is it bigger than a car?",
11
- "Is it a specific color?",
12
- "Is it man-made?",
13
- "Is it something you use daily?",
14
- "Is it electronic?",
15
- "Can you hold it in your hand?",
16
- "Does it have moving parts?",
17
- "Is it used for entertainment?"
 
 
 
 
 
 
 
18
  ]
 
19
 
20
- if "current_question" not in st.session_state:
21
- st.session_state.current_question = 0
22
- st.session_state.answers = []
 
 
 
23
 
24
- if st.session_state.current_question < len(questions):
25
- question = questions[st.session_state.current_question]
26
- st.subheader(f"Question {st.session_state.current_question + 1}: {question}")
 
27
 
28
- if st.button("Yes"):
29
- st.session_state.answers.append("Yes")
30
- st.session_state.current_question += 1
 
 
 
 
 
 
 
 
 
31
 
32
- if st.button("No"):
33
- st.session_state.answers.append("No")
34
- st.session_state.current_question += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  else:
36
- st.subheader("Here are your answers:")
37
- for i, (q, a) in enumerate(zip(questions, st.session_state.answers), 1):
38
- st.write(f"{i}. {q} - {a}")
39
- st.write("Thanks for playing!")
40
-
41
- if st.button("Play Again"):
42
- st.session_state.current_question = 0
43
- st.session_state.answers = []
44
-
45
- # Run the game
46
- if _name_ == "_main_":
47
- guess_the_thing()
 
1
  import streamlit as st
2
 
3
+ # List of objects and related questions (example)
4
+ objects_and_questions = {
5
+ "Cat": [
6
+ "Is it alive?",
7
+ "Is it a pet?",
8
+ "Does it have fur?",
9
+ "Is it a small animal?",
10
+ "Does it meow?"
11
+ ],
12
+ "Car": [
13
+ "Is it something that moves on wheels?",
14
+ "Can it be driven?",
15
+ "Is it used for transportation?",
16
+ "Does it have an engine?",
17
+ "Is it a four-wheeled vehicle?"
18
+ ],
19
+ "Banana": [
20
+ "Is it a fruit?",
21
+ "Is it yellow?",
22
+ "Does it grow on trees?",
23
+ "Is it edible?",
24
+ "Is it sweet?"
25
  ]
26
+ }
27
 
28
+ # Function to start the game
29
+ def start_game():
30
+ st.title("Think of a Thing and I'll Try to Guess It!")
31
+
32
+ # Initial instructions
33
+ st.write("Think of something, and I will try to guess it by asking you up to 10 yes/no questions.")
34
 
35
+ # Initialize game state
36
+ questions_asked = 0
37
+ possible_objects = list(objects_and_questions.keys()) # Start with all objects
38
+ current_questions = {} # Dict to track questions for each object
39
 
40
+ while questions_asked < 10 and len(possible_objects) > 1:
41
+ question = None
42
+ for obj in possible_objects:
43
+ if obj not in current_questions:
44
+ current_questions[obj] = 0 # Start with the first question for this object
45
+ if current_questions[obj] < len(objects_and_questions[obj]):
46
+ # Get the next unanswered question for the object
47
+ question = objects_and_questions[obj][current_questions[obj]]
48
+ break
49
+
50
+ if not question:
51
+ break
52
 
53
+ # Ask the question
54
+ answer = st.radio(question, ('Yes', 'No'), key=questions_asked)
55
+
56
+ # Update the progress
57
+ if answer == 'Yes':
58
+ possible_objects = [obj for obj in possible_objects if objects_and_questions[obj][current_questions[obj]] == question]
59
+ else:
60
+ possible_objects = [obj for obj in possible_objects if objects_and_questions[obj][current_questions[obj]] != question]
61
+
62
+ # Move to the next question for the object
63
+ for obj in possible_objects:
64
+ current_questions[obj] += 1
65
+
66
+ questions_asked += 1
67
+
68
+ # After 10 questions or fewer, try to guess
69
+ if len(possible_objects) == 1:
70
+ st.write(f"I think you were thinking of a **{possible_objects[0]}**!")
71
+ elif len(possible_objects) > 1:
72
+ st.write("I couldn't guess your object with certainty. Maybe you can narrow it down a bit?")
73
  else:
74
+ st.write("I couldn't guess anything based on the questions asked.")
75
+
76
+ # Main function for Streamlit
77
+ def main():
78
+ st.title("Guess My Thing Game")
79
+
80
+ # Start button
81
+ if st.button("Start Game"):
82
+ start_game()
83
+
84
+ if __name__ == "__main__":
85
+ main()