Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I'm new to programming, please help me to correct my code.

I'm attempting to compose a program that more than once prompts a client for integer numbers until the client enters 'done'. Once 'done' is entered, print out the biggest and littlest(smallest) of the numbers. On the off chance that the client enters something besides a legitimate number catch it with an attempt/aside from and put out a suitable message and disregard the number.

largest = None

smallest = None

while True:

        num = input("Enter a number: ")

        if num == 'done':

            break

        try:

            fnum = float(num)

        except:

            print("Invalid input")

            continue

      lst = []

numbers = int(input('How many numbers: '))

for n in range(numbers):

    lst.append(num)

print("Maximum element in the list is :", max(lst), "\nMinimum element in the list is :", min(lst))

closed

4 Answers

0 votes
by (19k points)
 
Best answer
Given below is the code to find the largest and smallest nmber in python

largest = None

smallest = None

lst = []

while True:

    num = input("Enter a number: ")

    

    if num == 'done':

        break

    

    try:

        fnum = float(num)

        lst.append(fnum)

        

        if largest is None or fnum > largest:

            largest = fnum

        

        if smallest is None or fnum < smallest:

            smallest = fnum

    

    except ValueError:

        print("Invalid input. Please enter a valid number.")

if lst:

    print("The largest number entered is:", max(lst))

    print("The smallest number entered is:", min(lst))

else:

    print("No numbers were entered.")

In this updated code, the program repeatedly prompts the user for numbers until they enter 'done'. The numbers are stored in a list, and the largest and smallest values are determined. Finally, it displays the maximum and minimum numbers entered. If the user inputs an invalid value, an appropriate message is shown.
0 votes
by (26.4k points)

Your code is correct, But you have to change a little bit.

Look at the below code.

lst = []

while True:

    user_input = input('Enter a number: ')

    if user_input == 'done':

        break

    try:

        lst.append(int(user_input))

    except ValueError:

        print('Invalid input')

if lst:

    print('max: %d\nmin: %d' % (max(lst), min(lst)))

Want to learn python to get expertise in the concepts of python? Join python certification course and get certified

0 votes
by (25.7k points)
I've made some corrections to your code. Please see the modified version below:

largest = None

smallest = None

lst = []

while True:

    num = input("Enter a number: ")

    

    if num == 'done':

        break

    

    try:

        fnum = float(num)

        lst.append(fnum)

        

        if largest is None or fnum > largest:

            largest = fnum

        

        if smallest is None or fnum < smallest:

            smallest = fnum

    

    except ValueError:

        print("Invalid input. Please enter a valid number.")

if lst:

    print("Maximum element in the list is:", max(lst))

    print("Minimum element in the list is:", min(lst))

else:

    print("No numbers entered.")

Here's what I changed:

Moved the declaration of lst outside the loop so that it accumulates all the numbers entered by the user.

Inside the loop, I converted the input num to a float (fnum) and added it to the list lst.

Added checks to update the largest and smallest values accordingly based on the input number.

Added a check at the end to ensure that the list is not empty before printing the maximum and minimum values.

Improved the formatting of the output statements for clarity.

Now, the program should repeatedly prompt the user for numbers, store them in a list, and finally print the largest and smallest numbers after the user enters 'done'. If the user enters an invalid input, it will display an appropriate message.
0 votes
by (15.4k points)
largest = None

smallest = None

lst = []

while True:

    num = input("Enter a number: ")

    

    if num == 'done':

        break

    

    try:

        fnum = float(num)

        lst.append(fnum)

        

        if largest is None or fnum > largest:

            largest = fnum

        

        if smallest is None or fnum < smallest:

            smallest = fnum

    

    except ValueError:

        print("Invalid input. Please enter a valid number.")

if lst:

    print("Maximum element in the list is:", max(lst))

    print("Minimum element in the list is:", min(lst))

else:

    print("No numbers entered.")

This code prompts the user for numbers until they enter 'done'. It stores the numbers in a list and tracks the largest and smallest values entered. Finally, it prints the maximum and minimum numbers. If an invalid input is entered, it displays an appropriate message.

Browse Categories

...