redrackham redrackham - 2 months ago 9
Python Question

Python Newbie - Rock, Paper, Scissors

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

Answer

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
Comments