# Beginner's problem

**URL:** <https://discuss.python.org/t/beginners-problem/80914>\
**Category:** Python Help\
**Created:** [February 16, 2025, 3:30pm UTC](https://discuss.python.org/t/beginners-problem/80914 "2025-02-16T15:30:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fonz](https://avatars.discourse-cdn.com/v4/letter/f/7ba0ec/32.png) [@Fonz](https://discuss.python.org/u/Fonz)\
**Post date:** [February 16, 2025, 3:30pm UTC](https://discuss.python.org/t/beginners-problem/80914/1 "2025-02-16T15:30:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chepner](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@chepner](https://discuss.python.org/u/chepner)\
**Post date:** [February 16, 2025, 4:49pm UTC](https://discuss.python.org/t/beginners-problem/80914/2 "2025-02-16T16:49:19Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![Fonz](https://avatars.discourse-cdn.com/v4/letter/f/7ba0ec/32.png) [@Fonz](https://discuss.python.org/u/Fonz)\
**Post date:** [February 16, 2025, 5:36pm UTC](https://discuss.python.org/t/beginners-problem/80914/3 "2025-02-16T17:36:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [February 17, 2025, 2:24pm UTC](https://discuss.python.org/t/beginners-problem/80914/4 "2025-02-17T14:24:26Z")

</div>

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.

> [@I got the right answer the wrong way](https://discuss.python.org/t/i-got-the-right-answer-the-wrong-way/79846/9):
>
> You can use something simple like this: the\_list = [143266561, 1738152473, 312377936, 1027708881, 1495785517, 1858250798, 1693786723, 1871655963, 374455497, 430158267] def max\_value(some\_list): max\_value\_in\_list = 0 for num in some\_list: if num \> max\_value\_in\_list: max\_value\_in\_list = num return max\_value\_in\_list print('The max value in the list is: ', max\_value(the\_list))

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

> [@Fonz](#):
>
> maximum = max(numbers)

---

<div class="post-metadata">

**Author:** ![AlSweigart](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alsweigart/32/12237_2.png) [@AlSweigart](https://discuss.python.org/u/AlSweigart)\
**Post date:** [February 17, 2025, 2:43pm UTC](https://discuss.python.org/t/beginners-problem/80914/5 "2025-02-17T14:43:53Z")

</div>

> [@Fonz](#):
>
> Thank you. The code I wrote is the following:  
> Largest = None  
> Smallest = None

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:

```python
print('Hello, world!')

```

---

<div class="post-metadata">

**Author:** ![Quercus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/quercus/32/31483_2.png) [@Quercus](https://discuss.python.org/u/Quercus)\
**Post date:** [February 17, 2025, 3:44pm UTC](https://discuss.python.org/t/beginners-problem/80914/6 "2025-02-17T15:44:05Z")

</div>

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:

- [Python Discourse Quick Start](https://discuss.python.org/t/python-discourse-quick-start/116)
- [About Discussions on Python.org](https://discuss.python.org/about)

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.

---

<div class="post-metadata">

**Author:** ![Fonz](https://avatars.discourse-cdn.com/v4/letter/f/7ba0ec/32.png) [@Fonz](https://discuss.python.org/u/Fonz)\
**Post date:** [February 17, 2025, 7:03pm UTC](https://discuss.python.org/t/beginners-problem/80914/7 "2025-02-17T19:03:23Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Fonz](https://avatars.discourse-cdn.com/v4/letter/f/7ba0ec/32.png) [@Fonz](https://discuss.python.org/u/Fonz)\
**Post date:** [February 17, 2025, 7:07pm UTC](https://discuss.python.org/t/beginners-problem/80914/8 "2025-02-17T19:07:45Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [February 18, 2025, 1:28am UTC](https://discuss.python.org/t/beginners-problem/80914/9 "2025-02-18T01:28:34Z")

</div>

Try like this:

 ![format_code](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/a/e/aefe8250b926e4cec16c00a617465cc57f0d4a43.png)

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

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

multi_two(5,4)

```

---

<div class="post-metadata">

**Author:** ![Quercus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/quercus/32/31483_2.png) [@Quercus](https://discuss.python.org/u/Quercus)\
**Post date:** [February 18, 2025, 1:08pm UTC](https://discuss.python.org/t/beginners-problem/80914/10 "2025-02-18T13:08:02Z")

</div>

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:

```python
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:

```python
    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:

```python
        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:

```python
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.

---

<div class="post-metadata">

**Author:** ![Fonz](https://avatars.discourse-cdn.com/v4/letter/f/7ba0ec/32.png) [@Fonz](https://discuss.python.org/u/Fonz)\
**Post date:** [February 21, 2025, 10:58am UTC](https://discuss.python.org/t/beginners-problem/80914/11 "2025-02-21T10:58:21Z")

</div>

Thank you very much!
