Python-based Project for "Rock, Paper, Scissors." !
Below is a complete Python-based project for "Rock, Paper, Scissors." This program allows a user to play against the computer, which makes random moves. It also includes a scoring system to keep track of rounds won by each player.
Rock, Paper, Scissors Project Code
import random
def print_welcome():
print("\nWelcome to Rock, Paper, Scissors!")
print("Rules:")
print("- Rock beats Scissors")
print("- Scissors beats Paper")
print("- Paper beats Rock")
print("- First to 3 wins the game!")
def get_computer_choice():
return random.choice(["rock", "paper", "scissors"])
def get_user_choice():
while True:
choice = input("Enter your choice (rock, paper, scissors): ").lower()
if choice in ["rock", "paper", "scissors"]:
return choice
else:
print("Invalid choice. Please choose 'rock', 'paper', or 'scissors'.")
def decide_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "tie"
elif (user_choice == "rock" and computer_choice == "scissors") or \
(user_choice == "scissors" and computer_choice == "paper") or \
(user_choice == "paper" and computer_choice == "rock"):
return "user"
else:
return "computer"
def print_round_result(user_choice, computer_choice, winner):
print(f"\nYou chose: {user_choice}")
print(f"Computer chose: {computer_choice}")
if winner == "tie":
print("It's a tie!")
elif winner == "user":
print("You win this round!")
else:
print("Computer wins this round!")
def main():
print_welcome()
user_score = 0
computer_score = 0
winning_score = 3
while user_score < winning_score and computer_score < winning_score:
user_choice = get_user_choice()
computer_choice = get_computer_choice()
winner = decide_winner(user_choice, computer_choice)
print_round_result(user_choice, computer_choice, winner)
if winner == "user":
user_score += 1
elif winner == "computer":
computer_score += 1
print(f"\nScore -> You: {user_score} | Computer: {computer_score}")
if user_score == winning_score:
print("\nCongratulations! You won the game!")
else:
print("\nSorry, the computer won the game. Better luck next time!")
if __name__ == "__main__":
main()
Features:
- Randomized Computer Moves: The computer's choice is made using Python's
random.choice()
function. - Input Validation: Ensures that the user enters a valid choice.
- Score Tracking: Keeps track of the scores for both the user and the computer.
- Winning Condition: The first player to reach a score of 3 wins the game.
- Interactive Gameplay: Users get immediate feedback on each round and the current score.
How to Run:
- Copy the code into a Python IDE or text editor (e.g., VS Code, PyCharm, or even IDLE).
- Save the file as
rock_paper_scissors.py
. - Run the program in your terminal or Python environment using:
python rock_paper_scissors.py
- Follow the prompts to play!
You can further enhance this project by adding features like:
- Allowing the user to set a custom winning score.
- Adding more choices like "Lizard" and "Spock" for an extended version.
- Implementing a graphical user interface (GUI) using libraries like
tkinter
orPyQt
.
Comments
Post a Comment
"Thank you for seeking advice on your career journey! Our team is dedicated to providing personalized guidance on education and success. Please share your specific questions or concerns, and we'll assist you in navigating the path to a fulfilling and successful career."