lundi 28 mars 2022

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

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}")



Aucun commentaire:

Enregistrer un commentaire