简体   繁体   中英

How can I find smallest and largest number with inputs not using min() nor max()

I'm receiving a course in which i'm asked to: "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. The desired output is:

Invalid input
Maximum is 10
Minimum is 2

"

I've not been taught min() and max(). So I guess I'm not supposed to use it. There's some given code to start with which is:

largest = None
smallest = None
while True:
    num = input("Enter a number: ")
    if num == "done" : break
    print(num)

print("Maximum", largest)

So far i've been able to make it work to do the largest part by doing this:

largest = None
while True:
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        number = int(num)
    except:
        print ("Invalid Input")
        break
    if largest is None:
        largest = number
    elif largest < number:
        largest = number
    elif largest > number:
        continue

print ("Maximum is", largest)

But whenever I try to enter the "smallest" variable I'm not sure how should I make it because if I just stick it inside, it will print as Minimum is None.

smallest = None
largest = None
while True:
    num = input("Enter a number: ")
    if num == "done":
        break
    number = int(num)
    if largest is None:
        largest = number
    elif largest < number:
        largest = number
    elif largest > number:
        continue
    elif smallest is None:
        smallest = number
    elif smallest > number:
        smallest = number
    elif smallest > number:
        continue

print ("Maximum is", largest)
print ("Minimum is", smallest)

It could be awesome if you could help me following the same type of code I'm using here, not using min(), max() or other more advanced code since this is a begginer python course.

THANK YOU!!!! <3

You can write min() and max() function yourself (you need them only for 2 arguments):

def min_(a, b):
    return (a, b)[a > b]

It works, because you can index list (tuple) with bool value, which is automaically mapped into int. When you have both min and max implemented, the task is trivial.

If you are into learning, you could totally write everything yourself, including a sort algorithm, eg Bubble Sort:

def bubbleSort(lst):
    n = len(lst)

    for i in range(n - 1):
        for j in range(0, n - i - 1):
            if lst[j] > lst[j + 1]:
                lst[j], lst[j + 1] = lst[j + 1], lst[j]


my_list = [10, 1, 21212, 212, 44, -1]

Now, you could use

bubbleSort(my_list)

And then

min_list = my_list[0]
max_list = my_list[len(my_list) - 1]
print(min_list, max_list)

Which would yield

-1 21212

Letting aside learning purposes, better use the inbuilt functions like sort() , min() and max() as they are optimized.

Jason's right:

  • rather than elif smallest is None , which won't be tested if any of the first three tests are true, use if smallest is None -- largest > number is always true if the number is a candidate for being smallest, and both the continue and elif will mean the next lines don't get run.
  • if you want an explicit "does nothing" clause, you might want pass instead
  • you have a typo on the last test: you're testing > like the line above

I'd rewrite those tests something like:

    if largest is None:
        largest = number
    if smallest is None:
        smallest = number
    if number > largest:
        largest = number
    if number < smallest:
        smallest = number

(Note I've swapped the order of the variables: "the number is bigger than the largest" makes more sense to me than "the largest is smaller than the number")

Try this code:

largest = None
smallest = None
while True:
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        number = int(num)
    except:
        print ("Invalid Input")
        break
    if largest is None:
        largest = number
    elif largest < number:
        largest = number
    elif largest > number:
        pass
    if smallest is None:
        smallest = number
    elif smallest < number:
        pass
    elif smallest > number:
        smallest=number

print ("Maximum is", largest)
print("Minimum is", smallest)

I'm new too but my opinion is you used "continue" instead of "pass".They're different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM