0

It's my first time here and I'm kinda panicking. I have an assignment to code Rock, Paper, Scissors in Python. (I use Python 3.1) and for some reason the function is not running...?

Here's the code:

hscore = 0
cscore = 0
tries = 0
#computer choice
rock = ("rock")
paper = ("paper")
scissors = ("scissors")
rps = (rock, paper, scissors)
cchoice = random.choice(rps)
choice = input("\nWhat do you choose? <rock, paper, scissors>: ")
tries +=1
def humanfunction():
 if choice == "rock":
 if cchoice == scissors:
 print("Human wins this round.")
 hscore +=1
 if choice == "scissors":
 if cchoice == paper:
 print("Human wins this round.")
 hscore +=1
 if choice == "paper":
 if cchoice == rock:
 print("Human wins this round.")
 hscore +=1
 if cchoice == choice:
 print("Tie.")
def compfunction():
 if cchoice == scissors:
 if choice == "paper":
 print("Computer wins this round.")
 cscore +=1
 if cchoice == rock:
 if choice == "scissors":
 print("Computer wins this round.")
 cscore +=1
 if cchoice == paper:
 if choice == "rock":
 print("Computer wins this rounud.")
 cscore +=1
 if cchoice == choice:
 print("Tie.")
def choose():
 tries = 0
 while 0 == 0:
 choice = input("\nWhat do you choose? <rock, paper, scissors>: ")
 tries +=1
 print("\nHuman choice: ", choice)
 print("Computer choice: ", cchoice)
 print("Finished game number", tries)
 if tries == 10:
 print("Limit reached!")
 break
humanfunction()
compfunction()
choose()

I've been trying to solve this for days now and for some reason, when I run the code, it doesn't show up who won. Help would be appreciated <3

EDIT: here's what i get when i run the code: output

the program is actually supposed to show this: output2

asked Dec 20, 2017 at 23:17
4
  • 1
    Could you post your error message or your output from this program? Commented Dec 20, 2017 at 23:18
  • docs.python.org/2.4/lib/truth.html - read it, remember it. Several *things* in python are considered False - like any kind of empty set, list, dict or ...... 0 Commented Dec 20, 2017 at 23:20
  • Many things: 1) You call the human/comp functions once, rather than in your loop. 2) choice in the choose() function will refer to a local variable and not overwrite the global... Start with a program that has simpler logic (e.g. guess a number), get that flow working, then move the RPS logic back. Commented Dec 20, 2017 at 23:33
  • 2
    If your program does not what you expect, there are generally 2 options: 1. use a debugger and go through it line by line. 2. cut it down until you understand what it does. Commented Dec 20, 2017 at 23:33

1 Answer 1

3

Here is my take on your code.

The main reason it wasn't running is that your cscore variable was being referenced before it was initialized, because you had setup cscore as a global variable, but didn't declare it as a global variable in your function.

You also needed to import the random library

Also instead of doing 4 if/then statements I combined them into 1 if/then statement

EDIT: cleaned up the code a little more

EDIT 2: no more globals, avoid globals if possible

import random 
def get_human_choice():
 valid_choice = False
 while not valid_choice:
 choice = input("\nWhat do you choose? <rock, paper, scissors>: ")
 if choice == 'rock' or 'paper' or 'scissors':
 valid_choice = True
 return choice 
def get_comp_choice():
 rps = ('rock', 'paper', 'scissors')
 comp_choice = random.choice(rps)
 return comp_choice 
def human_winner(comp_choice):
 print("The computer chooses: %s" % comp_choice)
 print("Human wins this round.") 
def comp_winner(comp_choice):
 print("The computer chooses: %s" % comp_choice)
 print("Computer wins this round.") 
def stats(attempts, human_score, comp_scored, tie_score):
 print("Finished game number: %s" % attempts)
 print('Human Score: %s' % human_score)
 print('Computer Score: %s' % comp_scored)
 print('Ties: %s' % tie_score) 
def play_game(human_score, comp_score, tie_score):
 choice = get_human_choice()
 comp_choice = get_comp_choice() 
 if choice == 'rock':
 if comp_choice == 'scissors':
 human_winner(comp_choice)
 human_score += 1
 else:
 comp_winner(comp_choice)
 comp_score += 1
 elif choice == 'scissors':
 if comp_choice == 'paper':
 human_winner(comp_choice)
 human_score += 1
 else:
 comp_winner(comp_choice)
 comp_score += 1
 elif choice == 'paper':
 if comp_choice == 'rock':
 human_winner(comp_choice)
 human_score += 1
 else:
 comp_winner(comp_choice)
 comp_score += 1
 elif choice == comp_choice:
 print("Tie.")
 tie_score += 1 
 return human_score, comp_score, tie_score 
if __name__ == '__main__':
 tries = 1
 h_score, c_score, t_score = 0, 0, 0
 while tries <= 10:
 h_score, c_score, t_score = play_game(h_score, c_score, t_score) 
 if tries == 10:
 print("\nLimit reached!\n")
 stats(tries, h_score, c_score, t_score)
 break
 else:
 tries += 1
answered Dec 20, 2017 at 23:41
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't really work as intended since you are only letting the computer make a choice once, plus encouraging a beginner to make excessive usage of globals is promoting bad habits, ultimately doing more harm than good.
heck thank you! unfortunately this is an assignment for school and we haven't gone over the making something global thing yet ^^;;; (its frustrating lol) is there any other way? if not, thank you for your code nonetheless!
Updated without globals, and the computers choice changes every round.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.