0

I have looked at the other posts about this topic but still can not find what I'm doing wrong at the beginning. Instead of rock, paper, and scissors, I am using python, ruby, and java. It is not close to being done yet. I'm not into the if loops yet for, but if the user inputs something different then "python", "ruby", or Java", I want it too print "The game is over". I get an error saying the string i entered is not defined. Could someone guide me in the direction I need to go? I think I'm confused when comparing userInput to gameList, since gameList is a list.

import random
def pythonRubyJava():
    gameList = ["python","ruby","java"]
    userInput = input("python, ruby, or java?:")
    randomInput = random.choice(gameList)
    if userInput != gameList:
        print "The game is over"

I got that part figured out. Do I need to store "python", "ruby", and "java" as variables to continue now? Or where would you go?

import random
def pythonRubyJava():
    gameList = ["python","ruby","java"]
    userInput = raw_input("python, ruby, or java?:")
    randomInput = random.choice(gameList)
    print randomInput
    if userInput not in gameList:
        print "The game is over"
    if userInput == "python" and randomInput == "python":
        print "stalemate"
    if userInput == "ruby" and randomInput == "ruby":
        print "stalemate"
    if userInput == "java" and randomInput == "java":
        print "stalemate"        

Instead of getting the same answer, I want to be able to run the game again and not have it print the stalemate to end the game, just start over. I know I would have to delete "print "stalemate"" but I just wanted to show that.

5
  • You would want to compare userInput to randomChoice, not the whole list, right? Though the comparison needs to be more complex to be like RPS. Commented Sep 25, 2013 at 22:58
  • 2
    Plus, I believe if Java beats either Python or Ruby, the universe will raise a LogicError, which will make it hard to write the RPS code. :) Commented Sep 25, 2013 at 23:17
  • Do i need to store the 3 strings as variables then? Sorry for my confusion. Commented Sep 25, 2013 at 23:20
  • @abarnert while I tend to agree with you, I think python could one-line suckerpunch Java (while ruby mumbles incoherent gibberish in the dark) Commented Sep 25, 2013 at 23:25
  • Ryan, no you don't need that. You are not using randomInput = random.choice(gameList) statement at all, you are still comparing with gameList, see my answer below Commented Sep 25, 2013 at 23:26

6 Answers 6

3

The error occurs in line 4, which reads the user input. The problem is that input(...) parses the expression after reading from command line, so strings would have to be quoted.

Use raw_input(...) instead:

userInput = raw_input("python, ruby, or java?:")
Sign up to request clarification or add additional context in comments.

Comments

2

your condition will always be false because you're comparing a string with a list. What you want to do is check whether the string is inside the list, like this:

if userInput not in gameList:
    print "game is over"

2 Comments

that is all of my code so far, I just want to get this part right before moving on. I changed that line of code, but it is still coming up with an error. Is there something I should change in my code in gameList?
See other answers, the problem is with using input instead of raw_input. Also, in general never use input (unless using python 3.x)
1

You need to use raw_input() instead of input().

import random
def pythonRubyJava():
    gameList = ["python","ruby","java"]
    userInput = raw_input("python, ruby, or java?:")
    randomInput = random.choice(gameList)
    if userInput != randomInput:
        print "The game is over"

When using input(), the input has to be formatted correctly by the user, i.e. 'java' with quotes.

2 Comments

Thank you for your help. I'm starting the if statements for which beats what, but not really understanding it fully. Do i need to store python, ruby, and java as variables for the game?
No no, just change it to raw_input and look at my answer for the next part
1

I guess you are trying to see if input is same with random choices between three, in that case, use randomInput instead of gameList. and use raw_input, so that python can be input instead of "python"

edited to address your edit

import random
def pythonRubyJava():
    gameList = ["python","ruby","java"]
    userInput = raw_input("python, ruby, or java?:")
    randomInput = random.choice(gameList)

    if userInput not in gameList:
        print "The game is over"
    if userInput == randomInput:
        print "stalemate"  

8 Comments

thank you for your help. Now I do not want this game to tie, so I will need a while loop somewhere, look at my updated code above and see if you can tell me where to put it in. Right now I don't have the loop in, it just prints stalemate, but I want to take that out have have the game run again if they are the same.
Well, then instead of three ifs, just use if userInput ==randomInput print "stalemate" . see edit above
I understand, but how to I run the program over if they are the same, I don't want it to end in a stalemate.
So, what is the criteria to start over or end the game, is this some kind of rock, paper, scissors? When do you want to end game when they both are not same?
sorry for not giving you the details. It is a RPS game, just different names. I want to print the outcome of the game, and want it to ask me again, not to come to an end, just to repeat playing the game.
|
0

Use the not in operator:

if userInput not in gameList:
    print "The game is over"

2 Comments

still saying the game is not defined after input
See Jens Erat's answer for the correction required. However you still need to use the not in operator instead of !=
0

Actually, the answer is a combination of both versions of the answers I have seen so far.

First, input() does not return a string, it returns the literal characters entered, so when you enter 'python', it is looking for the variable python which does not exist.

You would need to surround the input with quotes in order for it to work, but there is a better way. Use raw_input() which takes the input as a string value.

Also, once you've fixed that, you will come to an error on line 6. In line six, you are comparing the answer to the entire list, but it needs to be compared to each item in the list.

Easily solved with:

if userInput not in gameList:
    #execute

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.