0

not sure if my title does this question justice, this is my first post, forgive me if this is answered somewhere else but I really did try researching it and trying to find the answer specific to my question.

I am very new to Python and I was building my first program, everything is working fine, except for the part of the code that asks the user if they want to know the length of their name.

I am trying to add in multiple answers for "yes" and for "no" so that if the user inputs something like "yeah" or "nope" it will still accept the answer. I built lists for possible answers. However it just skips over my if statement, my elif statement, and goes right to my else statement. I have also tried not using lists, but "and" and also "or" with no luck.

# prints the length of name if user desires, if not it goes onto ask age

print('Would you like to know the length of your name?')
answer = input()
affirmative = ['yes', 'Yes', 'yeah', 'Yeah', 'yup', 'Yup', 'y', 'Y', 'yea',          
'Yea']
negative = ['No', 'no', 'Nope', 'nope', 'N', 'n', 'Nah', 'nah']
if answer == affirmative:
print('The length of your name is ' + str(int(len(myName))))
elif answer == negative:
    print('Ok! Thats fine! I didn\'t want to tell you anyway!')
else:
    print('Ok then.....next question')

I am using the latest version of Python. Thank-you in advance and again sorry if this was answered somewhere else.

DFC302
  • 17
  • 1
  • 6
  • Possible duplicate of [Is there a short contains function for lists in Python?](http://stackoverflow.com/questions/12934190/is-there-a-short-contains-function-for-lists-in-python) – Simon MᶜKenzie Jun 08 '16 at 03:10

2 Answers2

0

Here are a few suggestions that should help you on your way.

# prints the length of name if user desires, if not it goes onto ask age

answer = input('Would you like to know the length of your name?') 
# Rather than printing, input can take the string to print (helps with figuring out what the input is used for)
affirmative = ['yes', 'Yes', 'yeah', 'Yeah', 'yup', 'Yup', 'y', 'Y', 'yea',          
'Yea']
negative = ['No', 'no', 'Nope', 'nope', 'N', 'n', 'Nah', 'nah']
if answer in affirmative:
    print('The length of your name is ' + str(int(len(myName))))
elif answer in negative:
    print('Ok! Thats fine! I didn\'t want to tell you anyway!')
else:
    print('Ok then.....next question')

By using the in keyword, you can check if your string is in the list.

Jenner Felton
  • 787
  • 1
  • 9
  • 18
  • Thankyou very much! That solved the issue and now I understand more about how that works. Thankyou again. – DFC302 Jun 08 '16 at 17:53
0
   # prints the length of name if user desires, if not it goes onto ask age
           # prints the length of name if user desires, if not it goes onto 
   ask age

    answer = input('Would you like to know the length of your name?') 
   # Rather than printing, input can take the string to print (helps with figuring 
    out what the input is used for)
    affirmative = ['yes', 'Yes', 'yeah', 'Yeah', 'yup', 'Yup', 'y', 'Y', 'yea',          
    'Yea']
    negative = ['No', 'no', 'Nope', 'nope', 'N', 'n', 'Nah', 'nah']
   if answer in affirmative:
        print('The length of your name is ' + str(int(len(myName))))
    elif answer in negative:
      print('Ok! Thats fine! I didn\'t want to tell you anyway!')
    else:
       print('Ok then.....next question')
Dylan
  • 1
  • 1