0

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')
2
  • 2
    Why do you think the input is a list?! Commented Nov 4, 2014 at 22:38
  • Python strings are not char arrays. Commented Nov 4, 2014 at 22:41

3 Answers 3

2

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

if First_name == "Barack" :
Sign up to request clarification or add additional context in comments.

1 Comment

if First_name.lower() == barack will be more pythonic
1

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'

Comments

0

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']:

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.