AbeerJadoon's picture
Update app.py
61fa7e6 verified
Raw
History Blame Contribute Delete
1.69 kB
import streamlit as st
import random
# Title and description
st.title("Rock, Paper, Scissors Game")
st.write("Play a simple game of Rock, Paper, Scissors against the computer! Track your score as you play.")
# Options
choices = ["Rock", "Paper", "Scissors"]
# Initialize session state for scores
if 'user_score' not in st.session_state:
st.session_state.user_score = 0
if 'computer_score' not in st.session_state:
st.session_state.computer_score = 0
if 'draws' not in st.session_state:
st.session_state.draws = 0
# User's choice
user_choice = st.selectbox("Choose your option:", choices)
# Button to play
def play_game():
# Computer's choice
computer_choice = random.choice(choices)
# Determine the winner
if user_choice == computer_choice:
result = "It's a draw!"
st.session_state.draws += 1
elif (
(user_choice == "Rock" and computer_choice == "Scissors") or
(user_choice == "Paper" and computer_choice == "Rock") or
(user_choice == "Scissors" and computer_choice == "Paper")
):
result = "You win!"
st.session_state.user_score += 1
else:
result = "Computer wins!"
st.session_state.computer_score += 1
return computer_choice, result
if st.button("Play"):
computer_choice, result = play_game()
# Display results
st.write(f"You chose: {user_choice}")
st.write(f"Computer chose: {computer_choice}")
st.success(result)
# Display scores
st.write("### Scores:")
st.write(f"Your Score: {st.session_state.user_score}")
st.write(f"Computer Score: {st.session_state.computer_score}")
st.write(f"Draws: {st.session_state.draws}")