Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

3.11 Lab: Smallest number

Write a program whose inputs are three integers, and whose output is the smallest of the three values.

If the input is:

7
15
3

The output is: 3

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the code I have come up with:

num1 = input()
num2 = input()
num3 = input()

if (num1 < num2):
    if (num1 < num3):
        smallest_num = num1
elif (num2 < num1):
    if (num2 < num3):
        smallest_num = num2
else:
    smallest_num = num3
print(smallest_num)

This code works for that input, however if you input "29, 6, 17" it returned no output with an error

NameError: name 'smallest_num' is not defined".

I have dinked around quite a bit and tried adding smallest_num = min(num1, num2, num3) however nothing has yielded a passing output.

>Solution :

The issue is that input() returns a string. So when you compare your variables, you’re doing string comparisons instead of numerical comparisons. So, you need to convert your input to integers.

num1 = int(input("Enter num1: "))
num2 = int(input("Enter num2: "))
num3 = int(input("Enter num3: "))

print(min(num1, num2, num3))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading