game
To upload files, please first save the app
import streamlit as st
import numpy as np
st.title("Number Guessing Game")
# Initialize game state
if 'number' not in st.session_state:
st.session_state.number = np.random.randint(1, 101)
st.session_state.attempts = 0
st.session_state.game_over = False
# Game interface
st.write("I'm thinking of a number between 1 and 100. Can you guess it?")
# Input for user's guess
guess = st.number_input("Enter your guess:", min_value=1, max_value=100, step=1)
# Only enable the button if the game is not over
if st.button("Submit Guess", disabled=st.session_state.game_over):
st.session_state.attempts += 1
if guess == st.session_state.number:
st.success(f"🎉 Congratulations! You've found the number in {st.session_state.attempts} attempts!")
st.snow()
st.session_state.game_over = True
if st.button("Play Again"):
st.session_state.number = np.random.randint(1, 101)
st.session_state.attempts = 0
st.session_state.game_over = False
st.rerun()
elif guess < st.session_state.number:
st.warning("Too low! Try a higher number.")
else:
st.warning("Too high! Try a lower number.")
# Display number of attempts
st.write(f"Number of attempts: {st.session_state.attempts}")
# Instructions
with st.expander("How to play"):
st.write("""
1. Enter a number between 1 and 100
2. Click 'Submit Guess'
3. Follow the hints to guess the correct number
4. Try to guess the number in as few attempts as possible!
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?