0

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

This is the question I have tried many ways but unable to achieve min and max numbers. Please help me with the code:

largest = None
samllest = None
while True:
    num = input("Enter Numbers:")
    if num == 'done' :
        break
    else:
        try:
            n = int(num)
        except:
            print("Invalid Input")
3
  • 3
    You seem to do a good job of taking and handling the input. Here's an idea: put all those numbers in a list, and when the user inputs done, use the built-ins min and max to return the relevant numbers Commented Nov 18, 2020 at 17:54
  • yep I taught of using the list but for that user need to mention the list size initially but I dont want that. Is there any other way. Commented Nov 18, 2020 at 17:56
  • Why would they need to do that? This is not C... Aren't you familiar with the list.append() method - appends x to the end of the sequence Commented Nov 18, 2020 at 17:58

3 Answers 3

1

Using the walrus operator for fun & brevity:

numbers = list()
while (the_input := input('Enter a number:')) != 'done':
    numbers.append(int(the_input))
print(f'{min(numbers)=}, {max(numbers)=}')
Sign up to request clarification or add additional context in comments.

Comments

0

you can do a list of inputed numbers:

numbers=[]
while True:
    num = input("Enter Numbers:")
    if num == 'done' :
        break
    else:
        try:
            numbers.append(int(num))
        except:
            print("Invalid Input")
print("Min", str(min(numbers)))
print("Max", str(max(numbers)))

you will have this result:

Enter Numbers:2
Enter Numbers:3
Enter Numbers:4
Enter Numbers:5
Enter Numbers:6
Enter Numbers:7
Enter Numbers:8
Enter Numbers:done
Min 2
Max 8

2 Comments

No need to cast the print arguments to str - it is done automatically
yes your right. i have this mania to cast all elements.
0

Here you re, probe it:

lg = None
sm = None
s = None

while s != 'done':
    s = input("Enter numbers:")
    if s.isdigit():
        s = int(s)
        if lg == None and sm == None:
            lg = s
            sm = s
            
        if lg <= s:
            lg = s
        if sm >= s:
            sm = s
            
print(f"Min: {sm}, Max: {lg}")

And you can test it:

Enter numbers:5
Enter numbers:6
Enter numbers:1
Enter numbers:5
Enter numbers:8
Enter numbers:9
Enter numbers:done
Min: 1, Max: 9

And if you want a try/except block...

lg = None
sm = None
s = None

while s != 'done':
    try:
        s = int(input("Enter numbers:"))
        if lg == None and sm == None:
            lg = s
            sm = s
            
        if lg <= s:
            lg = s
        if sm >= s:
            sm = s
            
    except Exception as e:
        print(e)
        
print(f"Min: {sm}, Max: {lg}")

1 Comment

No problem, glad to help you ;). Please consider to mark it as correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.