Python Microproject
Python Microproject
Introduction:
The Rock Paper Scissors game is a classic hand-based strategy game traditionally
played between two participants. This Python implementation transforms the
physical game into a digital, interactive experience, where the player competes
against a computer opponent in a best-of-five match format.
Key Features
3. Technical Implementation
Component Description
Usage in Game:
def get_user_choice():
# Gets and validates user input
while True:
user_choice = input("Choose rock, paper, or scissors: ").lower()
if user_choice in ['rock', 'paper', 'scissors']:
return user_choice
print("Invalid choice. Please try again.")
if
elif
else
- Game Implementation:
if user_choice == computer_choice:
return "tie"
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 "user"
else:
return "computer"
While Loops:
while True:
# Main game loop
play_again = input("Play again? (y/n): ")
if play_again != 'y':
break
2. Python Standard Library Modules
2.1 random Module
Import Statement:
python
Copy
import random
Key Function Used:
python
Copy
random.choice(['rock', 'paper', 'scissors'])
python
Copy
user_choice = input("Choose rock, paper, or scissors: ")
Purpose: Get player input
print():
3. Data Structures
3.1 Lists
Usage Examples:
win_conditions = {
'rock': 'scissors',
'paper': 'rock',
'scissors': 'paper'
}
Advantage: More maintainable win logic
Components:
5. Error Handling
5.1 Input Validation
while True:
choice = input("Your choice: ").lower()
if choice in valid_options:
return choice
print("Invalid input!")
7. String Manipulation
7.1 Key Operations
Case Conversion:
user_choice.lower()
String Formatting:
print(f"Score: {user_score}-{computer_score}")
8. Program Structure Best Practices
8.1 Modular Design
Separation of:
- User input handling
- Game logic
- Display/output
- Benefits:
- Easier debugging
- Simple enhancements
Code:
import random
def get_user_choice():
"""Get and validate user input"""
while True:
user_choice = input("Choose rock, paper, or scissors (or 'q' to quit): ").lower()
if user_choice in ['rock', 'paper', 'scissors', 'q']:
return user_choice
print("Invalid choice. Please try again.")
def get_computer_choice():
"""Generate random computer choice"""
return random.choice(['rock', 'paper', 'scissors'])
user_choice = get_user_choice()
if user_choice == 'q':
return None
computer_choice = get_computer_choice()
if result == "tie":
print("It's a tie! No points awarded.")
elif result == "user":
print("You win this round!")
user_score += 1
else:
print("Computer wins this round!")
computer_score += 1
round_num += 1
# Match conclusion
print("\n=== Match Result ===")
print(f"Final Score - You: {user_score} | Computer: {computer_score}")
if user_score == 5:
print("Congratulations! You won the match!")
else:
print("Computer won the match. Better luck next time!")
return True
def main():
"""Main game function"""
print("Welcome to Rock, Paper, Scissors - Best of 5!")
print("--------------------------------------------")
while True:
match_result = play_match()
if __name__ == "__main__":
main()
OUTPUT:
Conclusion:
The game serves as an excellent example of how basic Python components can
combine to create interactive applications while following software engineering
best practices. Each element has been carefully selected to fulfill specific game
requirements while maintaining code clarity and extensibility.