Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm fairly new to python coding so I'm trying to challenge myself to do elif statements and I can't find how to fix this problem

So here is my code and I can’t see what causes the error.

foods = int(input("What is your favorite food?: "))

if foods == apple:
    print("you like fruits!")
elif foods == pork:
    print("you like meat!")
elif foods == egg:
    print("you like poultry!")

The error is

line 3, in <module>
    foods = int(input("What is your favorite food?: "))
ValueError: invalid literal for int() with base 10: 'egg:'

Does anyone have any idea how to fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The int part in the int(input("What is your favorite food?: ")) line tries to convert the input string "egg" into an integer, which it can´t. Remove it and the problem will go away.

Also, after the if == or elif ==, you need to write the food type as "egg", "pork", "apple", otherwise python think you are referring to an variable and not a specific string.

Good luck!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading