3.11 Lab: Smallest number - python

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.

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

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)

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)

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)

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)

Related

Printing an extra number

I'm asking the user to enter a number, then printing every even number up to the number entered. problem is if the inputed number is odd then the program prints an extra number.
What's happening?
num1 = int(input("Please enter a number : "))
num2 = 0
while num1 > num2:
num2 += 2
if num2 % 2 == 0:
print(num2)
else:
num2 -= 2
print(num2)
Let's settle this problem first step by step
If else statement is not needed
Your if else checks if num2 is even, but since num2 will always start at 0, and then be incremented by 2, it will always be even.
So this is what your code currently does :
num1 = int(input("Please enter a number : "))
num2 = 0
while num1 > num2:
num2 += 2
print(num2)
This should explain better why the program prints an extra number for an even input number.
Correct version
A very simple way to correct this code, would be to slightly change the while condition and add +1 to num2
num1 = int(input("Please enter a number : "))
num2 = 0
while num1 > num2 + 1:
num2 += 2
print(num2)
So here, it will print even numbers from 2 to num1
If num1 is even, it stops at num2 == num1 + 1
If num1 is not even, it stops at num2 == num1 + 2
num1 = int(input("Please enter a number: "))
num2 = 0
while num1 > num2:
if num2 % 2 == 0:
num2 += 2
if num2 > num1:
break
print(num2)

How to use input function while defining a function [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
def max(num1,num2,num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num1 and num2 > num3:
return num2
elif num3 > num1 and num3 > num2:
return num3
elif num1 == num2 == num3:
return num1 == num2 == num3
else:
print("something went wrong.")
print(max(4,5,8))
I am a basic python programmer learning python,i have tried to define a function which prints out maximum of a three number,but how to use the input function to get the number as input.
you can just use something like
we are using int function because input function takes the number or string and makes it a string so we need integer version of them so we can compare them
num1 = int(input("what is your first number: "))
num2 = int(input("what is your second number: "))
num3 = int(input("what is your third number: "))
but it can give errors because you did not use try except so the proper way is
while True:
try:
num1 = int(input("what is your first number: "))
num2 = int(input("what is your second number: "))
num3 = int(input("what is your second number: "))
except:
print("give me integer or number or i do not know what type of data do you want")
else:
break
you can change what is in the last print statement
you can use that
it is still impovable you should check assert
i hope it is helpful

Hello I was confused about this code because purely from a technical stand point it should work

This is supposed to take three int values, and give you the LOWEST amount
num1 = int(input())
num2 = int(input())
num3 = int(input())
if (num1 < num2) and (num1 < num3):
print(num1)
elif num2 < num3
print(num2)
else:
print(num3)
I know it's messy and kinda hard to read but my point is when i put something like 7 15 3, it always outputs 15. Why?
I reprodcued papke's result: adding a colon to the end of the elif line fixed the syntax error and produced the output you expected.
Taking advantage of the built-in min function, you could refactor your code along these lines:
num1 = int(input())
num2 = int(input())
num3 = int(input())
print(min([num1, num2, num3]))
You were missing a colon after the elif. The code gives the expected answer:
num1 = int(input("Enter: "))
num2 = int(input("Enter: "))
num3 = int(input("Enter: "))
if (num1 < num2) and (num1 < num3):
print(num1)
elif num2 < num3:
print(num2)
else:
print(num3)

What is the detailed difference between two Python codes, both written to find the largest number among three inputs

How can I understand detailed difference between below two codes written to find the largest number among three numbers.
Code1:
def max_num(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3
return num2
else:
return num3
Code2:
def max_num(num1, num2, num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num1 and num2 > num3
return num2
else:
return num3
EDIT: Code2 could erroneously return num3 as max if num1 == num2, ie in [6, 6, 5].
The main difference, as commented above, is the use of "greater than" vs "greater or equal". I think the only place this would matter is when you have multiple values in the list which are equal, in which case Code2 would return the first instance, whereas Code3 would return the last.

Python GCD algorithms without using 'define'

I need to make two different types of codes to find GCD.
I'm not allowed to use def() because I didn't learn it yet.
This is my first code. I am not supposed to use the euclid method here.
I need to input two numbers together,
and set the smaller one to 'bound'.
num1, num2 = (input("input two integers to find GCD: ")).split()
num1=int(num1); num2=int(num2)
if num1 > num2:
bound == num2
elif num1 < num2:
bound == num1
i=2
while (i <= bound) :
if (num1 % i == 0) and (num2 % i == 0):
i == GCD
i=i+1
print("The GCD for %d, %d is %d."%(num1,num2,GCD))
This is my second code. Here, I need to be using the euclid method.
I have tried my best, but I can't catch my errors.
num1, num2 = (input("input two integers to find GCD: ")).split()
num1=int(num1)
num2=int(num2)
temp=num1%num2
while (num1 != 0):
num1=num2;
num2=tmp;
tmp=num1%num2
print("The GCD for %d, %d is %d."%(num1, tmp, num2))
GCD for your first code:
Edit: You were using == instead of =
num1, num2 = (input("input two integers to find GCD: ")).split()
num1=int(num1)
num2=int(num2)
if num1 > num2:
bound = num2
elif num1 < num2:
bound = num1
i=2
for i in range(1, bound+1):
if (num1 % i == 0) and (num2 % i == 0):
gcd=i
i=i+1
print("The GCD for %d, %d is %d."%(num1,num2,gcd))
GCD for your second code without using def using euclid method:
num1, num2 = (input("input two integers to find GCD: ")).split()
num1 = int(num1)
num2 = int(num2)
while num1 != num2:
if num1 > num2:
num1 = num1 - num2
else:
num2 = num2 - num1
print(num1)

Categories

Resources