The explanation is below:
def displaySortedNumbers(num1, num2, num3):
smallest = num1
if num2 < smallest:
smallest = num2
if num3 < smallest:
smallest = num3
return smallest
def main():
num1, num2, num3 = eval(input("Enter three numbers seperated by commas:"))
print("The numbers are,",displaySortedNumbers(num1, num2, num3))
main()
After the three numbers are entered, the smallest number prints out but the rest of the numbers do not follow. I need the numbers to print out from smallest to largest. I'm not sure what I did wrong.
Your function is only returning the one smallest number of the provided three, you may consider using a list and sorting it instead if that works for you.
def displaySortedNumbers(num1, num2, num3):
s = ""
for c in sorted([num1, num2, num3]):
s += " " + str(c)
return s
The sorted() function takes an iterable argument and returns it sorted by a key, however in this case, if you are just sorting it in increasing order, you do not need to input a key.
In your return statement there is only ´smallest´, not the other variables.
You can store the values in a list, sort it and then return that list, just like this
def displaySortedNumbers(num1, num2, num3):
list = [num1, num2, num3]
list.sort()
return list
In your return statement, you only return one of the three numbers, the one you deem the smallest. But your function is expected to return all three numbers sorted. I'm guessing you can't use the built-in sorted() function and so you need to program the sort manually. You can do a simple bubble sort on 3 numbers by changing your function to be the following:
def displaySortedNumbers(num1, num2, num3):
if num2 < num1:
num1, num2 = num2, num1
if num3 < num2:
num2, num3 = num3, num2
if num2 < num1:
num1, num2 = num2, num1
return num1, num2, num3
This will print all three numbers, properly sorted.
If you CAN use built-in functions, you could simply say:
def displaySortedNumbers(num1, num2, num3):
return sorted((num1, num2, num3))
Try to rewrite like this:
if smallest > num2:
smallest = num2
elif smallest > num3:
smallest = num3
Related
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)
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)
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.
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)
I can't seem to make this program to run. There's no error, there's just nothing showing up when I run it. Any help is appreciated1
#Return multiple values
def load():
name=input("enter a name")
num1=int(input("Enter num1: "))
num2=int(input("Enter num2: "))
num3=int(input("Enter num3: "))
return name, num1, num2, num3
def calc(num1, num2, num3):
sum=num1, num2, num3
avg=sum/3
return sum, avg
def output(name, num1, num2, num3, avg, sum):
print("Your name is: ", name)
print("The 3 numbers are: ", num1, num2, num3)
print('The sum is: ',sum )
print("The average is: ", avg)
def main():
name, num1, num2, num3=load()
sum, avg=calc(num1, num2, num3)
output(name, num1, num2, num3, sum, avg)
You need to call main() function, this line will probably throw an exception:
sum=num1, num2, num3
avg=sum/3
change it to:
sum=num1 + num2 + num3
avg=sum/3
As a friendly commenter noted earlier, you were not calling your main() routine (see last line). You had also mixed the sequence of sum and avg calls in the signature of output. Furthermore sum is a Python built-in function. Avoid those. I used raw_input because I'm using Python 2.x, don't let that confuse you. My use of Python 2.x is also the reason for the extra parentheses you'll see in my output.
Overall you were very close to a working solution, nice job for a Beginner!
#Return multiple values
def load():
name=raw_input("enter a name: ")
num1=int(raw_input("Enter num1: "))
num2=int(raw_input("Enter num2: "))
num3=int(raw_input("Enter num3: "))
return name, num1, num2, num3
def calc(num1, num2, num3):
sum1=num1+num2+num3 # sum is a python keyword
avg=sum1/3.0 # avoiding integer division in case you are using python 2.x
return sum1, avg
def output(name, num1, num2, num3, sum1, avg):
print("Your name is: ", name)
print("The 3 numbers are: ", num1, num2, num3)
print('The sum is: ',sum1 )
print("The average is: ", avg)
def main():
name, num1, num2, num3=load()
sum1, avg=calc(num1, num2, num3)
output(name, num1, num2, num3, sum1,avg)
main()
Output:
enter a name: Anton
Enter num1: 1
Enter num2: 2
Enter num3: 3
('Your name is: ', 'Anton')
('The 3 numbers are: ', 1, 2, 3)
('The sum is: ', 6)
('The average is: ', 2.0)