Reputation: 43
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.
Upvotes: 0
Views: 1574
Reputation: 3711
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"
Upvotes: 1
Reputation: 5673
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 variablepython
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
Upvotes: 0
Reputation: 18457
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"
Upvotes: 2
Reputation: 38732
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?:")
Upvotes: 3
Reputation: 671
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.
Upvotes: 1
Reputation: 432
Use the not in
operator:
if userInput not in gameList:
print "The game is over"
Upvotes: 0