Ask your own question, for FREE!
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW) 21 Online
OpenStudy (anonymous):

Exercise 1.7 - Rock, Paper, Scissors: raw_input('Player_1?') raw_input('Player_2?') P_1 = 'Player_1' P_2 = 'Player_2' if P_1 == 'rock' and P_2 == 'rock': print 'Tie.' if P_1 == 'rock' and P_2 == 'scissors': print 'Player 1 wins.' if P_1 == 'rock' and P_2 == 'paper': print 'Player 2 wins.' When I run it and enter the choices, it doesn't print anything. Can anyone tell me what I'm doing wrong?

OpenStudy (anonymous):

I think you need to use () in your if statement, like this: if (P_1 == 'rock' and P_2 == 'rock'): print 'Tie.'

OpenStudy (anonymous):

That was how I had it in the first place, then thought that may have been why it wasn't printing anything, but even with the parentheses, it still doesn't print anything at all. You think maybe I need to be using elif and else, instead of just using the if statement over and over?

OpenStudy (anonymous):

I just started so forgive me if i am wrong, but i think your 2 variables P_1 and P_2 are never equal to the string 'rock', 'paper', or 'scissors' so your if statements never run. I would save the input directly to a variable and try it again. P_1 = raw_input('Player1?')

OpenStudy (anonymous):

That causes it to continuously ask for the input over and over without displaying the outcome. I appreciate the effort though.

OpenStudy (anonymous):

Not sure what would be causing that then. I attached my work, probably much more code than it should be, but i hope it helps.

OpenStudy (anonymous):

Ahaaa. No. No. No. That's perfect. If I were to continue writing mine as is, mine would be way too much code. My problem was either the fact that I didn't assign P_1 and P_2 to the raw_inputs or the fact that within my raw_inputs, I didn't add the spaces at the end of them. I had raw_input('Player_1?') instead of raw_input(Player_1? ') I'm not entirely sure. But your work actually helped me out a lot. Thank you, kind sir.

OpenStudy (anonymous):

no problem, glad i could help. about the space at the end of the input string, i added them for purely cosmetic reasons, i don't think they have any effect on the code. Looking back at your code i can see that you assigned P_1 = 'Player_1' right before your if statements, thus P_1 would never be rock/paper/scissors. Getting rid of those 2 lines of code and assigning the variable directly to your raw_inputs would have you working.

OpenStudy (anonymous):

That's exactly what it was, and exactly what I did. You actually just answered my last question that I ended up closing, because I guess I didn't word it very well. You got yourself a medal and a new fan :) Thanks again.

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!