Lesson 15 - The Big If


#1

Hey guys, I have a little problem. Exercise 15 ("The Big If") passes me, but after trying it out on a different console the answer I always get after writing in any number is the last elif - "You're too old for Douwe.".

The following is my code:


def the_flying_circus():
    
    a = raw_input ("Enter your age in numbers.")
    if (a <= 14): 
     print "You're way too young for Douwe."
     return True   
        
    elif (a >= 15 and a <= 19):
     print "It's possible Douwe may date you."
     return True  
     
    elif (a >= 20 and a <= 26):
     print "You're the perfect age for Douwe!"
     return True
     
    elif (a >= 27):
     print "You're too old for Douwe."
     return True
        
    else:
        print "You didn't enter a proper number."
        return True
the_flying_circus()

As I mentioned before, it always prints out "You're too old for Douwe.". What is the problem here? Thanks.


#2

the problem is that raw_input gives you a unicode:

a = raw_input ("Enter your age in numbers.")
print type(a)

type is a built in function to give you the data type. comparing unicodes with integers isn't going to work well, as you discovered, you could catch it to a integer:

a = int(raw_input ("Enter your age in numbers."))

the problem now is that if the user doesn't enter a number, your program will crash. So you would actually have to validate that the user enters a number


#3

Thanks! I haven't learned what you told me yet, so it was a pain trying to understand what you wrote, but it made the code work! I guess I'm just rushing into new things too soon. At least I'll be a little familiar with the unicodes and integers in the future. Thanks again for your help, I appreciate it.


#4

well, integers should be know. They are non decimal numbers and you use them:

if (a <= 14):

14 is a integer. Unicode isn't taught in this course, but you can see them as strings, and comparing string with integers doesn't work good


#5

Thanks. I'm feeling a little embarrassed, seems like the name "integer" was deleted from my memory. I realize what it is now, but I forgot it was called that way. :smiley:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.