Lecture
Indicating Invalid Values with ValueError
The ValueError is raised when a function receives a valid data type but an inappropriate or invalid value.
Situations Where ValueError Occurs
-
When attempting to convert a non-numeric string to a number: Trying to convert a non-numeric string like 'hello' using
int()orfloat()will raise am error -
When attempting to convert a string representing a decimal number to an integer: Converting a string such as
'3.14'withint()will aslo result in an error.
Example of ValueError Occurrence
try: number = int("hello") except ValueError: print("Attempted to convert a non-numeric value to a number.") try: number = int("3.14") except ValueError: print("Attempted to convert a string with a decimal to an int() function.")
Previous lessonUsing the `input` Function to Receive User InputNext lessonCoding Quiz - Splitting Strings
Lessons in this chapter Β· String Formatting Made Easy
- 1. Inserting Variables into Strings with the format() Function
- 2. Tips for Using `format()` Function - indexError
- 3. Displaying Integers with the format() Function
- 4. Handling Floating-Point Numbers with the format() Function
- 5. Multiple-choice quiz
- 6. Case Conversion Methods upper() and lower()
- 7. Removing Leading and Trailing Whitespace with the strip() Function
- 8. String Composition Verification with isOOO() Methods
- 9. Finding the Position of a Specific Character Using find() and rfind()
- 10. Using the split() Method for Strings
- 11. How to Check if a Specific Character Exists in a String
- 12. Multiple-choice quiz
- 13. How to Format Strings with f-Strings
- 14. Comparison of format() Function and f-Strings
- 15. Using the `input` Function to Receive User Input
- 16. Indicating Invalid Values with ValueError
- 17. Coding Quiz - Splitting Strings
- 18. Multiple-choice quiz
- 19. Fill-in-the-blank quiz
Quiz
0 / 1
A ValueError can occur when trying to convert a non-numeric value to a number.
True
False
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help