Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def get_user_choice():
- # Get user input for their choice, ensure it's valid
- user_choice = input("Enter your choice (Rock, Paper, or Scissors): ").capitalize()
- while user_choice not in ["Rock", "Paper", "Scissors"]:
- print("Invalid choice. Please choose Rock, Paper, or Scissors.")
- user_choice = input("Enter your choice (Rock, Paper, or Scissors): ").capitalize()
- return user_choice
- def get_computer_choice():
- # Generate a random choice for the computer
- choices = ["Rock", "Paper", "Scissors"]
- return random.choice(choices)
- def determine_winner(user_choice, computer_choice):
- # Determine the winner based on the game rules
- if user_choice == computer_choice:
- return "It's a tie!", 0
- elif (user_choice == "Rock" and computer_choice == "Scissors") or \
- (user_choice == "Paper" and computer_choice == "Rock") or \
- (user_choice == "Scissors" and computer_choice == "Paper"):
- return "You win!", 1
- else:
- return "Computer wins!", -1
- def main():
- # Main function to execute the game
- print("Welcome to Rock, Paper, Scissors!")
- user_score = 0
- computer_score = 0
- rounds = 0
- while True:
- user_choice = get_user_choice()
- computer_choice = get_computer_choice()
- print(f"You chose {user_choice}.")
- print(f"Computer chose {computer_choice}.")
- result, score_change = determine_winner(user_choice, computer_choice)
- print(result)
- user_score += score_change
- computer_score -= score_change
- rounds += 1
- print(f"Score after {rounds} rounds: You {user_score} - {computer_score} Computer")
- play_again = input("Do you want to play again? (yes/no): ").lower()
- if play_again != "yes":
- print("Thanks for playing! Final score:")
- print(f"You: {user_score} - Computer: {computer_score}")
- break
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement