💻Project 3 : Snake-Water-Gun Game In Python
Project 3 : Snake-Water-Gun Game In Python
This Python project is a simple game called "Snake-Water-Gun." It utilizes several fundamental programming concepts to create an interactive game where a player competes against the computer. The project showcases the use of the random module, if-else statements, lists, variables, and basic data types.
Here's an explanation of the code in easy-to-understand language:
-
Introduction: The program begins with a welcoming message, introducing the user to the "Snake-Water-Gun" game.
-
Importing the Random Module: The code imports the
random
module, which allows the program to randomly select an option for the computer's choice. -
Creating the List of Choices: A list named
list1
is created containing the three choices: "Snake," "Water," and "Gun." These are the options available for both the player and the computer. -
Computer's Choice: The computer's choice is generated randomly from the
list1
using therandom.choice()
function. This represents the computer's move in the game. -
Player's Choice: The program prompts the player to enter their choice among "Snake," "Water," or "Gun." The player's choice is stored in the
Players_Choice
variable. -
Comparison and Result:
- The program compares the computer's choice and the player's choice using a series of
if-else
statements. - If both choices are the same, it results in a tie.
- If the choices differ, the program checks different combinations of choices to determine the winner. For instance:
- If the computer chose "Snake" and the player chose "Water," the computer wins.
- If the computer chose "Snake" and the player chose "Gun," the player wins.
- And similarly for other combinations.
- Depending on the comparisons, the program prints out who won or if it's a tie.
- The program compares the computer's choice and the player's choice using a series of
-
Outcome Printing: The program displays the computer's choice and then prints the outcome of the game based on the comparison made earlier. If the player enters an option other than "Snake," "Water," or "Gun," the program indicates that the input is invalid.
In summary, this project demonstrates a basic interactive game where the player competes against the computer in the "Snake-Water-Gun" game. The code randomly selects the computer's choice, takes the player's input, compares the choices using if-else
statements, and then displays the winner or a tie. This project effectively employs essential programming concepts like randomness, conditional statements, list handling, variables, and basic data types.
Code:
1import random
2
3print("\nWellcome To Snake-Water-Gun Game !!!\n")
4
5
6list1 = ["Snake", "Water","Gun"]
7Computers_Choice = random.choice(list1)
8print("{ Snake-Water-Gun }")
9Players_Choice = input("Enter Option : ")
10print(f"Computer choose : ",Computers_Choice)
11
12if (Computers_Choice == Players_Choice):
13 print("!!!Tie!!!")
14
15
16elif (Computers_Choice == "Snake" and Players_Choice == "Water"):
17 print("!!!Computer Won!!!")
18
19elif (Computers_Choice == "Snake" and Players_Choice == "Gun"):
20 print("!!!You Won!!!")
21
22
23
24elif (Computers_Choice == "Water" and Players_Choice == "Snake"):
25 print("!!!You Won!!!")
26
27elif (Computers_Choice == "Water" and Players_Choice == "Gun"):
28 print("!!!Computer Won!!!")
29
30
31elif (Computers_Choice == "Gun" and Players_Choice == "Snake"):
32 print("!!!Computer Won!!!")
33
34elif (Computers_Choice == "Gun" and Players_Choice == "Water"):
35 print("!!!You Won!!!")
36
37else :
38 print("Invalid Input")