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):
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.