简体   繁体   中英

Python Rock Paper Scissors game

I am in a beginning programming class and was assigned a "Rock, Paper Scissors" game to write. The instructors stipulated that the functions be named as I have them, and that the game loop with no way to end without closing the program. For some reason, when I run this code I am told that the "randomnumber" is not defined. What am I doing wrong? Thanks

import random
keepgoing = 1

def main():
    get_computermove()
    get_playermove()
    calculatewinner()

def get_computermove():
    computermove = random.randint(1, 3)
    randomnumber = computermove
    return randomnumber

def get_playermove():
    playermove = input('Please pick rock, paper, or scissors. ')
    return playermove

def calculatewinner():
    if randomumber == 1 and playermove == "rock":
        print('It is a tie!')
    elif randomnumber == 2 and playermove == "paper":
        print('It is a tie!')
    elif randomnumber == 3 and playermove == "scissors":
        print('It is a tie!')
    elif randomnumber == "1" and playermove == "Paper":
        print('Paper covers rock. You win!')
    elif randomnumber == "1" and playermove == "scissors":
        print('Rock breaks scissors. You lose.')
    elif randomnumber == "2" and playermove == "Rock":
        print('Paper covers rock. You lose.')
    elif randomnumber == "2" and playermove == "Scissors":
        print('Scissors cuts paper. You win!')
    elif randomnumber == "3" and playermove == "Rock":
        print('Rock breaks scissors. You win!')
    elif randomnumber == "3" and playermove == "Paper":
        print('Scissors cuts paper. You lose.')

while keepgoing == 1:
    main()

You have created two randomnumber variables: one inside get_computermove , the other inside calculate_winner . The second one has no value defined. The first one disappears as soon as you leave the function. Instead, try this: save the return value from each function, and pass those into the final routine.

def main():
    computermove = get_computermove()
    playermove = get_playermove()
    calculatewinner(computermove, playermove)

...
def calculatewinner(randomnumber, playermove):

You can do a little better with variable names, but this should keep the rest of your code intact.

That's a great way to approach this problem, but you need to be aware that anything returned in Python is not stored as a global variable, unless you explicitly do so. Whenever you call get_computermove() function, it does return a 'randomnumber', but it gets lost since you do not store it anywhere. Also, you would need to turn it in to the calculatewinner() function as one of the arguments for it to be accessible inside the function.

On a side note, the first line in your code has 'randomnumber' misspelled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM