Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

LBWicket

  • 2 years ago

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?

  • This Question is Closed
  1. Daeruin
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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?

  2. nickgfly
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  3. LBWicket
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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!

  4. LBWicket
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  5. rsmith6559
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  6. Daeruin
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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? ")

  7. LBWicket
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  8. rsmith6559
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  9. LBWicket
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  10. rsmith6559
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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!

  11. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.