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

I'm working on hw_1, ex 1.7 "Rock, Paper, Scissors". I realize I'm missing something in my code and I'm sure it must have something to do with the raw_input function. 1) How do I create the relationship between player1 and rock? 2) Where does the raw_input function go?

OpenStudy (anonymous):

I didn't keep very good notes so I'm not sure if I'm thinking of the same exercise. But if it's the one I'm thinking of, you have to get input from player 1 and then check to see if the input is a valid choice (either rock, paper, or scissors). You create the relationship between player1 and rock simply by assigning the results of the raw_input function to a variable that represents player1's choice, like this: player1 = raw_input("What is your selection? ") Python will display "What is your selection? " and waits for user input. Whatever the user enters will be assigned to the variable "player1". Then you check to see whether the value of "player1" is one of the three allowed choices. Does that help?

OpenStudy (anonymous):

I work with java, but I think I understand what your trying to do. How my pseudo-code would look for this program: 1.) Ask the user to choose from the three options and type it. 2.) Use a loop to check that the user typed in one of the three options 3.) Assign player1 a value for whatever object they chose (e.g. 1 for scissors, 2 for rock, 3 for paper) 4.) use a method to make a random number (in this case I dont know what the method is for python). If your method makes random numbers between 0 and 1, then do something like this: if (randNum< .5) then computer = 2 (rock). if (randNum >= .5) then computer = 3 (paper). 5.) use if statements to decide what happens and display either: " you win!" or "you loose". Example, if ((player1 = 1) and (computer = 3)) then print "you win!". Sorry if you dont understand because I talked in java terms, but I tried to write descriptions of what the code should do in each step, not specific syntax. So, yeah, and if you wanted to make the game keep going until the user did not want to play anymore, then I would use a loop to ask them if they wanted to play after every round.

OpenStudy (anonymous):

Thanks, Daeruin. That helps me get a lot further along. Nickgfly, I don't know any code but this extremely rudimentary level of Python, so I can't really follow your suggestion. I appreciate the feedback nevertheless!

OpenStudy (anonymous):

I have a new question on this homework problem. (I hope this is the correct use of this space!) Here is my code: player1 = raw_input("Player 1?") player2 = raw_input("Player 2?") if player1 == "rock": if player2 == "scissors": winner = 1 elif player2 == "paper": winner = 2 if player1 != player2: print "Player", winner,"wins!" else: print "Tie" Here's what gets returned: Player 1? rock Player 2? scissors Player Traceback (most recent call last): File "/Users/lweera/Desktop/Python MOOC/rps.py", line 23, in <module> print "Player", winner,"wins!" NameError: name 'winner' is not defined I thought I'd defined "winner" in my first loop. What's going wrong here?

OpenStudy (rsmith6559):

In your output, it looks like there's a space between the question mark and the answer to your raw_input(). If the user is answering " rock" as opposed to "rock", it won't match "rock" in your if statements.

OpenStudy (anonymous):

I think rsmith is right. When you're entering player one's choice, if you type a space, the space will be part of the string. If you want a space after the question mark to make the output look pretty, put it inside the quotation marks in the raw_input function, like this: player1 = raw_input("Player 1? ")

OpenStudy (anonymous):

Thanks, everyone. I appreciate the help. It was indeed the space. Man, Python is sooo literal!

OpenStudy (rsmith6559):

It's not Python that's soo literal. it's computers in general. Firefox and FileZilla have a bad time with spaces at the end of filenames, too. Down at the execution level, computers aren't too bright. The world is nothing but voltage/no voltage pulses. A string is a series of characters, which most encodings represent as one byte, which is 8 pulses in parallel. The same pattern of pulses could be a letter or an instruction or an integer or a floating point (scientific notation in binary) number. It depends on the context. Actually, the computer is too dumb to understand context, it depends on which order the bytes are in. When I was in a bad mood, I used to have my Commodore 64 execute a text string. Usually crashed in a couple of milliseconds.

OpenStudy (anonymous):

I see. Thanks, rsmith. This whole field is new to me, so your insight reveals a lot. I'm unaccustomed to how computers "think."

OpenStudy (rsmith6559):

That's the point. Computers can't think. They're absolute slaves to what a programmer tells them to do, innocent, stupid or malicious. As if just explaining something to a piece of sand isn't enough fun, you have to guard against user stupidity and protect against malice too. Lot's of fun!

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!