3
votes

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.')
4
Your playerChoose and computerChoose functions don't return anything. - JJJ
i.o.w. return, but what? - Jacob Vlijm
Add 'return playerInput ' in playerChoose() instead of only return. - Dinesh Pundkar
Add ' return computerPick ' in computerChoose() instead of return. - Dinesh Pundkar
@redrackham - Please check the updated the code. I have updated to calculate the number of game won by player - Dinesh Pundkar

4 Answers

2
votes

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.')
2
votes

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
2
votes

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

1
votes

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