A Value error is
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value
the float function can take a string, ie float('5'), it's just that the value 'string' in float('string') is an inappropriate (non-convertible) string
On the other hand,
Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError
so you would get a TypeError if you tried float(['5']) because a list can never be converted into a float.
Cite
Answer from David on Stack OverflowIn this example:
x = int(input("x: "))
print(f'x = {x}')if the input is a string, a ValueError is raised. Why not a TypeError? An invalid data type is inputted after all.
A Value error is
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value
the float function can take a string, ie float('5'), it's just that the value 'string' in float('string') is an inappropriate (non-convertible) string
On the other hand,
Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError
so you would get a TypeError if you tried float(['5']) because a list can never be converted into a float.
Cite
ValueError a function is called on a value of the correct type, but with an inappropriate value
TypeError : a function is called on a value of an inappropriate type
What are common Python errors?
How do I handle errors in Python?
How can I monitor Python errors in real time?
I don't understand when each one will be raised, and I don't understand the online explanations
I'm getting a value error on function 2 (is_list_odd) but not on function 1 (is_list_even) why is that? I isolate the functions and function one run fine but function 2 returns a ValueError every time, why?
def is_list_even(check_even):
user_input = int(input())
for i in range(user_input):
user_num = int(input())
check_even.append(user_num)
if check_even[i] % 2 == 0:
return True
def is_list_odd(check_odd):
user_input2 = int(input())
for j in range(user_input2):
user_num2 = int(input())
check_odd.append(user_num2)
if check_odd[j] % 2 != 0:
return True
if __name__ == '__main__':
check_even = []
check_odd = []
if is_list_even(check_even):
print('all even')
if is_list_odd(check_odd):
print('all odd')
else:
print('not even or odd')
#I don't understand why