0

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)
wade07
  • 17
  • 4
  • Can you specify what inputs you give and what you expect, along with what you get on your current implementation? – Kota Mori Aug 14 '20 at 05:03
  • Note that towards the end of your code, your assignments are backwards. Instead of assigning the value of fnum into Largest or Smallest, you're doing the opposite. – Blue Star Aug 14 '20 at 05:06
  • @TamirIlan doesnt fnum = Largest mean youre putting fnum in Largest? or have i been knowing this wrong – wade07 Aug 14 '20 at 05:10
  • No, in fact, `fnum = Largest` will put the value held by Largest into fnum. – Blue Star Aug 14 '20 at 05:11
  • thank you so much @TamirIlan that literally fixed it i never realised it lmao – wade07 Aug 14 '20 at 05:19

3 Answers3

0

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, i.e 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)
lu5er
  • 3,229
  • 2
  • 29
  • 50
  • 1
    You should use `is` for `None` – Cireo Aug 14 '20 at 05:37
  • @Cireo, not necessarily. You might be confusing it with `nan`. `None` is totally fine with `==`. – lu5er Aug 14 '20 at 05:39
  • 2
    Sure, it works here but it just isn't recommended. Don't change what they were doing well. https://stackoverflow.com/questions/3257919/what-is-the-difference-between-is-none-and-none – Cireo Aug 14 '20 at 08:04
0

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)
0

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.

enter image description here

Output with your numbers.

enter image description here

Karhu12
  • 41
  • 10