Reputation: 27
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
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.
Upvotes: -1
Views: 34651
Reputation: 1
Here’s what worked for me:
x = int(input())
y = int(input())
z = int(input())
min_num = min(x, y, z)
print(min_num)
Upvotes: 0
Reputation: 1
num1 = int(input())
num2 = int(input())
num3 = int(input())
smallest_num = min(num1, num2, num3)
if (num1 < num2):
if (num1 < num3):
smallest_num = num1
elif (num2 < num1):
if (num2 < num3):
smallest_num = num2
elif (num3 < num2):
if (num3 < num1):
smallest_num = num3
print(smallest_num)
Upvotes: -1
Reputation: 1
I am currently doing this lab for my own homework, and one thing that is missing from their responses is the definition of the variable that specifies which of the three values is the lowest/minimum value: lowest = min(num1, num2, num3)
num1 = int(input())
num2 = int(input())
num3 = int(input())
lowest = min(num1, num2, num3)
if num1 < num2 and num1 < num3:
lowest = num1
elif num2 < num1 and num2 < num3:
lowest = num2
elif num3 < num1 and num3 < num2:
lowest = num3
print(lowest)
Upvotes: -1
Reputation: 1
num1 = int(input())
num2 = int(input())
num3 = int(input())
if (num1 <= num2 and num1 <= num3):
small = num1
elif (num2 <= num1 and num2 <= num3):
small = num2
else:
small = num3
print(small)
Upvotes: 0
Reputation: 1
In case anyone else needed help on this one in the future. :)
Original question: 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
num1 = int(input())
num2 = int(input())
num3 = int(input())
if (num1 < num2 and num1 < num3):
small = num1
elif (num2 < num1 and num2 < num3):
small = num2
else:
small = num3
print(small)
Upvotes: 0
Reputation: 1
This should work. I use C++, but it also looks like you didn't define smallest_num as a variable which could be the only problem.
if (num1 < num2 and num1 < num3):
small = num1
elif (num2 < num1 and num2 < num3):
small = num2
else:
small = num3
print(small)
Upvotes: 0
Reputation: 5597
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))
Upvotes: 2