3

I'm very new to Python and decided to set myself a challenge of programming a Rock, Paper, Scissors game without copying someone else's code. However, I need help from a Pythonista grown-up!

I've seen many other variations on Rock, Paper, Scissors, on here but nothing to explain why my version isn't working. My program basically follows this format: set empty variables at start, define 4 functions that prints intro text, receives player input, randomly picks the computer's choice, then assesses whether its a win or a loss for the player.

This is all then stuck in a while loop that breaks once the player selects that they don't want to play anymore. (This bit is working fine)

However, whenever I run the code, it just always gives a draw and doesn't seem to store any data for the computer's choice function call. Does anybody know what I'm doing wrong?

Many thanks!

import random

playerAnswer = ''
computerAnswer = ''
winsTotal = 0
timesPlayed = 0

def showIntroText():
    print('Time to play Rock, Paper, Scissors.')
    print('Type in your choice below:')

def playerChoose():
    playerInput = input()
    return

def computerChoose():
    randomNumber = random.randint(1, 3)
    if randomNumber == 1:
        computerPick = 'Paper'
    elif randomNumber == 2:
        computerPick = 'Scissors'
    else:
        computerPick = 'Rock'
    return

def assessResult():
    if playerAnswer == computerAnswer:
        print('Draw!')
    elif playerAnswer == 'Rock' and computerAnswer == 'Paper':
        print('Paper beats Rock. You lose!')
    elif playerAnswer == 'Paper' and computerAnswer == 'Scissors':
        print('Scissors cuts Paper. You lose!')
    elif playerAnswer == 'Scissors' and computerAnswer == 'Rock':
        print('Rock blunts Scissors. You lose!')
    else:
        print('You win!')
        winsTotal += 1
    return

while True:
    timesPlayed += 1

    showIntroText()

    playerAnswer = playerChoose()
    computerAnswer = computerChoose()

    assessResult()

    print('Do you want to play again? (y/n)')
    playAgain = input()
    if playAgain == 'n':
        break

print('Thank you for playing! You played ' + str(timesPlayed) + ' games.')
kimnorth
  • 47
  • 7

4 Answers4

2

You have missed returning values in most of the case.

** Add 'return playerInput ' in playerChoose() instead of only return.

** Add ' return computerPick ' in computerChoose() instead of return.

** Initialize winsTotal variable before using it as 'winsTotal = 0' in assessResult().

** Variables you have intialized at the start of program are out of scope for functions.

Please check this StackOverFlow link for understanding scope of variables in python.

** Add 'return winsTotal' in assessResult() instead of return.

import random

def showIntroText():
    print('Time to play Rock, Paper, Scissors.')
    print('Type in your choice below:')

def playerChoose():
    playerInput = input()
    return playerInput

def computerChoose():
    randomNumber = random.randint(1, 3)
    if randomNumber == 1:
        computerPick = 'Paper'
    elif randomNumber == 2:
        computerPick = 'Scissors'
    else:
        computerPick = 'Rock'
    return computerPick

def assessResult(winsTotal):
    if playerAnswer == computerAnswer:
        print('Draw!')
    elif playerAnswer == 'Rock' and computerAnswer == 'Paper':
        print('Paper beats Rock. You lose!')
    elif playerAnswer == 'Paper' and computerAnswer == 'Scissors':
        print('Scissors cuts Paper. You lose!')
    elif playerAnswer == 'Scissors' and computerAnswer == 'Rock':
        print('Rock blunts Scissors. You lose!')
    else:
        print('You win!')
        winsTotal += 1
    return winsTotal


total_win = 0
while True:

    timesPlayed += 1

    showIntroText()

    playerAnswer = playerChoose()
    computerAnswer = computerChoose()

    total_win = assessResult(total_win)

    print('Do you want to play again? (y/n)')
    playAgain = input()
    if playAgain == 'n':
        break

print('Thank you for playing! You played ' + str(timesPlayed) + ' games.' + 'Out of which you won '+ str(total_win))

Output:

   C:\Users\dinesh_pundkar\Desktop>python c.py
Time to play Rock, Paper, Scissors.
Type in your choice below:
"Rock"
You win!
Do you want to play again? (y/n)
"y"
Time to play Rock, Paper, Scissors.
Type in your choice below:
"Rock"
Draw!
Do you want to play again? (y/n)
"y"
Time to play Rock, Paper, Scissors.
Type in your choice below:
"Rock"
Paper beats Rock. You lose!
Do you want to play again? (y/n)
"y"
Time to play Rock, Paper, Scissors.
Type in your choice below:
"Rock"
Paper beats Rock. You lose!
Do you want to play again? (y/n)
"n"
Thank you for playing! You played 4 games.Out of which you won 1
Community
  • 1
  • 1
Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
  • 1
    Thank you, that's very useful as it subsequently turned up an error about initializing the winTotal variable which I was struggling to solve. – kimnorth Sep 13 '16 at 16:19
  • 1
    Although, it only seemed to work when I initialized the "timesPlayed = 0" variable at the start. Otherwise it came up with an error when running. – kimnorth Sep 13 '16 at 16:26
2

It is always a draw because you aren't returning the answers from your function, both playerAnswer and computerAnswer return None

Gareth Miller
  • 332
  • 1
  • 3
  • 10
2

add input and return in your functions

def computerChoose And def assessResultreturn None

for Example by this code you can play this game :

import random

playerAnswer = ''
computerAnswer = ''
winsTotal = 0
timesPlayed = 0


def playerChoose():
    playerInput = input("insert:")
    return playerInput


def computerChoose():
    randomNumber = random.randint(1, 3)
    if randomNumber == 1:
        computerPick = 'Paper'
    elif randomNumber == 2:
        computerPick = 'Scissors'
    else:
        computerPick = 'Rock'
    return computerPick


def assessResult(playerAnswer, computerAnswer):
    if playerAnswer == computerAnswer:
        print('Draw!')
    elif playerAnswer == 'Rock' and computerAnswer == 'Paper':
        print('Paper beats Rock. You lose!')
    elif playerAnswer == 'Paper' and computerAnswer == 'Scissors':
        print('Scissors cuts Paper. You lose!')
    elif playerAnswer == 'Scissors' and computerAnswer == 'Rock':
        print('Rock blunts Scissors. You lose!')
    else:
        print('You win!')
    return


while True:
    timesPlayed += 1

    playerAnswer = playerChoose()
    computerAnswer = computerChoose()

    assessResult(playerAnswer,computerAnswer)

    print('Do you want to play again? (y/n)')
    playAgain = input()
    if playAgain == 'n':
        break

print('Thank you for playing! You played ' + str(timesPlayed) + ' games.')
Mikail Land
  • 269
  • 3
  • 9
1

As some of people said playerChoose() and computerChoose() return with None

Modifidy these statement playerChoose() -> return playerInput and computerChoose() -> return computerPick

AS well as you have to use global variable. Insert this row

global winsTotal

in the assessResult().

Attila
  • 51
  • 3
  • Please check this link regarding use of global variables - http://stackoverflow.com/questions/19158339/why-are-global-variables-evil – Dinesh Pundkar Sep 13 '16 at 10:42