|
| 1 | +import streamlit as st |
| 2 | + |
| 3 | + |
1 | 4 | def check_guess(guess, answer):
|
2 |
| - global score |
3 |
| - still_guessing = True |
4 |
| - attempt = 0 |
5 |
| - while still_guessing and attempt < 3: |
6 |
| - if guess.lower() == answer.lower(): |
7 |
| - print("Correct Answer") |
8 |
| - score = score + 1 |
9 |
| - still_guessing = False |
10 |
| - else: |
11 |
| - if attempt < 2: |
12 |
| - guess = input("Sorry Wrong Answer, try again") |
13 |
| - attempt = attempt + 1 |
14 |
| - if attempt == 3: |
15 |
| - print("The Correct answer is ",answer ) |
16 |
| - |
17 |
| -score = 0 |
18 |
| -print("Guess the Animal") |
19 |
| -guess1 = input("Which bear lives at the North Pole? ") |
20 |
| -check_guess(guess1, "polar bear") |
21 |
| -guess2 = input("Which is the fastest land animal? ") |
22 |
| -check_guess(guess2, "Cheetah") |
23 |
| -guess3 = input("Which is the larget animal? ") |
24 |
| -check_guess(guess3, "Blue Whale") |
25 |
| -print("Your Score is "+ str(score)) |
| 5 | + if guess and guess.lower() == answer.lower(): |
| 6 | + return True, "Correct Answer!" |
| 7 | + elif guess: |
| 8 | + return False, "Sorry, Wrong Answer!" |
| 9 | + return None, "" |
| 10 | + |
| 11 | + |
| 12 | +def main(): |
| 13 | + st.title("Guess the Animal") |
| 14 | + |
| 15 | + score = 0 |
| 16 | + |
| 17 | + st.write("Which bear lives at the North Pole?") |
| 18 | + guess1 = st.text_input("Your answer for bear:", key='input1') # Unique key assigned |
| 19 | + is_correct1, response1 = check_guess(guess1, "polar bear") |
| 20 | + |
| 21 | + if is_correct1 == True: |
| 22 | + score += 1 |
| 23 | + st.success(response1) |
| 24 | + elif is_correct1 == False: |
| 25 | + st.error(response1) |
| 26 | + |
| 27 | + st.write("Which is the fastest land animal?") |
| 28 | + guess2 = st.text_input("Your answer for land animal:", key='input2') # Unique key assigned |
| 29 | + is_correct2, response2 = check_guess(guess2, "cheetah") |
| 30 | + |
| 31 | + if is_correct2 == True: |
| 32 | + score += 1 |
| 33 | + st.success(response2) |
| 34 | + elif is_correct2 == False: |
| 35 | + st.error(response2) |
| 36 | + |
| 37 | + st.write("Which is the largest animal?") |
| 38 | + guess3 = st.text_input("Your answer for largest animal:", key='input3') # Unique key assigned |
| 39 | + is_correct3, response3 = check_guess(guess3, "blue whale") |
| 40 | + |
| 41 | + if is_correct3 == True: |
| 42 | + score += 1 |
| 43 | + st.success(response3) |
| 44 | + elif is_correct3 == False: |
| 45 | + st.error(response3) |
| 46 | + |
| 47 | + st.write("Your Score is:", score) |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + main() |
0 commit comments