Home > Blockchain >  Why is this code of snake, water and gun not working?
Why is this code of snake, water and gun not working?

Time:03-29

I got an exercise while learning python from this tutorial and I got an exercise of Snake, Water and Gun and did solve it but the program just takes an input and then stops. This is the code I am using:

import random
print("Welcome to Snake, Water or Gun!")

opts = ["Snake", "Water", "Gun"]
u_ch = input("Enter Snake, Water or Gun:")
c_ch = random.choice(opts)
chances = 10
ties = 0
u_score = 0
c_score = 0
while chances < 10:
    if u_ch == "Snake" and c_ch == "Snake":
        chances -= 1
        ties  = 1
        print("It is a tie.")
        print(f"{chances} chances left.")

    elif u_ch == "Snake" and c_ch == "Water":
        chances -= 1
        u_score  = 1
        print("Computer wins 1 point.")
        print(f"{chances} chances left.")

    elif u_ch == 'Snake'and c_ch == "Gun":
        chances -= 1
        u_score  = 1
        print("You win 1 point.")
        print(f"{chances} chances left.")

    if u_ch == "Water" and c_ch == "Snake":
        chances -= 1
        c_score  = 1
        print("Computer wins 1 point.")
        print(f"{chances} chances left.")

    elif c_ch == "Water":
            chances -= 1
            ties  = 1
            print("It is a tie.")
            print(f"{chances} chances left.")

    if c_ch == "Gun":
            chances -= 1
            u_score  = 1
            print("Player wins 1 point.")
            print(f"{chances} chances left.")

    if u_ch == "Gun":
        if c_ch == "Snake":
            chances -= 1
            u_score  = 1
            print("Player wins 1 point.")
            print(f"{chances} chances left.")

        elif c_ch == "Water":
            chances -= 1
            c_score  = 1
            print("Computer wins 1 point.")
            print(f"{chances} chances left.")

        elif c_ch == "Gun":
            chances -= 1
            ties  = 1
            print("It is a tie.")
            print(f"{chances} chances left.")

if c_score > u_score:
    print(f"You lose!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
if u_score > c_score:
    print(f"You win!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
if c_score == u_score:
    print(f"Tie!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")

I am not able to understand the problem at all.

I wanted the computer to play a full game and I used the random module also but it stops after taking an input only. This is the code:

import random
print("Welcome to Snake, Water or Gun!")

opts = ["Snake", "Water", "Gun"]
u_ch = input("Enter Snake, Water or Gun:")
c_ch = random.choice(opts)
chances = 10
ties = 0
u_score = 0
c_score = 0
while chances < 10:
    if u_ch == "Snake" and c_ch == "Snake":
        chances -= 1
        ties  = 1
        print("It is a tie.")
        print(f"{chances} chances left.")

    elif u_ch == "Snake" and c_ch == "Water":
        chances -= 1
        u_score  = 1
        print("Computer wins 1 point.")
        print(f"{chances} chances left.")

    elif u_ch == 'Snake'and c_ch == "Gun":
        chances -= 1
        u_score  = 1
        print("You win 1 point.")
        print(f"{chances} chances left.")

    if u_ch == "Water" and c_ch == "Snake":
        chances -= 1
        c_score  = 1
        print("Computer wins 1 point.")
        print(f"{chances} chances left.")

    elif c_ch == "Water":
            chances -= 1
            ties  = 1
            print("It is a tie.")
            print(f"{chances} chances left.")

    if c_ch == "Gun":
            chances -= 1
            u_score  = 1
            print("Player wins 1 point.")
            print(f"{chances} chances left.")

    if u_ch == "Gun":
        if c_ch == "Snake":
            chances -= 1
            u_score  = 1
            print("Player wins 1 point.")
            print(f"{chances} chances left.")

        elif c_ch == "Water":
            chances -= 1
            c_score  = 1
            print("Computer wins 1 point.")
            print(f"{chances} chances left.")

        elif c_ch == "Gun":
            chances -= 1
            ties  = 1
            print("It is a tie.")
            print(f"{chances} chances left.")

if c_score > u_score:
    print(f"You lose!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
if u_score > c_score:
    print(f"You win!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
if c_score == u_score:
    print(f"Tie!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")

CodePudding user response:

Look at those two lines from your code, they will tell you what is wrong!

...
chances = 10
while chances < 10:
    ...

CodePudding user response:

while chances < 10: I'm guessing you want to do it ten times randomly and judge the score based on the result
I fixed your code, try again

import random
print("Welcome to Snake, Water or Gun!")

opts = ["Snake", "Water", "Gun"]
u_ch = input("Enter Snake, Water or Gun:")
chances = 10
ties = 0
u_score = 0
c_score = 0
while chances > 0:
    c_ch = random.choice(opts)
    if u_ch == "Snake" and c_ch == "Snake":
        chances -= 1
        ties  = 1
        print("It is a tie.")
        print(f"{chances} chances left.")

    elif u_ch == "Snake" and c_ch == "Water":
        chances -= 1
        u_score  = 1
        print("Computer wins 1 point.")
        print(f"{chances} chances left.")

    elif u_ch == 'Snake'and c_ch == "Gun":
        chances -= 1
        u_score  = 1
        print("You win 1 point.")
        print(f"{chances} chances left.")

    if u_ch == "Water" and c_ch == "Snake":
        chances -= 1
        c_score  = 1
        print("Computer wins 1 point.")
        print(f"{chances} chances left.")

    elif c_ch == "Water":
            chances -= 1
            ties  = 1
            print("It is a tie.")
            print(f"{chances} chances left.")

    if c_ch == "Gun":
            chances -= 1
            u_score  = 1
            print("Player wins 1 point.")
            print(f"{chances} chances left.")

    if u_ch == "Gun":
        if c_ch == "Snake":
            chances -= 1
            u_score  = 1
            print("Player wins 1 point.")
            print(f"{chances} chances left.")

        elif c_ch == "Water":
            chances -= 1
            c_score  = 1
            print("Computer wins 1 point.")
            print(f"{chances} chances left.")

        elif c_ch == "Gun":
            chances -= 1
            ties  = 1
            print("It is a tie.")
            print(f"{chances} chances left.")

if c_score > u_score:
    print(f"You lose!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
if u_score > c_score:
    print(f"You win!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
if c_score == u_score:
    print(f"Tie!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")

CodePudding user response:

The chances variable is set to 10, but the while loop only runs if it is less than 10.

  • Related