Ask your own question, for FREE!
Functions 21 Online
OpenStudy (anonymous):

I'm on exercise 2.1 of homework 2 for 6.189, intro to python. I don't know how to turn the rock paper scissors game into a function. I keep getting this error: "name 'rock' is not defined." Can someone give me a hint or an example of a possible solution?

OpenStudy (rsmith6559):

It sounds like rock isn't assigned a value before some code is trying to use rock's value.

OpenStudy (latinc):

the only thing you should have to change is the input into a function, like def game(x,y) and print to return, and then your program should run smooth.

OpenStudy (anonymous):

There are two parts to the Rock, Paper, Scissors code in the unit 1 assignment, part one is about getting user input, part two is about deciding the winner. If you place the 2nd part into a function, you could then call is using something like: def rps(player_1_input, player_2_input): To test your function, you can add test cases like this: print rps('rock','scissors') print rps('paper','paper') print rps('rock','paper') To make it into the "game" again, you would first ask for input from user #1 (rock, paper, or scissors) and the input from user #2 (rock, paper, or scissors)... assigning the input to variables player_1_input and plyaer_2_input respectively in this case. ... then, finally, you would call your function ... print rps(player_1_function, player_2_funciton) ... Don't forget, your function should RETURN the value of "Player 1 wins", "Player 2 wins", or "Tie". Hope this helps! :-)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!