scissors paper stone game without the use of list in python

Viewed 23

im new to this python program and was tasked to generate a game or scissors paper stone game in python without the use of a list. i have the function here as:

def getRandomShape():

    Shape = random.randint(1, 3)
    if Shape == 1:
        print('Scissors'.upper())
    elif Shape == 2:
        print('Stone'.upper())
    else: 
        print('Paper'.upper())
    
getRandomShape()

but whenever i call for the function, it says its not defined. the error occurse at the possibleHands section where im unable to call for the function but able to at the bottom for the checkForWinner function call

here's the full program.

import random

print('welcome to scissors paper stone')

cpuScore = 0
playerScore = 0
tieScore = 0
possibleHands = getRandomShape(computerHand)


def getRandomShape(computerHand):

    Shape = random.randint(1, 3)
    if Shape == 1:
        print('Scissors'.upper())
    elif Shape == 2:
        print('Stone'.upper())
    else: 
        print('Paper'.upper())
    

def checkForWinner(playerHand, computerHand):
    if(playerHand == 'Stone' and computerHand == 'Paper'):
        print('you lost')
        return 'cpu'
    elif(playerHand == 'Stone' and computerHand == 'Scissors'):
        print('you won')
        return 'player'
    elif(playerHand == 'Scissors' and computerHand == 'Paper'):
        print('you won')
        return 'player'
    elif(playerHand == 'Scissors' and computerHand == 'Stone'):
        print('you lost')
        return 'cpu'
    elif(playerHand == 'Paper' and computerHand == 'Scissors'):
        print('you lost')
        return 'cpu'
    elif(playerHand == 'Paper' and computerHand == 'Stone'):
        print('you won')
        return 'player'
    else:
        print('its a tie. play again')
        return 'tie'


while(playerScore != 3 and cpuScore != 3):
    name = input('Please enter your name: ')
    while True:
        playerHand = (input('Round 1: '+str(name)+ ', please choose a shape:'))
        if(playerHand == 'Scissors' or playerHand == 'Paper' or playerHand == 'Stone'):
           break
        else:
            print('invalid input, case sensitive. Try again')

    computerHand = random.choice(possibleHands)
    print('your Hand: ', playerHand)
    print('cpu Hand: ', computerHand)
    results = checkForWinner(playerHand, computerHand)
    if(results == 'player'):
        playerScore += 1
    elif(results == 'cpu'):
        cpuScore += 1
    else:
        tieScore += 1
    print('your score: ', playerScore, 'CPU: ', cpuScore, 'Ties: ', tieScore)


print('gg game over')

got this from a youtube tutorial

1 Answers

You must declare the function before its usage. On line 8 getRandomShape is called before its definition, so a NameError would occur.

What you can do is to put all the code that is not in a function, other than the import statements inside a main() function. And at the end you add:

if __name__ == ‘__main__’:
    main()

When the program is called it would execute the main() function, and it would not matter if you put the function before or after others. The only thing that matters is you only call functions after they are already defined.

You can learn more about it here.

There are many more problems with this code, and this only fixes the NameError that you are currently facing.

Related