🚀 Excited to share my first Python project! I’ve built a simple Snake-Water-Gun game while learning the basics of Python. This project helped me understand concepts like logic building, conditionals, and randomness. 🚀 Snake-Water-Gun Game using Python As part of my learning journey in Python, I built a simple Snake-Water-Gun game (similar to Rock-Paper-Scissors). Here’s a step-by-step breakdown of how the code works: 🔹 1. Importing Required Module We use the random module to allow the computer to make a random choice. import random 🔹 2. Defining Game Logic We assign numeric values to each option: Snake → 1 Water → -1 Gun → 0 This helps simplify comparison logic. 🔹 3. Computer’s Choice The computer randomly selects one of the three values: computer = random.choice([1, -1, 0]) 🔹 4. User Input The user enters their choice as: "s" for Snake "w" for Water "g" for Gun youstr = input("Enter your choice (s/w/g): ") 🔹 5. Mapping User Input We use a dictionary to convert user input into numeric form: youDict = {"s": 1, "w": -1, "g": 0} you = youDict[youstr] 🔹 6. Reverse Mapping for Output Another dictionary helps display readable results: reverseDict = {1: "Snake", -1: "Water", 0: "Gun"} 🔹 7. Display Choices We print both the user’s and computer’s selections: print(f"You chose {reverseDict[you]} and Computer chose {reverseDict[computer]}") 🔹 8. Game Result Logic If both choices are same → Draw Otherwise, conditions decide the winner: Snake drinks water → Snake wins Gun kills snake → Gun wins Water damages gun → Water wins if computer == you: print("It's a draw") else: if (computer == -1 and you == 1) or (computer == 1 and you == 0) or (computer == 0 and you == -1): print("You win 🎉") else: print("You lose 😢") 💡 Key Learnings: Using dictionaries for efficient mapping Applying conditional logic Generating randomness in Python Structuring a simple game 📌 This small project helped me understand how logic building works in real programs. Looking forward to building more! #Python #Coding #BeginnerProjects #LearningJourney #100DaysOfCode

To view or add a comment, sign in

Explore content categories