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?
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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?
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.
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!
Not the answer you are looking for? Search for more explanations.
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!"
Here's what gets returned:
Player 1? rock
Player 2? scissors
Traceback (most recent call last):
File "/Users/lweera/Desktop/Python MOOC/rps.py", line 23, in
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?
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.
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? ")
Thanks, everyone. I appreciate the help. It was indeed the space. Man, Python is sooo literal!
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.
I see. Thanks, rsmith. This whole field is new to me, so your insight reveals a lot. I'm unaccustomed to how computers "think."
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!