Snake Water Gun Game in Python - Exercise 5 - Solution | Python Tutorial - Day #63

preview_player
Показать описание

python, C, C++, Java, JavaScript and Other Cheetsheets [++]:

►Learn in One Video[++]:

►Complete course [playlist]:

Follow Me On Social Media
Comment "#HarryBhai" if you read this 😉😉
Рекомендации по теме
Комментарии
Автор

@CodeWithHarry bhai mene matrix ke through iss problem ko solve kiya hai

import numpy as np
import random
Array=np.array([["TIE", "Win", "Loose"] , ["Loose","Tie","WIN"] , ["Win","Loose","Tie"]] )
l={"Snake":0, "Water":1, "Gun":2}

print("Enter\n0 for snake\n1 for water\n2 for Gun")
a=int(input("Enter your value : "))
print(f"Computer = {b[0]}")
print("Result = ", Array[a][b[1]])

sagarjamadar
Автор

import random

def playerPlays():
return input("Choose between rock, paper, and scissors: ")

def computerPlays():
choices = ["rock", "paper", "scissors"]
return random.choice(choices)

def getGameResult(player_choice, computer_choice):
rules = {
"rock": {"rock": "It's a draw", "paper": "You lose", "scissors": "You win"},
"paper": {"rock": "You win", "paper": "It's a draw", "scissors": "You lose"},
"scissors": {"rock": "You lose", "paper": "You win", "scissors": "It's a draw"}
}
return

while True:
player_choice = playerPlays()
computer_choice = computerPlays()
print(f"Computer plays: {computer_choice}.")

game_play = getGameResult(player_choice, computer_choice)
print(game_play)

if "lose" in game_play:
print(f"You lost to the computer. Thanks for playing, {name}!")
break

MidKnight
Автор

Harry i used a different function at the place of random module. I created a set of 3 values 0 1 and2 and used pop function instead and its working great.

FighterMAGNUM
Автор

You can also use SET, because sets are unordered and you can put snake, water and gun in the set and after accessing the set[0] items, you will get a new item every time.
so you don't have to include the random Module

Jashan_
Автор

Simple but working


import random

opt = ["snake", "water", "gun"]
print(opt)
a = random.choice(opt)
b = input("Choose between the list options:")
print (f"Computer choose: {a}")
def check():
if (a==b):
return "draw"
elif (a == opt[0] and b== opt[1]):
return "You lose"
elif (a == opt[1] and b== opt[0]):
return "You win"
elif (a == opt[2] and b== opt[1]):
return "You win"
elif (a == opt[2] and b== opt[0]):
return "You lose"
elif (a == opt[1] and b== opt[2]):
return "You lose"
elif (a == opt[0] and b== opt[2]):
return "You win"
print(check())

hamza.syed
Автор

print("enter your value between 1 to 3")
print(" choose 1 for snake")
print(" choose 2 for water")
print(" choose 3 for gun")
output = int(input())
print("player:", output)
if (output == 1):
print("snake")
if (output == 2):
print("water")
if (output == 3):
print("gun")
import random

num = random.randint(1, 3)
print("computer:", num)
if (num == 1):
print("snake")
if (num == 2):
print("water")
if (num == 3):
print("gun")
print("final result:")
if (output == 1 and num == 1):
print("this match is draw")
if (output == 1 and num == 2):
print("boom you win this game")
if (output == 1 and num == 3):
print("oh oh you loose better luck nxt tym")
if (output == 2 and num == 1):
print("boom you win this game")
if (output == 2 and num == 2):
print("this match is draw")
if (output == 2 and num == 3):
print("oh oh you loose better luck nxt tym")
if (output == 3 and num == 1):
print("oh oh you loose better luck nxt tym")
if (output == 3 and num == 2):
print("boom you win this game")
if (output == 3 and num == 3):
print("this match is draw")
if(output>3 ):
print("error in input concentrate bro")

dhillon
Автор

import random
print("1 for Snake\n2 for Gun\n3 for Snake")
user=int(input("Enter your choise : "))
comp = random.randint(1, 3)
print(comp)
if((user==1 and comp==1) or(user==2 and comp==2)or (user==3 and comp==3)):
print("Its DRAW")
elif((user==1 and comp==3) or(user==2 and comp==1)or (user==3 and comp==2)):
print("You WON")
else:
print("Try Again")

ankitmatade
Автор

Harry bhai...i learnt the entire JavaScript by following your playlist. But i have a question for you. Its not ki will chatgpt replace programmers (i already watched your chatgpt video ), my question actually is ki will the industry now need much lesser number of software developers than used to be the case before.

spacexfanclub
Автор

# SNAKE WATER GAME BY HARRY.
print("RULES OF THE GAME ARE :-")
print("YOU HAVE ONLY FIVE CHANCES !!")
print("enter the number between 0, 1, 2")
print("0 stands for snake, 1 stands for water, 2 stands for gun")

count = 1
while count<=5:
import random

def check(comp, user):
if comp == user:
return 0
if(comp == 0 and user== 1):
return -1
if(comp == 0 and user == 2):
return -1
if(comp == 2 and user == 0):
return -1
else:
return 1

user = int(input("enter between (0, 1, 2):"))
comp = random.randint(0, 2)
print("you entered:", user)
print("the computer entered:", comp)

score = check(comp, user)
if score == 0:
print("it's a draw")
if count == 4:
print("LAST CHANCE COME ON!!!!")
elif score == -1:
print("you lose!")
if count == 4:
print("LAST CHANCE COME ON!!!")
elif score == 1:
print(" hurray you won!!!!")
break
if count == 4:
print("LAST CHANCE COME ON!!!")

count = count+1
print("all the chances are

mr.detecto
Автор

I made my own full blown snake water gun in python with single and two player modes, i even got a score method with negative marking that does not go below 0.
import random
# Main Menu
print("Press 0 for SinglePlayer(Bot)")
print("Press 1 for Two Players")
S1 = 0 # initial player1 score
S2 = 0 # initial player2 score
userinput = int(input("Chose: "))
print("")
# Main Game Loop (Singleplayer)
if (userinput == 0):
player = input("Player Name: ")
player1 = player.capitalize()
player2 = str("Computer")
print("")
while True:
a = input(f"{player1} {S1}, Snake/Water/Gun: ")
if(a.upper() == "E"):
exit()
b = random.randint(0, 2)
if(b == 0):
b = str("Snake")
if(b == 1):
b = str("Water")
if(b == 2):
b = str("Gun")
print("Computer Chose", b)
if(a.lower() == b.lower()):
print("Draw!!")
print(f"Score = {S1}")
elif(a.capitalize() == "Snake" and b.capitalize() == "Water"):
print(f"{player1} Won!!")
S1 = S1 + 1
print(f"Score = {S1}")
elif(a.lower() == "snake" and b.lower() == "gun"):
print(f"{player2} Won!!")
if(S1 != 0):
S1 = S1 - 1
print(f"Score = {S1}")
elif(a.capitalize() == "Water" and b.capitalize() == "Gun"):
print(f"{player1} Won!!")
S1 = S1 + 1
print(f"Score = {S1}")
elif(a.lower() == "water" and b.lower() == "snake"):
print(f"{player2} Won!!")
if(S1 != 0):
S1 = S1 - 1
print(f"Score = {S1}")
elif(a.capitalize() == "Gun" and b.capitalize() == "Snake"):
print(f"{player1} Won!!")
S1 = S1 + 1
print(f"Score = {S1}")
elif(a.lower() == "gun" and b.lower() == "water"):
print(f"{player2} Won!!")
if(S1 != 0):
S1 = S1 - 1
print(f"Score = {S1}")
else:
print("Invalid Entries!!")
wait = input()
# Main Game Loop (Two Players)
if (userinput == 1):
player1 = input("Player 1: ").capitalize()
player2 = input("Player 2: ").capitalize()
print("")
while True:
a = input(f"{player1} {S1}, Snake/Water/Gun: ")
if(a.upper() == "E"):
exit()
b = input(f"{player2} {S2}, Snake/Water/Gun: ")
if(b.lower() == "e"):
exit()
if(a.lower() == b.lower()):
print("Draw!!")
print(f"{player1} = {S1}")
print(f"{player2} = {S2}")
elif(a.capitalize() == "Snake" and b.capitalize() == "Water"):
print(f"{player1} Won!!")
S1 = S1 + 1
if(S2 != 0):
S2 = S2 - 1
print(f"{player1} = {S1}")
print(f"{player2} = {S2}")
elif(a.lower() == "snake" and b.lower() == "gun"):
print(f"{player2} Won!!")
S2 = S2 + 1
if(S1 != 0):
S1 = S1 - 1
print(f"{player1} = {S1}")
print(f"{player2} = {S2}")
elif(a.capitalize() == "Water" and b.capitalize() == "Gun"):
print(f"{player1} Won!!")
S1 = S1 + 1
if(S2 != 0):
S2 = S2 - 1
print(f"{player1} = {S1}")
print(f"{player2} = {S2}")
elif(a.lower() == "water" and b.lower() == "snake"):
print(f"{player2} Won!!")
S2 = S2 + 1
if(S1 != 0):
S1 = S1 - 1
print(f"{player1} = {S1}")
print(f"{player2} = {S2}")
elif(a.capitalize() == "Gun" and b.capitalize() == "Snake"):
print(f"{player1} Won!!")
S1 = S1 + 1
if(S2 != 0):
S2 = S2 - 1
print(f"{player1} = {S1}")
print(f"{player2} = {S2}")
elif(a.lower() == "gun" and b.lower() == "water"):
print(f"{player2} Won!!")
S2 = S2 + 1
if(S1 != 0):
S1 = S1 - 1
print(f"{player1} = {S1}")
print(f"{player2} = {S2}")
else:
print("Invalid Entries!!")
wait = input()

krishivgoel
Автор

Aaj bahot achha laga apse mere naam ka shout out sunke
Thank you :)

ThoreKrishna
Автор

check function is so optimized bro,
i realy love it
😍😍😍

LowPC-Programmer
Автор

kabhi ap ko itna nahi ae ga ke
dusra ap ko kuch na sikha pae😍
love from pakistan

shahabrao
Автор

import random

def game():
choices = ['snake', 'water', 'gun']

# Get user's choice
user_choice = input("Enter your choice (snake/water/gun): ").lower()

# Validate user's choice
if user_choice not in choices:
print("Invalid choice! Please choose either snake, water, or gun.")
return

# Generate computer's choice
computer_choice = random.choice(choices)

# Print the choices made by the user and computer
print("Your choice:", user_choice)
print("Computer's choice:", computer_choice)

# Determine the winner
if user_choice == computer_choice:
print("It's a tie!")
elif user_choice == 'snake':
if computer_choice == 'water':
print("You win! Snake drinks water.")
else:
print("You lose! Gun kills snake.")
elif user_choice == 'water':
if computer_choice == 'gun':
print("You win! Water extinguishes the gun.")
else:
print("You lose! Snake drinks water.")
elif user_choice == 'gun':
if computer_choice == 'snake':
print("You win! Gun kills snake.")
else:
print("You lose! Water extinguishes the gun.")

# Start the game
game()

AliMurtaza-cg
Автор

import random

# . R P S
# R 0 -1 1
# P 1 0 -1
# S -1 1 0
comp = random.choice([0, 1, 2])
choice = int(
input("Enter your choice\n0 for Rock\n1 for Paper\n2 for Scissor\n"))

game = [[0, 1, -1], [-1, 0, 1], [1, -1, 0]]
print(f"Computer has choosen: {comp}")
if game[comp][choice] == 0:
print("Draw")
if game[comp][choice] == 1:
print("You Won")
if game[comp][choice] == -1:
print("You loose")

AbhishekGupta-irly
Автор

here is my solution
# The gun beats the snake, the water beats the gun, and the snake beats the water
while True:
import random

a = random.randint(1, 3+1)
inp = input("Enter any one of these ('snake', 'water', 'gun')")

if a == 1:
a_1 = ("snake")
if inp == "snake":
print(f"snake vs {a_1}\t 'TIE' ")
elif inp == "water":
print(f" water vs {a_1} \tyou lost")
break
elif inp == "gun":
print(f"gun vs {a_1} \tyou WON")
else:
print("enter a valid value read the instructions")


elif a == 2:
a_2 = ("water")

if inp == "snake":
print(f"snake vs {a_2}\tyou WON")
elif inp == "water":
print(f"water vs {a_2}\tTIE")
elif inp == "gun":
print(f"gun vs {a_2}\tyou lost")
break
else:
print("enter a valid value read the instructions")
else:
a_3 = ("gun")
if inp== "gun":
print(f"gun vs {a_3}\tTIE")
elif inp == "snake":
print(f"snake vs {a_3}\tyou lost")
break
elif inp == "water":
print(f"water vs {a_3}\tyou won")
else:
print("enter a valid value read the instructions")

HamzaShahid-st
Автор

import random
while True:
play = input("do you want to play game say Y/N:")
if play == "y":
print("Welcome to the game")
lst = ["snake", "water", "gun"]
usesr_input = input("choose one option from \n snake \n water\n gun ")
comp_input = random.choice(lst)
print("computer choice", comp_input)

if comp_input == "snake" and usesr_input == "water":
print("Comuter win and you lose")

elif comp_input == "gun" and usesr_input == "water":
print("Comuter win and you lose")

elif comp_input == "gun" and usesr_input == "snake":
print("Comuter win and you lose")

elif comp_input == usesr_input:
print("Match draw")



else:
print("you win")
#gun


else:
print("thanks for palying game CU soon....!!!")
break

anupamchaubey
Автор

You are the real teacher, love you carry bro.

amazingshorts
Автор

My code : import random
a= random.randint(1, 4)

while True:
b=int(input("Enter your choice: "))
if(a==b):
print("Draw")
elif (a==1 and b==2) or (a==2 and b==3) or (a==3 and b==1):
print("You lose")
elif (a==1 and b==3) or (a==1 and b==1) or (a==3 and b==2):
print("Congratulations!\nYou win")

dhanashrichitukane
Автор

Harry bhai itne pyare hai itne masumi se smile karte hai

GlitchyGlobe
visit shbcf.ru