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(1)

mudit2013 10 months, 1 week ago
Thanks for the solution. you are amazing teacher .

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