While we still cannot see its indentation, it is evident that the code needs to be revised. What you have is more complicated than necessary. Consider reorganizing your solution into three parts, as follows:
- Initialize a list for storing valid input from the user.
- Collect, validate, and store input from the user.
- Display results.
Your numbers
list will be fine for storing valid input, however prior to the while
loop, it should be initialized as an empty list, as follows:
numbers = []
Next, within the while
loop, you can collect, validate, and store user input in that list. The loop header in your original code was appropriate.
At the beginning of each iteration of the loop, request data from the user. Modifying a line from your original code a bit, this would inform the user to either enter a number, or to terminate the input:
number = input("Enter an integer (or done to exit): ")
if the user chooses to exit, a break
will terminate execution of the while
loop. You already have that in the code that was provided.
If the user has chosen to input data rather than to exit, this would be where to validate that input. Make sure the try
and except
blocks are placed inside the while
loop. Within the try
block, you can do this in order to store valid integers into the numbers
list:
numbers.append(int(number))
Within the except
block, you can provide a message to inform the user of invalid input.
After the while
loop, display the output. As suggested earlier, you could check whether or not the user entered any valid data. The following would display a maximum and minimum if there was any valid data, or a message if there was not any valid data at all:
try:
print(f"Maximum is {max(numbers)}")
print(f"Minimum is {min(numbers)}")
except ValueError:
# if unsuccessful, the list was empty
print("No numbers were entered.")
Hopefully, the above will help. Please be sure to format any code that you post.