0

I have tried to find smallest of three numbers but for some values the answer is wrong like 10,30,4 for example and it shows smallest number to be 10.

 num1 = input("Enter a number: ")
num3 = input("Enter a number: ")
num4 = input("Enter a number: ")
if (num1 < num3) and (num1 < num4):
    print(num1)
elif (num3 < num4) and (num3 < num1):
    print(num3)
else:
    print(num4)
Nitin
  • 11

3 Answers3

1

You are comparing text. You need to convert the input to int objects before comparing.

num1 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
num4 = int(input("Enter a number: "))
if (num1 < num3) and (num1 < num4):
    print(num1)
elif (num3 < num4) and (num3 < num1):
    print(num3)
else:
    print(num4)
nosklo
  • 217,122
  • 57
  • 293
  • 297
1

Problem: input() returns a string, and strings are compared lexicographically. So you should convert it into an integer, you can do this with int(..):

num1 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
num4 = int(input("Enter a number: "))
if (num1 < num3) and (num1 < num4):
    print(num1)
elif (num3 < num4) and (num3 < num1):
    print(num3)
else:
    print(num4)

But nevertheless this is not a good way to calculate the minimum. What will you do if you have to calculate the minimum of 50 integers? Construct a hug amount of checks? Computer scientist Edsger W. Dijkstra once said:

"Two or more? Use a for!" - Edsger W. Dijkstra

So in case you have to do work multiple times, you better use a for loop to handle that. The skeleton of such code fragment looks like:

for i in range(3):
    # ...
    pass

The body of the for loop (here ...) will be repeated three times. Now what work can we put into the for loop? Of course we can query the user for input. So we can write:

for i in range(3):
    number = int(input("Enter a number: "))
    # ...

but now we still have to calculate the minimum. A way to handle this is by using an accumulator: a variable that is maintained through iteration, and updated accordingly. Here an accumulator can be the thus far obtained minimum. Since initially we have no minimum, we can initialize it with None:

thus_far_minimum = None

for i in range(3):
    number = int(input("Enter a number: "))
    # ...

Now in case thus_far_minimum is None, we can assign the number we have queried from the user. Furthermore in case thus_far_minimum is no longer None (so it is initialized), but number is less than thus_far_minimum, we can alter thus_far_minimum such that it now has the minimum. For example:

thus_far_minimum = None

for i in range(3):
    number = int(input("Enter a number: "))
    if thus_far_minimum is None or number < thus_far_minimum:
        thus_far_minimum = number

print(thus_far_minimum)

at the end we can print the thus_far_minimum since it is the minimum over all the numbers we have seen.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
0

With input(), you get strings, so convert the input values to integers.

num1 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
num4 = int(input("Enter a number: "))

Set a variable to num1, then reset it as you compare num1 to num3 and num4.

smallest = num1

if num3 < smallest:
        smallest = num3    
if num4 < smallest:
        smallest = num4
        if num3 < num4:
            smallest = num3

print(smallest)
srikavineehari
  • 2,502
  • 1
  • 11
  • 21