Course Content

Exercise 6 Solution & First Solver

In this tutorial, I have described the Exercise-6 Solution (Snake Water Gun). In Exercise-6, we have to create a 'Snake Water Gun' game, so here is the solution to that exercise. We've used simple concepts of Python to create this game. Go through the video for a better understanding. 

Follow these instructions to get to a perfect solution:

Instructions:

Today's task for you guys will be to develop your first-ever Python game i.e., "Snake Water Gun."

Most of you must already be familiar with the game. Still, I will provide you with a brief description.

This is a two-player game where each player chooses one object.  As we know, there are three objects, snake, water, and gun. So, the result will be 

  • Snake vs. Water: Snake drinks the water hence wins.
  • Water vs. Gun: The gun will drown in water, hence a point for water
  • Gun vs. Snake: Gun will kill the snake and win.
  • In situations where both players choose the same object, the result will be a draw.

Now moving on to instructions:

  • You have to use a random choice function that we studied in tutorial #38, to select between, snake, water, and gun.
  • You do not have to use a print statement in case of the above function.
  • Then you have to give input from your side.
  • After getting ten consecutive inputs, the computer will show the result based on each iteration.
  • You have to use loops(while loop is preferred).

 

Code file as described in the video

# Snake water gun

import random
lst = ['s','w','g']

chance = 10
no_of_chance = 0
computer_point = 0
human_point = 0

print(" \t \t \t \t Snake,Water,Gun Game\n \n")
print("s for snake \nw for water \ng for gun \n")

# making the game in while
while no_of_chance < chance:
    _input = input('Snake,Water,Gun:')
    _random = random.choice(lst)

    if _input == _random:
        print("Tie Both 0 point to each \n ")

    # if user enter s
    elif _input == "s" and _random == "g":
        computer_point = computer_point + 1
        print(f"your guess {_input} and computer guess is {_random} \n")
        print("computer wins 1 point \n")
        print(f"computer_point is {computer_point} and your point is {human_point} \n ")

    elif _input == "s" and _random == "w":
        human_point = human_point + 1
        print(f"your guess {_input} and computer guess is {_random} \n")
        print("Human wins 1 point \n")
        print(f"computer_point is {computer_point} and your point is {human_point} \n")

    # if user enter w
    elif _input == "w" and _random == "s":
        computer_point = computer_point + 1
        print(f"your guess {_input} and computer guess is {_random} \n")
        print("computer wins 1 point \n")
        print(f"computer_point is {computer_point} and your point is {human_point} \n ")

    elif _input == "w" and _random == "g":
        human_point = human_point + 1
        print(f"your guess {_input} and computer guess is {_random} \n")
        print("Human wins 1 point \n")
        print(f"computer_point is {computer_point} and your point is {human_point} \n")

    # if user enter g

    elif _input == "g" and _random == "s":
        human_point = human_point + 1
        print(f"your guess {_input} and computer guess is {_random} \n")
        print("Human wins 1 point \n")
        print(f"computer_point is {computer_point} and your point is {human_point} \n")


    elif _input == "g" and _random == "w":
        computer_point = computer_point + 1
        print(f"your guess {_input} and computer guess is {_random} \n")
        print("computer wins 1 point \n")
        print(f"computer_point is {computer_point} and your point is {human_point} \n ")

    else:
        print("you have input wrong \n")

    no_of_chance = no_of_chance + 1
    print(f"{chance - no_of_chance} is left out of {chance} \n")

print("Game over")

if computer_point==human_point:
    print("Tie")

elif computer_point > human_point:
    print("Computer wins and you loose")

else:
    print("you win and computer loose")

print(f"your point is {human_point} and computer point is {computer_point}")

#
# Snake Water Gun Game in Python
# The snake drinks the water, the gun shoots the snake, and gun has no effect on water.
#

  

Comments(2)

mudit2013 1 year, 1 month ago
Thanks for the solution. you are amazing teacher .
thesunil 3 months ago
# Snake water Gun
import random

print("***** Snake water Gun *****")
print()
print("        GAME START         ")
print()
no_chance = 10
chance_used = 1
computer_score = 0
player_score = 0
choices = ["w", "s", "g"]

while (chance_used <=  no_chance):
    choice = random.choice(choices)
    print("Please note:  s for snake ,w for water and g for gun: ")
    print()
    player_1 = input("please select: ")

    if player_1 == "s" and choice == "w":
        print(f"snake drinks all {choice}")
        print("you win")
        player_score = player_score + 1
        print(f"chance used : {chance_used}")


    elif player_1 == "w" and choice == "s":
        print(f"snake drinks all {choice}")
        print("you loose !!")
        computer_score = computer_score + 1
        print(f"chance used : {chance_used}")

    elif player_1 == "w" and choice == "g":
        print(f"water destroy {choice}")
        print("You win")
        player_score = player_score + 1

        print(f"chance used : {chance_used}")

    elif player_1 == "g" and choice == "w":
        print(f"water destroy {choice}")
        print("You loose!!")
        computer_score = computer_score + 1

        print(f"chance used : {chance_used}")

    elif player_1 == "g" and choice == "s":
        print(f"{choice} kills snake")
        print("you loose!")
        computer_score = computer_score + 1

        print(f"chance used : {chance_used}")

    elif player_1 == "s" and choice == "g":
        print(f"{choice} kills snake")
        print("you win ")
        player_score = player_score + 1

        print(f"chance used : {chance_used}")

    elif player_1 == choice:
        print("Tie both get 0 point")

        print(f"chance used : {chance_used}")

    else:
        print("wrong input ")
    chance_used = chance_used + 1
    print()
print("*****Game Over*****")
print(f"your score is {player_score}")
print(f"computer score is {computer_score}")
if computer_score > player_score:
    print("you loose the game !!!")
    print("Try Again")
elif computer_score == player_score:
    print("game Tie ")
    print("Try again")
else:
    print("You won the game")

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here