Learn Before
Code
ValueError Example
current_time_str = input("What is the current time (in hours 0-23)?") current_time_int = int(current_time_str) wait_time_str = input("How many hours do you want to wait") wait_time_int = int(wait_time_str) final_time_int = current_time_int + wait_time_int print(final_time_int)
If the program is run, but the user does not input a valid numerical value, the following error is generated:
ValueError: invalid literal for int() with base 10: '' on line 2
ValueErrors are not always caused by user input error, but in this program that is the case. It is worth repeating that you need to keep track of the restrictions needed for your variables, and understand what your function is expecting. You can do this by writing comments in your code, or by naming your variables in a way that reminds you of their proper form.
0
1
Updated 2021-06-22
Tags
Python Programming Language
Data Science