1

Adding a number to the variable isn't working? (This is once again the rock paper scissors program.)

    import random
hscore = 0
cscore = 0
tries = 0


#computer choice
rock = ("rock")
paper = ("paper")
scissors= ("scissors")
rps = (rock, paper, scissors)

#human wins
def humanfunction():
    hscore +=1
    if choice == "rock":
        if cchoice == scissors:
            hscore +=1
            print("Human wins this round.")
    if choice == "scissors":
        if cchoice == paper:
            hscore +=1
            print("Human wins this round.")
    if choice == "paper":
        if cchoice == rock:
            hscore +=1
            print("Human wins this round.")
def computerwin():

#computer wins
    cscore +=1
    if cchoice == "rock":
        if choice == scissors:
            cscore +=1
            print("Computer wins this round.")
    if cchoice == "scissors":
        if choice == paper:
            cscore +=1
            print("Computer wins this round.")
    if cchoice == "paper":
        if choice == rock:
            cscore +=1
            print("Computer wins this round.")
def tie():

    if cchoice == choice:          
        print("It's a tie!")

#choosing
while 0 == 0:

    choice = input("\nWhat do you choose? <rock, paper, scissors>: ")
    tries +=1
    cchoice = random.choice(rps)
    humanfunction()
    computerwin()
    tie()
    print(hscore, cscore)
    print("Human choice: ",choice)
    print("Computer choice: ",cchoice)
    print("Finished game number", tries)
    if tries == 10:
        print("limit reached")
        break

When I run the code it shows this (ignore the error beforehand):

enter image description here

The human chose paper and the computer chose rock, which means the human beats the computer--but the score for the human didn't increase the next game. Functions have been messing up my code super badly for some reason.

The hscore is there again in the function because without it, the output will say something about a local variable being reference beforehand. (This is a class assignment and we cannot use things we haven't gone over yet.) So adding a global would not help. The choices are defined in the code later on. I just didn't include it as I wanted to just show the part where the variable is being added to.

nashishi
  • 21
  • 3

2 Answers2

0

variable hscore gets reinitialized to 0 for every function call that's making it global will bring you desired result, same is applicable for cscore.

make hscore and cscore global as follows:

import random
hscore = 0
cscore = 0
tries = 0


#computer choice
rock = ("rock")
paper = ("paper")
scissors= ("scissors")
rps = (rock, paper, scissors)

#human wins
def humanfunction():
    global hscore
    hscore +=1
    if choice == "rock":
        if cchoice == scissors:
            hscore +=1
            print("Human wins this round.")
    if choice == "scissors":
        if cchoice == paper:
            hscore +=1
            print("Human wins this round.")
    if choice == "paper":
        if cchoice == rock:
            hscore +=1
            print("Human wins this round.")

def computerwin():
#computer wins
    global cscore
    cscore +=1
    if cchoice == "rock":
        if choice == scissors:
            cscore +=1
            print("Computer wins this round.")
    if cchoice == "scissors":
        if choice == paper:
            cscore +=1
            print("Computer wins this round.")
    if cchoice == "paper":
        if choice == rock:
            cscore +=1
            print("Computer wins this round.")
def tie():

    if cchoice == choice:          
        print("It's a tie!")

#choosing
while 0 == 0:
    choice = input("\nWhat do you choose? <rock, paper, scissors>: ")
    tries +=1
    cchoice = random.choice(rps)
    humanfunction()
    computerwin()
    tie()
    print(hscore, cscore)
    print("Human choice: ",choice)
    print("Computer choice: ",cchoice)
    print("Finished game number", tries)
    if tries == 10:
        print("limit reached")
        break

output:

What do you choose? <rock, paper, scissors>: 0
(1, 1)
('Human choice: ', 0)
('Computer choice: ', 'rock')
('Finished game number', 1)

What do you choose? <rock, paper, scissors>: 1
(2, 2)
('Human choice: ', 1)
('Computer choice: ', 'scissors')
('Finished game number', 2)

What do you choose? <rock, paper, scissors>: 0
(3, 3)
('Human choice: ', 0)
('Computer choice: ', 'scissors')
('Finished game number', 3)

What do you choose? <rock, paper, scissors>: 0
(4, 4)
('Human choice: ', 0)
('Computer choice: ', 'paper')
('Finished game number', 4)

What do you choose? <rock, paper, scissors>: 2
(5, 5)
('Human choice: ', 2)
('Computer choice: ', 'paper')
('Finished game number', 5)

What do you choose? <rock, paper, scissors>: 2
(6, 6)
('Human choice: ', 2)
('Computer choice: ', 'rock')
('Finished game number', 6)
Mahesh Karia
  • 2,045
  • 1
  • 12
  • 23
0

The current and potential issues with your code are numerous, so perhaps the best answer to this question would be to prescribe a general approach to this problem that perhaps will give you insight to how your Python programs should be structured in general.

The main loop of your function is fine, but it's expecting functions to alter the general state of the program. This is not what function should usually do (except in rare circumstances). Instead, a function should take arguments and return a result. In this case, you want a function which takes two arguments - the player's choice and the computer's choice - and returns who wins.

Consider the following pseudo-code:

function judge_game: 
    arguments: player choice, computer choice
    return: 'tie' or 'player' or 'computer'

initialize variables for tries and scores

while tries are sufficiently small
    get player choice
    get computer choice

    if judge_game returns 'player' then increment player score
    if judge_game returns 'computer' then increment computer score

    # carry on with remaining logic

If you re-approach the problem with this skeleton in mind, you will likely find that most of your problems with variables and scope have disappeared.

Jared Goguen
  • 8,772
  • 2
  • 18
  • 36