Joe Seigel
Joe Seigel

Reputation: 5

My elif/else statements in Python are not working

I am learning python right now, and this is the code I have so far, just as a little excercise with conditionals and such:

def the_flying_circus():
    animals = raw_input("You are the manager of the flying circus. Which animals do you select to perform today?")
    if animals == "Monkeys":
        num_monkeys = raw_input("How many monkeys are there?")
        if num_monkeys >= 20:
            return "Don't let all of 'em hurt you!"
        elif num_monkeys < 20 and num_monkeys >= 5:
            return "Not too many, but be careful!"
        elif num_monkeys < 5:
            return "You're in luck! No monkeys to bother you."
    elif animals == "Anteaters":
        return "What the hell kinda circus do you go to?!"
    elif animals == "Lions":
        height_lion = raw_input("How tall is the lion (in inches)?")
        if height_lion >= 100:
            return "Get the hell outta there."
        elif height_lion < 100:
            return "Meh. The audience usually has insurance."
    else:
        return "Dude, we're a circus, not the Amazon rainforest. We can only have so many animals."
print the_flying_circus()

So the problem I'm having is that the code works fine, until after I've entered an animal. If I do anteaters, it's fine. But if I do monkeys or lions, no matter what number I put in, only the string under the initial if statement is printed ("Don't let all of em hurt you" or "get the hell outta there"). I'm also not getting any errors. Why is this?

Upvotes: 0

Views: 409

Answers (3)

Chetan
Chetan

Reputation: 1271

raw_input takes input as a string. It should be converted to int

def the_flying_circus():
    animals = raw_input("You are the manager of the flying circus. Which animals do you select to perform today?\n")
    if animals.lower() == "monkeys":
        num_monkeys = int(raw_input("How many monkeys are there?\n"))
        if num_monkeys >= 20:
            result = "Don't let all of 'em hurt you!\n"
        elif num_monkeys < 20 and num_monkeys >= 5:
            result = "Not too many, but be careful!\n"
        elif num_monkeys < 5:
            result = "You're in luck! No monkeys to bother you.\n"
    elif animals.lower() == "anteaters":
        result = "What the hell kinda circus do you go to?!\n"
    elif animals.lower() == "lions":
        height_lion = int(raw_input("How tall is the lion (in inches)?\n"))
        if height_lion >= 100:
            result = "Get the hell outta there.\n"
        elif height_lion < 100:
            result = "Meh. The audience usually has insurance.\n"
    else:
        result = "Dude, we're a circus, not the Amazon rainforest. We can only have so many animals.\n"
    return result

result = the_flying_circus()
print result

Upvotes: 1

Sushant Pachipulusu
Sushant Pachipulusu

Reputation: 6143

You are taking an input of a string in your code and you are comparing it with an integer which should not be done. Type cast your input

num_monkeys=int(raw_input("Write your content here"))

Upvotes: 1

Yu Hao
Yu Hao

Reputation: 122493

num_monkeys = raw_input("How many monkeys are there?")

raw_input returns a string, you need to convert it to int:

num_monkeys = int(raw_input("How many monkeys are there?"))

Upvotes: 3

Related Questions