In 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.
I don't understand when each one will be raised, and I don't understand the online explanations
How do I handle errors in Python?
What are common Python errors?
How can I monitor Python errors in real time?
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
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
The code i think you'll need
class Object(pygame.sprite.Sprite): def __init__(self, x, y, width, height, name=None): super().__init__() self.rect = pygame.Rect(x, y, width, height) self.image = pygame.surface((width, height), pygame.SRCALPHA) self.width = width self.height = height self.name = name def draw(self, win): win.blit(self.image, (self.rect.x, self.rect.y)) class Block(object): def __init__(self, x, y, size): super().__init__(x, y, size, size) block = get_block(size) self.image.blit(block, (0, 0)) self.mask = pygame.mask.from_surface(self.image)
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