johnny
johnny

Reputation: 171

Comparing string with an input string :: python

I am trying to compare an input string but when ever i enter barack as an input, the compiler directly goes to the else condition ignoring the the if condition and giving me the output "Wrong answer"

def main():
    First_name = raw_input(" enter the first name of President Obama   :  ") #input
    if First_name == ['b', 'a', 'r','a', 'c', 'k'] :
            print "Correct answer"
        else :
            print "Wrong answer"


    Exit_key = input('Press any key to end')

Upvotes: 0

Views: 381

Answers (3)

Hackaholic
Hackaholic

Reputation: 19753

using lambda and map. just if you want to learn basic concept

if map(lambda x:x,First_name.lower()) == ['b', 'a', 'r','a', 'c', 'k']:

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180471

raw_input is a string so to do what you want you would have to call list on the string:

if list(First_name) == ['b', 'a', 'r','a', 'c', 'k'])

It is easier to just do if First_name == "barack"

In [1]: inp = raw_input()
barack

In [2]: list(inp)
Out[2]: ['b', 'a', 'r', 'a', 'c', 'k']

In [3]: inp
Out[3]: 'barack'

Upvotes: 1

marsh
marsh

Reputation: 2730

Is there a reason you are doing it like that? Try:

if First_name == "Barack" :

Upvotes: 2

Related Questions