简体   繁体   中英

Bug in my rock paper scissors game , loop doesn't work correct after pass statement

    # Created By Evan Ryan

# we import our random module and time module
from random import *
import time

# this will track our wins
comp_wins = 0
player_wins = 0

# this will let us know when the match is finished
endgame = 0

# here we use simple print and sleep function to display a welcome message

print("Welcome to Rock, Paper, Scissors, Lizard, Spock")
time.sleep(3)
print("This match will be best 3 out of 5")
time.sleep(3)
print("Good Luck")
time.sleep(1)

# this will be our function to allow the user to make a choice
# they have 5 choices
# if they make a typo or choose a non option they will be asked to choose again
# we will also convert their selection to a lowercase letter

def pick_option():
    user_pick = input("Choose Rock, Paper, Scissors, Lizard, Spock: ")
    if user_pick in ["Rock", "rock", "r", "R"]:
        user_pick = "r"
    elif user_pick in ["Paper", "paper", "p", "P"]:
        user_pick = "p"
    elif user_pick in ["Scissors", "scissors", "sc", "SC"]:
        user_pick = "sc"
    elif user_pick in ["Lizard", "lizard", "l", "L"]:
        user_pick = "l"
    elif user_pick in ["Spock", "spock", "sp", "SP"]:
        user_pick = "sp"
    else:
        print("Are you speaking english buddy?")
        pick_option()
    return user_pick

# this will act as our function for the computer to choose
# they will random select a number 1-5
# these number are assinged to one of the five options the user has

def comp_option():
    comp_pick = randint(1,5)
    if comp_pick == 1:
        comp_pick = "r"
    if comp_pick == 2:
        comp_pick ="p"
    if comp_pick == 3:
        comp_pick = "sc"
    if comp_pick == 4:
        comp_pick = "l"
    if comp_pick == 5:
        comp_pick = "sp"
    return comp_pick

# this is where we begin our loop

while True:
    print("")
    user_pick = pick_option()
    comp_pick = comp_option()
    print("")
 
# here we first look at what the user picked
# then we compare it to what the computer picked and give the proper response
# after that we add to the win counter of either the computer or user


    if user_pick == "r":
        if comp_pick == "r":
            print("You chose rock. The computer chose rock. They bounce off each other. Its a tie.")
        elif comp_pick == "p":
            print("You chose rock. The computer chose paper. Paper covers rock. You lose.")
            comp_wins += 1
        elif comp_pick == "sc":
            print("You chose rock. The computer chose paper. Rock crushes paper. You win.")
            player_wins += 1
        elif comp_pick == "l":
            print("You chose rock. The computer chose lizard. Rock crushes lizard. You win.")
            player_wins += 1
        elif comp_pick == "sp":
            print("You chose rock. The computer chose Spock. Spock vaporizes Rock. You lose.")
            comp_wins += 1
    elif user_pick == "p":
        if comp_pick == "r":
            print("You chose paper. The computer chose Rock. Paper covers Rock. You win.")
            player_wins += 1
        elif comp_pick == "p":
            print("You chose paper. The computer chose paper. They cover eachother. You tie.")
        elif comp_pick == "sc":
            print("You chose paper. The computer chose scissors. Scissors cuts paper. You lose.")
            comp_wins += 1
        elif comp_pick == "l":
            print("You chose paper. The computer chose lizard. Lizard eats paper. You lose.")
            comp_wins += 1
        elif comp_pick == "sp":
            print("You chose paper. The computer chose spock. Paper disproves spock. You win.")
            player_wins += 1
    elif user_pick == "sc":
        if comp_pick == "r":
            print("You chose scissors. The computer chose Rock. Rock crushes scissors. You lose.")
            comp_wins += 1
        elif comp_pick == "p":
            print("You chose scissors. The computer chose Paper. Scissors cuts paper. You win.")
            player_wins += 1
        elif comp_pick == "sc":
            print("You chose scissors. The computer chose scissors. The scissors break each other. You tie.")
        elif comp_pick == "l":
            print("You chose scissors. The computer chose lizard. Scissors decapitates lizard. You win.")
            player_wins += 1
        elif comp_pick == "sp":
            print("You chose scissors. The computer chose spock. Spock smashes scissors. You lose.")
            comp_wins += 1
    elif user_pick == "l":
        if comp_pick == "r":
            print("You chose lizard. The computer chose rock. Rock crushes lizard. You lose.")
            comp_wins += 1
        elif comp_pick == "p":
            print("You chose lizard. The computer chose paper. Lizard eats paper. You win.")
            player_wins += 1
        elif comp_pick == "sc":
            print("You chose lizard. The computer chose scissors. Scissors decapitates lizard. You lose.")
            comp_wins += 1
        elif comp_pick == "l":
            print("You chose lizard. The computer chose lizard. The lizards lick each other. You tie.")
        elif comp_pick == "sp":
            print("You chose lizard. The computer chose spock. Lizard poisons spock. You win.")
            player_wins += 1
    elif user_pick == "sp":
        if comp_pick == "r":
            print("You chose spock. The computer chose rock. Spock vaporizes rock. You win.")
            player_wins += 1
        elif comp_pick == "p":
            print("You chose spock. The computer chose paper. Paper disproves spock. You lose.")
            comp_wins += 1
        elif comp_pick == "sc":
            print("You chose spock. The computer chose scissors. Spock smashes scissors. You win.")
            player_wins += 1
        elif comp_pick == "l":
            print("You chose spock. The computer chose lizard. Lizard poisons spock. You lose.")
            comp_wins += 1
        elif comp_pick == "sp":
            print("You chose spock. The computer chose spock. The spocks stare each other down. You tie.")

# if either the computer or the user wins the game you are showed two messages
# either you won or you lose
# this also sets the end game counter

    if int(player_wins) >= int(3): 
        endgame += 1
        print("")
        print("You win the match!")
        print("")
    if int(comp_wins) >= int(3):
        endgame += 1
        print("")
        print("The computer wins the match. You lose!")
        print("")

# once the endgame counter is hit 
# this triggers a question to ask if the user wants to play again
# if they select yes it will pass and restart the loop
# if they select no it will break the loop
# also an emergency else statement to break the loop

    if int(endgame) == int(1):
        user_pick = input("Do you want to play again (y/n)?")
        if user_pick in ["Y", "y", "yes", "Yes"]:
            pass
        elif user_pick in ["N", "n", "no", "No"]:
            break
        else:
            break
          

So the trouble I am having is that. The game works fine, once either the computer or the user reaches 3 wins we are greeted with a win or lose message. After that happen you are asked to play again. If you hit no the code ends. If you hit yes the game restart properly, but upon your first selection it take you back to the win or lose screen. It appears to be the win counters are not resetting or I have improper indents ?

Any help is appreciated.

PS: I know there is probably an easier way to make the game as well instead of using all those elif statements. Maybe with classes or something ? Im new though so this was the easiest for me.

A few side-note suggestions:

  • To check if a variable is one of a selection of strings, you can make it a bit more compact by doing for example:

     if user_pick.lower() in ["rock", "r"]:
  • No need to cast an int to an int :) int(1) == 1

  • endgame serves as a flag - Did the game end or not. Therefore it makes more sense to use a boolean - True / False - than an int.


Now your main problem is that your code sets the endgame variable according to player_wins and comp_wins . When you start a new game, those stay with their old values so immediately after one round the game is over again. Simply reset all critical variables in the last part of your code:

endgame = False
...
if player_wins >= 3: 
    endgame = True
...
if comp_wins >= 3:
    endgame = True
...
if endgame:
    user_pick = input("Do you want to play again (y/n)?")
    if user_pick.lower() in ["y", "yes"]:
        endgame = False
        player_wins = 0
        comp_wins = 0
    else:
        break

Bonus side-note suggestion:

Usually a repeating if / elif / else tree structure can be reduced. In other languages you have the switch statement - which kinda leaves the code looking the same just not with if/else . In Python you can usually transform most if trees to a selection from a dictionary. So your comp_option could be changed to:

def comp_option():
    comp_pick = randint(1,5)
    options = {1: 'r', 2: 'p', 3: 'sc', 4: 'l', 5: 'sp'}
    return options[comp_pick]

I think you have to add some additional statements that reset the in-game statistics when the user opts to restart the game. Something like:

 if int(endgame) == int(1):
        user_pick = input("Do you want to play again (y/n)?")
        if user_pick in ["Y", "y", "yes", "Yes"]:
            comp_wins = 0
            player_wins = 0
            endgame = 0
            continue

You found the problem! You have to reset the endgame count. For that, just put your endgame variable in the while True loop (at the beginning)

while True:

   endgame = 0

This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM