Beginner's problem

Hello,
sorry for bother you. I am a beginner and I’m having trouble with this problem, could someone help me? Thak you very much.

5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters ‘done’. Once ‘done’ is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below.

Desired Output

Invalid input
Maximum is 10
Minimum is 2

1 Like

You need to ask specific questions about what you are having trouble with. We aren’t here to do your assignment for you. (You can assume someone will help; that’s why the channel is here: you need to let us know what you are having trouble with so that we can help you.)

4 Likes

Thank you. The code I wrote is the following:
Largest = None
Smallest = None
try :
while True:
numbers = [7, 2, 10, 4]
maximum = max(numbers)
minimum = min(numbers)
number = input(“Enter a number”)
if number == “done”:
break
print(num)
except :
print(“Invalid input”)
numbers = [7, 2, 10, 4]
maximum = max(numbers)
if Largest is None :
Largest = maximum
elif “maximum” > “Largest” :
Largest = maximum
print(“Maximum is”, maximum)
minimum = min(numbers)
if Smallest is None :
Smallest = minimum
elif “minimum” > “Smallest” :
Smallest = minimum
print(“Minimum is”, minimum)
However it tells me that I should compute the maximum and the minimum ; how am I supposed to do that?

Hi,

here is a thread that provides a function that determines the maximum value from a list. By the same reasoning, you can determine the minimum value from a list as well.

You can of course, also obtain the max as you have done here:

1 Like

To help us help you, please format your post so the code keeps its indentation. Otherwise, we can’t really understand it and can’t find any mistakes you’ve made. You can either edit your post, highlight your code, and then click the </> button. Or you can put three backtick characters ` (they are to the left of the 1 key on your keyboard) on a line by themselves. You need three backticks before your code and three backticks after, like this:

```
print(‘Hello, world!’)
```

This will appear as:

print('Hello, world!')
3 Likes

Hello, @Fonz, and welcome to the Python community!

We hope you enjoy learning Python.

Please see the following pages for information that will enhance your benefit from the discussions on this forum:

The first of those two pages offers important guidance regarding how to format Python code and program output for correct display whenever you post such content while asking and answering questions. Correct formatting is always essential.

As asked earlier in this discussion, please format your current code accordingly, so that it is easy for us to see important details such as its indentation.

With a guess at how your actual code is indented, it appears that regardless of what the user enters at the prompt, your code is always looking for the maximum and minimum values in the list assigned to the numbers variable. Instead, you should allow the user to enter the suggested 7, 2, bob, 10, 4, and done or any other data at the prompts that appear during program execution. Most likely, that data suggested in the instructions is just an example meant to clarify what your program should do, and as a means to test it. Your program needs to be rewritten to execute correctly for that data, or any other set of data that is entered.

Your code includes handling of a situation wherein an entered value cannot be converted to an integer. Handling possible errors such as that is a good policy. However, something else that your code does not anticipate is a situation wherein the user simply enters done during the very first iteration of the loop. In that case, there would be no numerical data to process, and therefore no maximum and minimum value to determine. Later on, after you have gotten your code to work according to the instructions that were given, you could add the ability for it to handle such a situation.

1 Like

Thank you,

Largest = None
Smallest = None
try :
while True:
numbers = [7, 2, 10, 4]
maximum = max(numbers)
minimum = min(numbers)
number = input(“Enter a number”)
if number == “done”:
break
print(num)
except :
print(“Invalid input”)
numbers = [7, 2, 10, 4]
maximum = max(numbers)
if Largest is None :
Largest = maximum
elif “maximum” > “Largest” :
Largest = maximum
print(“Maximum is”, maximum)
minimum = min(numbers)
if Smallest is None :
Smallest = minimum
elif “minimum” > “Smallest” :
Smallest = minimum
print(“Minimum is”, minimum)

Thank you,

Preformatted text
Largest = None
Smallest = None
try :
while True:
numbers = [7, 2, 10, 4]
maximum = max(numbers)
minimum = min(numbers)
number = input(“Enter a number”)
if number == “done”:
break
print(num)
except :
print(“Invalid input”)
numbers = [7, 2, 10, 4]
maximum = max(numbers)
if Largest is None :
Largest = maximum
elif “maximum” > “Largest” :
Largest = maximum
print(“Maximum is”, maximum)
minimum = min(numbers)
if Smallest is None :
Smallest = minimum
elif “minimum” > “Smallest” :
Smallest = minimum
print(“Minimum is”, minimum)Preformatted text

Try like this:

This way, the script will appear as it would on your editor (as an example):

def multi_two(x, y):
    return x * y

multi_two(5,4)
1 Like

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.

2 Likes

Thank you very much!

1 Like