I have seen things on how to sort out a list but I am having trouble figuring out how print the smallest number that a user inputs from from a total of 3 numbers that they input. I have found some answers on here for other languages and I am sure this has been asked before but I have had trouble finding anything that helps this particular assignment.
user_input = int(input())
smallest_number =
print(smallest_number)
a = int(input('Enter number: '))
b = int(input('Enter number: '))
c = int(input('Enter number: '))
lst = [a,b,c]
smallest_number = min(lst)
print(smallest_number))
Or a shorter way would be
smallest_number = min([int(input('Enter Number: ')) for count in range(3)])
print(smallest_number)
min just returns the smallest number
Input:
Enter Number: 4
Enter Number: 5
Enter Number: 7
Output
4
Found some help elsewhere but let me post what we came up with for other people to read later.
print('enter 3 integers')
user_input_1 = int(input())
user_input_2 = int(input())
user_input_3 = int(input())
inputs = [user_input_1, user_input_2, user_input_3]
smallest_number = min(inputs)
print(smallest_number)
This solution works for any number of inputs.
list = map(int, input().split())
print(min(list))
Input: 4 2 5
Output: 2
Input: 9 7 5 3 4
Output: 3
Related
I'm starting next month full stack developer and im doing some practicing
i started with Python and i want to make some code
with while loop that will ask the user to input how many integers they want
and i want to calculate all the numbers
im doing something wrong not sure what
thanks in advance
oz
example:
number = int(input('Enter how many integer: '))
my_list = [number]
while len(my_list) < number:
user_input = int(input('Enter a integer: '))
my_list.append(user_input)
print(user_input+number)
print(my_list)
number = int(input('Enter how many integer: '))
my_list = []
while len(my_list) < number:
user_input = int(input('Enter a integer: '))
my_list.append(user_input)
print(user_input, ' ' ,number)
print(my_list)
how can i print the sum of all the numbers between any two given numbers in python.
i am not allowed to use functions like sum(). i can only use while or for loop.
i have a code which does the job but the problem is it also prints result every time after adding two numbers but i only want it to print final sum of all the numbers.
Here's the code:
...
a = int(input("Please enter a number"))
b = int(input("Please enter a number"))
n = 0
for x in range(a+1,b):
n+=x
print(n)
...
thanks
There is an Indentation Error just check the line below the for loop and give the proper indentation. You can refer to my code
a = int(input("Please enter a number: "))
b = int(input("Please enter a number: "))
n = 0
for x in range(a+1,b):
n+=x
print(n)
This question already has answers here:
How do I find the maximum of 2 numbers?
(12 answers)
Closed 3 years ago.
i'm new to programming and this has got me stumbling and i was thinking of doing something like this but i can't got further
num1 = int(input('What is the first number?:'))
num2 = int(input('What is the second number?:'))
num3 = int(input('What is the third number?:'))
[[After this my mind is thinking of elif statements and using [and,or]]
Add all of your variables to a list and then you can use the max function like so max(lista)
num1 = int(input('What is the first number?: '))
num2 = int(input('What is the second number?: '))
num3 = int(input('What is the third number?: '))
lista = [num1, num2, num3]
biggest = max(lista)
print(f"{biggest} is the largest value.")
(xenial)vash#localhost:~/python/stack_overflow$ python3.7 max.py
What is the first number?: 10
What is the second number?: 3
What is the third number?: 8
10 is the largest value.
Just for a little bonus, didn't include handling TypeErrors but want to give you some ideas where you can go with this little project:
while True:
numbers = int(input("How many numbers would you like to enter: "))
values = []
for i in range(numbers):
if i == numbers - 1:
values.append(int(input(f"Enter 1 number: ")))
else:
values.append(int(input(f"Enter {numbers - i} numbers: ")))
print(f"\nThe largest number entered was {max(values)}")
(xenial)vash#localhost:~/python/stack_overflow$ python3.7 max.py
How many numbers would you like to enter: 5
Enter 5 numbers: 10
Enter 4 numbers: 8
Enter 3 numbers: 29
Enter 2 numbers: 13
Enter 1 number: 22
The largest number entered was 29
num1=int(input('What is the first number?:'))
num2=int(input('What is the second number?:'))
num3=int(input('What is the third number?:'))
if(num1>num2 and num1>num2):
print(num2, 'is the largest value')
elif(num2>num1 and num2>num3):
print(num2, 'is the largest value')
else:
print(num3, 'isenter code here the largest value')
Python's max(list) does the job for you.
If you wish to create a function that accepts arbitrary amount of inputs, you can create a function like this.
def my_function(*args):
return max(args)
I spent all day on this code. It failed.
def output (n):
n = int(input('Enter a number: ')
while n != 0:
if n % 5 == 0:
print(n, 'Yes')
n = int(input('Enter a number: ')
if n == 0
output = range(1, int(input('Enter a number: '))+1)
print (output)
output (n)
Question is:
let user enter integers to determine if multiple of 5.
If it is then keep count that will keep a sum of all numbers that are multiples of 5.
Task done using a loop in a function and the loop will terminate when a value of 0 is entered.
when the loop terminates, return the count of how many numbers that were multiple of 5s.
After complete, NEXT:
pass the variable sum_multiple_five to another function called print_result() and still
print the same message but now the print will be done in its own function.
def test(n):
if not n%5:
print (n,'Yes')
return n
else:
print (n,'No')
return 0
total = 0
while True:
n = int(input('Enter a number: '))
if not n:
break
total+=test(n)
print(total)
def sum_multiple_five(n):
count = 0
if n == 0: #initial check if the first input is 0 if it is return count as 0
return count
while n != 0: # not leaving this while loop until n is 0
if n % 5 == 0: #if divisible by 5 increment count by 1 otherwise get new input
count = count + 1
n = int(input('Enter a number: ')) # Update n's value from user input. This is important because n is what the while loop is checking.
return count #when the while loop exit as user input 0 we return count
def print_result(answer):
# print(answer)
print( str(answer) + " numbers were multiple of 5s")
def init():
n = int(input('Enter a number: ')) #get user input and store it in variable n
print_result(sum_multiple_five(n)) #call sum_multiple_five() function and use n as an input. then give the returned int to print_result function
init() #call the function init()
result:
Enter a number: 10
Enter a number: 10
Enter a number: 100
Enter a number: 50
Enter a number: 5
Enter a number: 9
Enter a number: 7
Enter a number: 10
Enter a number: 4
Enter a number: 15
Enter a number: 5
Enter a number: 8
Enter a number: 2
Enter a number: 0
8 numbers were multiple of 5s
Is it possible to write a clean code in python to print something like:
Introduce the number of lists you want to have: 3
Introduce how many numbers you want it to have: 3
Number: 1
Number: 2
Number: 3
[1,2,3]
Introduce how many numbers you want it to have: 4
Number: 1
Number: 2
Number: 5
Number: 9
[1,2,5,9]
Introduce how many numbers you want it to have: 5
Number: 1
Number: 7
Number: 2
Number: 8
Number: 3
[1,7,2,8,3]
This is my try at it, but it only works for a list, as I don't know how to add multiple lists:
v1=[]
n=input ("Introduce how many numbers you want it to have: ")
def introdTast():
print("Introduce the numbers: ")
for i in range(0,n):
v1.append(input())
introdTast()
print "v1 =",v1
print "\n"
Your answer is here: The Python Tutorial
However, here you go:
lists = int(raw_input('Introduce the number of lists you want to have: '))
for i in xrange(lists):
numbers = int(raw_input('Introduce how many numbers you want it to have: '))
l = []
for j in xrange(numbers):
l.append(int(input('Number: ')))
print l