1

Okay so I'm trying to do two things let me post my code real quick:

def main():

    animals = ["Monkey", "Parrot", "Dog", "Koala", "Giraffe"]
    favAnimal = input("What is your favorite animal?")

    print("What is your favorite animal?")
    if(favAnimal == animals):
        print("The " + favAnimal + " is both of our favorite's!")
    else:
        print("We don't like any of the same animals. D;")

    number= 1
    for n in range(0, 5):
        print(number + "." + animals[n])
        number=number+1

main()

So in the if statement I'm trying to figure out how to check if the users input is the same as any of the items in the animals list and if it is print that statement.

The other thing I'm trying to do is in the for loop I'm printing out the animals list with ascending numbers as such:

  1. Monkey
  2. Parrot 3... etc

I've tried different things but nothing has worked for the for loop it keeps saying I need to add int or float to line 18 (the print number line) but when I do it doesn't seem to work.

Can anyone help me out with this?

JakeNBake
  • 69
  • 5

4 Answers4

1

You can fix your code with this (using Python 2.7.x):

def main():

    animals = ["Monkey", "Parrot", "Dog", "Koala", "Giraffe"]
    favAnimal = raw_input("What is your favorite animal?")

    print("What is your favorite animal?")
    if favAnimal in animals:
        print("The " + favAnimal + " is both of our favorite's!")
    else:
        print("We don't like any of the same animals. D;")

    for n in range(0, 5):
        print(str(n+1) + ". " + animals[n])

main()
aldarel
  • 446
  • 2
  • 7
  • This was the easiest to understand and worked perfectly! Thanks! I added what Marcus said about using .capitalize() at the end of the input statement so the users input would have the right capitalization no matter what they did. – JakeNBake Feb 09 '17 at 18:51
1

The if statement can be fixed by using the "in" operator:

if favAnimal in animals:
    print("The " + favAnimal + " is both of our favorite's!")
else:
    print("We don't like any of the same animals. D;")

The error with the float/int is because you can not add a string and an number in the output. If you simply change the print statement from:

    print(number + "." + animals[n])

To:

    print(str(number) + "." + animals[n])

Everything should be fine.

John F
  • 176
  • 4
1
if __name__ == "__main__":
    animals = ["Monkey", "Parrot", "Dog", "Koala", "Giraffe"]
    # Make sure the 'animals' data is capitalized, because there could have been a typo
    animals = [animal.capitalize() for animal in animals]

    # Python's most followed style guide, PEP8, recommends 'lowercase_with_underscores' 
    #for method names and instance variables. See:
    # https://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables

    # Also, we treat the user's input with .capitalize()
    favorite_animal = input("What is your favorite animal? ").capitalize()

    if favorite_animal in animals:
        # See 'format' method: https://docs.python.org/3.6/library/stdtypes.html#str.format
        print("The {} is in both of our favorite's!".format(favorite_animal))
    else:
        print("We don't like any of the same animals. D;")

    # See accessing index in 'for' loops:
    # http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops
    for i, animal in enumerate(animals):
        print("{}. {}".format(i + 1, animal))
0

One thing to consider is that the user might enter the name of the animal with all lower-case, all upper-case or other combination of letters.

So the recommended way of checking whether the animal exists in the list would be:

if favAnimal.lower() in [x.lower() for x in animals]:
swbandit
  • 1,986
  • 1
  • 26
  • 37