Brinarose Gohde
Brinarose Gohde

Reputation: 1

why will it return 0 when i need to give me something else?

i built this program for school. its still in progress, but i hit a lump in the road and im about he tear my hair out.

ive already tried changing my variables, how they are set up, and its super annoying how nothing i can think of works. i cant get it to make my chart either, but its not my top priority.

def compare(u1, u2):
    if u1 == u2:
      return("It's a tie!")
      TIE = TIE +1

    elif u1 == 'rock':
        if u2 == 'scissors':
            return("{0} wins with Rock!".format(user1))
            u1WIN +1
        else:
            return("{0} wins with Paper!".format(user2))
            u2WIN +1
    elif u1 == 'scissors':
        if u2 == 'paper':
            return("{0} wins with scissors!".format(user1))
            u1WIN +1
        else:
            return("{0} wins with rock!".format(user2))
            u2WIN +1
    elif u1 == 'paper':
        if u2 == 'rock':
            return("{0} wins with paper!".format(user1))
            u1WIN +1
        else:
            return("{0} wins with scissors!".format(user2))
            u2WIN +1
    else:
        return("Invalid input! some one has not entered rock, paper or scissors, try again.")
        sys.exit()
#this is so frustrating! i cant get my variables to change!
#the winner should be getting points, but they arent. and its so annoying!! it always returns 0 no matter what. i am about this || close to giving up on it. 
x = 0
u1WIN = x
u2WIN = x
TIES = x
play = 3
while play >= 0:
  if play == 0:
    break
  else:

    user1_answer = getpass.getpass("{0}, do you want to choose rock, paper or scissors? ".format(user1))
    user2_answer = getpass.getpass("{0}, do you want to choose rock, paper or scissors? ".format(user2))
    print(compare(user1_answer, user2_answer))
    play = play +-1



print('player 1\'s wins', u1WIN)
print ('player 2\'s wins', u2WIN)
print('number of ties', TIES)
user_scores = [u1WIN, u2WIN, TIES]
wins = ['user1', 'user2', 'ties']
colors = ['blue', 'green', 'red']
plt.pie(user_scores, labels=wins, colors=colors, startangle=90, autopct='%.1f%%')
plt.show()

i expected to get an output that matches the amount of times won, unfortunately, it always comes out as 0

Upvotes: -2

Views: 79

Answers (3)

Patrick Artner
Patrick Artner

Reputation: 51673

After fixing your errors (globals, returns, adding) - see my 1st answer, you could restructure your code a bit and use some of python features:

Example of how to structure the game:

from enum import IntEnum
from sys import platform
import os

if platform.startswith("linux"):
    clear = lambda: os.system('clear')
else:
    clear = lambda: os.system('cls')

class G(IntEnum):
    """Represents our states in a meaningful way"""
    Rock = 1
    Scissor = 2
    Paper = 3 
    QUIT = 4

def get_gesture(who):
    """Gets input, returns one of G's "state" enums.
    Loops until 1,2,3 or a non-int() are provided."""
    g = None
    while True:
        choice = input(who + " pick one: Rock (1) - Scissor (2) - Paper (3): ")
        clear()
        try:
            g = int(choice)
            if g == G.Rock:
                return G.Rock
            elif g == G.Scissor:
                return G.Scissor
            elif g == G.Paper:
                return G.Paper
        except:
            return G.QUIT

def check(one,two):
    """Prints a message and returns: 
    0 if player 1 won,     1 if player 2 won,    2 if it was a draw"""
    if (one, two) in {(1,2) , (2,3) , (3,1)}:
        print("Player 1 wins with {} against {}".format(one.name, two.name)) 
        return 0
    elif (two, one) in {(1,2) , (2,3) , (3,1)}:
        print("Player 2 wins with {} against {}".format(two.name, one.name))
        return 1
    else:
        print("Draw: ", one.name, two.name)
        return 2

Game:

player1 = None
player2 = None
total = [0,0,0]   # P1 / P2 / draws counter in a list

while True:
    player1 = get_gesture("Player 1")
    if player1 != G.QUIT:
        player2 = get_gesture("Player 2")
    if G.QUIT in (player1,player2):
        print("You quit.")
        break

    # the result (0,1,2) of check is used to increment the total score index
    total[ check(player1, player2) ] += 1

print("Player 1 wins: ",total[0])
print("Player 2 wins: ",total[1])
print("Draws:         ",total[2])

Output:

Player 1 pick one: Rock (1) - Scissor (2) - Paper (3): 1
Player 2 pick one: Rock (1) - Scissor (2) - Paper (3): 1
Draw:  Rock Rock

Player 1 pick one: Rock (1) - Scissor (2) - Paper (3): 2
Player 2 pick one: Rock (1) - Scissor (2) - Paper (3): 2
Draw:  Scissor Scissor

Player 1 pick one: Rock (1) - Scissor (2) - Paper (3): 3
Player 2 pick one: Rock (1) - Scissor (2) - Paper (3): 3
Draw:  Paper Paper

Player 1 pick one: Rock (1) - Scissor (2) - Paper (3): 1
Player 2 pick one: Rock (1) - Scissor (2) - Paper (3): 2
Player 1 wins with Rock against Scissor

Player 1 pick one: Rock (1) - Scissor (2) - Paper (3): Q
You quit.

Player 1 wins:  1
Player 2 wins:  0
Draws:          3

Upvotes: 0

Patrick Artner
Patrick Artner

Reputation: 51673

Problems:

You use global variables without providing them or returning them from your function

def compare(u1, u2): 
    if u1 == u2:
      return("It's a tie!")
      TIE = TIE +1     # this adds one to the _global_ TIE's value  and stores it into 
                       # a _local_ TIE: your global is not modified at all

You do stuff after you return from a function - return == Done - nothing after it will ever happen

    if u2 == 'scissors':
        return("{0} wins with Rock!".format(user1))
        u1WIN +1              # wont ever happen

You add to variables without storing the value back:

u1WIN +1              # adds one and forgets it immerdiately because you do not reassign.

Do not use globals:

def compare(u1,u2):
    """Returns 0 if u1 and u2 equal, returns 1 if u1 > u2 else -1"""
    if u1 == u2:
        return 0
    elif u1 > u2: 
        return 1
    else:
        return -1 

and handle your results in your main program ... or

def compare(u1,u2):
    """Returns (0,1,0) if u1 and u2 equal, returns (1,0,0) if u1 > u2 else (0,0,1)"""
    return (u1 > u2, u1 == u2, u1 < u2) # True == 1, False == 0

or plenty of other possibilites.

Upvotes: 1

Everett
Everett

Reputation: 9608

It looks like you need to ASSIGN the results of your calculations.

This performs the calculation, but the result is not assigned and stored.

u1WIN +1

Probably what you meant was this:

u1WIN = u1WIN +1

Upvotes: 1

Related Questions