简体   繁体   中英

I have a problem with arranging None and input data to find largest and smallest value using python

Edit: Here are the test values (7, 2, bob, 10, and 4) and desired output

Invalid input Maximum is 10 Minimum is 2 for people who want them:)

Hello. So I got this problem where I have to take input from user and then when the type done the largest and smallest values come out. There was a small part where if you put wrong data it will tell you that but I got that "done" perfectly. But the part where I have to decide as largest and smallest is my issue. I setup Nonetype for each of the values but when i put it in the if statements it simply selects the first input as largest. I have attached my code below. Any modification could be of help. Thank you.

Largest = None
Smallest = None
while True :
    num = input("Enter a number: ")
    if num == "done" :
        break
    try :
        fnum = int(num)
    except :
        print("Invalid input")
        continue
    if Largest is None:
        Largest = fnum
    elif Smallest is None:
        Smallest = fnum
    elif fnum > Largest:
        fnum = Largest
    elif fnum < Smallest:
        fnum = Smallest
print("Maximum is",Largest)
print("Minimum is",Smallest)

Ideally, you can go with == (equals to) for the comparison between None

x = None
y = None
print(x is y)
print(x==y)

prints

True
True

Now, you have all the conditions as if and elif , so every time in the first iteration the Largest is assigned (because that is the first True condition) and in the second iteration Smallest is assigned (because that is the second True condition) and this logic may not be the case always.

What you can do is, set the largest and smallest at the same time, ie have the same reference point before the comparison.

Finally, you are not updating the smallest and the largest but doing it reverse (as pointed out in the comments).

Stitching it all together,

Largest = None
Smallest = None
while True :
    num = input("Enter a number: ")
    if num == "done" :
        break
    try :
        fnum = int(num)
    except :
        print("Invalid input")
        continue
    if Largest == None or Smallest == None:
        Largest = fnum
        Smallest = fnum
    elif fnum > Largest:
        Largest = fnum
    elif fnum < Smallest:
        Smallest = fnum
print("Maximum is",Largest)
print("Minimum is",Smallest)

There is so many ways that you can do this. You should try using an array. First, you create an array which will store all the numbers. If the user type "done", you store the maximun and the minimun number for the array in two variables. Really simple.

numbers = []

while True :
    num = input("Enter a number: ")
    if num == "done" :
        break
    try:
        num = int(num)
        numbers.append(num)

    except:
        print("Invalid input")
        continue

Largest = max(numbers)
Smallest = min(numbers)

print("Maximum is", Largest)
print("Minimum is", Smallest)

If you want to keep the Largest and Smallest as None you could perform a conditional expression to assign the values. This would result the first value being assigned to both Largest and Smallest and then only to Smallest of Largest if the value is bigger or smaller.

This is not the most optimized way to do this but it gets the job done.

Largest = None
Smallest = None
while True :
    num = input("Enter a number: ")
    if num == "done" :
        break
    try :
        fnum = int(num)
    except :
        print("Invalid input")
        continue
    Largest = fnum if Largest is None else fnum if fnum > Largest else Largest
    Smallest = fnum if Smallest is None else fnum if fnum < Smallest else Smallest

print("Maximum is", Largest)
print("Minimum is", Smallest)

Output with single number.

在此处输入图像描述

Output with your numbers.

在此处输入图像描述

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