Yoav Kozenyuk
Yoav Kozenyuk

Reputation: 1

How can I make an if statement that recognizes whether or not my input appears in a list?

That is what I have so far and it doesn't work, I am new to Python so sorry if there is a really obvious mistake that I don't see :)

Quotes = ['Iron and Blood' , 'No Quote Available' ]   
Blood=Quotes[0]
Else=Quotes[1]

Name = raw_input('Who do you want this humble AI to quote?')
if Name == Bismark:
    print(Blood)

Upvotes: 0

Views: 44

Answers (1)

m00am
m00am

Reputation: 6298

You are missing quotes around Bismark if you want to treat it as a string. You want that, because the raw_input function returns the text the user entered as a string against which you are comparing. This should do the trick:

Quotes = ['Iron and Blood' , 'No Quote Available' ]   
Blood=Quotes[0]
Else=Quotes[1]

Name = raw_input('Who do you want this humble AI to quote?')
if Name == 'Bismark':
    print(Blood)

That said there are some better and or more 'pythonic' ways to do this. This one uses a dictionary to store quotes:

quotes = {
    'Bismark': 'Iron and Blood',
    'pep8': 'Variable names should be lower case to make them distinguishable from class names.', 
}
not_found = 'No Quote Available'

# this would be just input for python3
name = raw_input('Who do you want this humble AI to quote?')

try:
    print(quotes[name])
except KeyError:
    print(not_found)

This allows you to add new quotes to the dictionary without adding an if-statement every time.

Upvotes: 2

Related Questions