Back

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

I have been taking a Python course. Be that as it may, one of the tasks 5.2 on week 7 stood out enough to be noticed. 

The goal is to cause the user to enter a few numbers and enter done when he entered all the numbers he needed. From that point onward, the output should be the greatest number and most modest number he entered. 

Here is the issue. In the event that I enter a negative number, it isn't shown. Suppose I enter: 32, 55,10, - 2 76. The output should be 76 and - 2. However, what truly happens is that 76 and 10 are printed out. 

Do you guys know why this occurs? 

Here is the code.

largest = None

smallest = None

while True:

    try:

        num = input("Enter a number: ")

        if num == "done":

            break

        print (num)

        num = int(num)

        for number in range(num):

            if largest is None or largest < num:

                largest = num

                continue

            elif smallest is None or smallest > num:

                smallest = num       

    except ValueError:

        print("Please, enter only numbers.")

print ("Maximum", largest)

print ("Minimum", smallest)

1 Answer

0 votes
by (26.4k points)

You can do it without a loop

code:

largest = None

smallest = None

while True:

    try:

        num = input("Enter a number: ")

        if num == "done":

            break

        print (num)

        num = int(num)

        if largest is None or largest < num:

            largest = num

        elif smallest is None or smallest > num:

             smallest = num

    except ValueError:

        print("Please, enter only numbers.")

print ("Maximum", largest)

print ("Minimum", smallest)

output:

Enter a number: 12

12

Enter a number: 56

56

Enter a number: 34

34

Enter a number: -2

-2

Enter a number: 17

17

Enter a number: done

Maximum 56

Minimum -2

Are you interested to learn python? Join python certification course fast!

Browse Categories

...