FAQ: Learn Python - Python Syntax - ValueError

This community-built FAQ covers the “ValueError” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python syntax exercise ValueError

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try searching for it by clicking the spyglass icon (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

1 Like

2 posts were split to a new topic: Solution is wrong

9 posts were split to a new topic: Create a string called product

2 posts were split to a new topic: Strings Not Getting Added Properly

A post was split to a new topic: The solution given is wrong

2 posts were split to a new topic: Don’t really understand what part 2 is asking

3 posts were split to a new topic: Can python turn strings into integers?

The page would not accept 10.0 as an answer when you bring “big_string” as it was a float, it wanted a definitive 10 to let me proceed, I couldn’t figure out how to do this as “float_1” and “float_2” are obviously both floats.

When I clicked “show me the answer” it basically wrote out the exact same code I had written, which when printed showed 10.0.

This was, of course, accepted. Why was mine not?

The provided solution resulted in an answer of 10, however the product of two floats should result in a float, 10.0. I would think that the following code provided as a solution would include a step that converts a float to an interger, unless, of course, the conversion of a float to a string automatically produces and interger.

The code given as an example does not work:

string_num = “7.5”
print int(string_num)
print float(string_num)

When i run this it says:

Traceback (most recent call last):
File “script.py”, line 2, in
print int(string_num)
ValueError: invalid literal for int() with base 10: ‘7.5’

Why is this happening?

That’s Python telling us that a string containing a float cannot be directly cast to an integer. But, it can be cast to a float, and then to an int.

>>> int('7.5')
Traceback (most recent call last):
  File "<pyshell#220>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '7.5'
>>> int(7.5)
>>> int(float('7.5'))
1 Like

What other datatypes aside from floats, integers, and strings?

Built-in Types — Python 3.12.0 documentation

Boolean, Numeric, and Text Sequence are the most commonly used types, but as you will have read above, there are others. At some point down the road you will be exploring them, starting with Sequences.

From where i should know that i have to add o.25 x 40.0 it wasnt written anywhere i have to put there these numbers