The problem is that when you catch the ValueError, you don't do anything to fix input_number (i.e. make it an int), so the program continues past your try statement and into your range() call, which proceeds to raise another exception because input_number isn't an int.
Simply catching an exception doesn't fix the error that caused it to be raised in the first place. If you catch an exception, you need to understand why it raised, and do something appropriate to correct the situation; blindly catching an exception will often just lead to another error later in your program, as it did here.
One way to correct this situation is to prompt the user again:
while True:
try:
input_number = int(input(
"How many numbers would you like the program to calculate the average of?\n>> "
))
break
except ValueError:
print("Invalid entry")
With the above code, the loop continues until input_number is an int.
Since it looks like you'll be wanting to do this again, I'd suggest putting it in a function:
def input_number(prompt: str) -> int:
"""Prompt for a number until a valid int can be returned."""
while True:
try:
return int(input(prompt))
except ValueError:
print("Invalid entry")
n = input_number(
"How many numbers would you like the program to calculate the average of?\n>> "
)
numbers = [
input_number(f"Please enter a whole number ({i}/{n} numbers entered):\n>>")
for i in range(1, n+1)
]
print(f"Average: {sum(numbers)/len(numbers)}")
How many numbers would you like the program to calculate the average of?
>> I don't know
Invalid entry
How many numbers would you like the program to calculate the average of?
>> 4
Please enter a whole number (1/4 numbers entered):
>>5
Please enter a whole number (2/4 numbers entered):
>>42
Please enter a whole number (3/4 numbers entered):
>>???
Invalid entry
Please enter a whole number (3/4 numbers entered):
>>543
Please enter a whole number (4/4 numbers entered):
>>0
Average: 147.5
except ValueError or TypeErrorThat is not the right syntax for catching multiple errors. Use a comma instead ofor.for i in rangeline? That doesn't seem possible given your code. If you're getting both "Invalid entry" and then an error, that makes sense, and the answer provided will help you fix your problem.