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
Related
I'm new to lists in python and trying to write a code where one inputs 10 numbers using a list and the following is performed:
Copy the number which is even to a new list named "EvenList" and
output that new list.
Output the average of the numbers stored in the new list.
Here is my code:
List=[]
EvenList=[]
totalnum=0
count=0
for i in range(10):
List.append(int(input("Enter a number: ")))
while List[i]%2==0:
EvenList.append(List[i])
totalnum=totalnum+List[i]
count=count+1
print(EvenList)
average=totalnum/count
print("Average: ", average)
Whenever I run this module, after inputting my values my outputs (both the EvenList and average) aren't printed. Here's the output I get:
Example 1:
Enter a number: 1
Enter a number: 2
Example 2:
Enter a number: 1
Enter a number: 1
Enter a number: 1
Enter a number: 1
Enter a number: 1
Enter a number: 1
Enter a number: 1
Enter a number: 1
Enter a number: 1
Enter a number: 2
By further analysis, I realized whenever an even number was inputted, that's when the code gave an empty output. So I'm speculating my error lies in line 8 - 11 (there may be even more).
I so far changed my inital code:
List[i]=int(input("Enter a number: ")) and EvenList[index]=(List[i])
to
List.append(int(input("Enter a number: "))) and EvenList.append(List[i]) respectively - which I'm still confused as to why the intial code isn't considered to be correct since I thought they did the exact same thing [if someone could explain, it'd be very appreciated] - but that didn't fix this error.
It's because in while loop condition, if the number is even it gets stuck in infinite loop, instead of while add if:
List=[]
EvenList=[]
totalnum=0
count=0
for i in range(10):
List.append(int(input("Enter a number: ")))
if List[i]%2==0:
EvenList.append(List[i])
totalnum=totalnum+List[i]
count=count+1
print(EvenList)
average=totalnum/count
print("Average: ", average)
Here's alternative using list comprehension, sum(), and len() as substitutes of creating EvenList, totalnum, count, and average :
List = [int(input("Enter a number: ")) for i in range(10)]
EvenList = [i for i in List if i % 2 == 0]
average = sum(EvenList)/len(EvenList)
print(EvenList); print("Average: ", average)
Or list comprehension in list comprehension:
EvenList = [i for i in [int(input("Enter a number: ")) for i in range(10)] if i % 2 == 0]
average = sum(EvenList)/len(EvenList)
print(EvenList); print("Average: ", average)
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)
The problem i am having with this question is that the code i have written is showing a logical error. Here is the code i have written:
x = int(input("Please enter the base number: "))
n = int(input("Please enter the number of terms: "))
s = 0
factorial = 1
for i in range(1,n+1,2):
factorial = factorial*i
s = (x**i) / factorial
#using f string to format the numbers to a fixed number of decimal places
print(f"{s:.6f}", end='+')
I used the for loop to only show odd values of index, because that is the requirement in the question. My output is coming as follows:
Please enter the base number: 2
Please enter the number of terms: 4
2.000000+2.666667+
We don't have to actually find the sum, only display all the individual addends separated by a plus sign. What changes should i make within the code to get the required results?
My required output would look something like this:
Please enter the base number: 2
Please enter the number of terms: 4
2.000000+1.333333+0.266666+0.025396
Just change to
for i in range(1,2*(n)+1,2):
from
for i in range(1,n+1,2):
Now, the output is:
Please enter the base number: 2
Please enter the number of terms: 4
2.000000+2.666667+2.133333+1.219048+
Also, the method of calculating factorial is wrong as it skips half of the terms, when you jump i from 1 to 3 to 5, so that 2, 4, 6.. are getting missed.
So, you can do:
if i > 1:
factorial = factorial*i*(i-1)
elif i == 1:
factorial*i
So, the final code would be:
x = int(input("Please enter the base number: "))
n = int(input("Please enter the number of terms: "))
s = 0
factorial = 1
for i in range(1,2*n+1,2):
if i > 1:
factorial = factorial*i*(i-1)
elif i == 1:
factorial*i
s = (x**i) / factorial
#using f string to format the numbers to a fixed number of decimal places
print(f"{s:.6f}", end='+')
Factorial is in the loop but the loop does i=1,3,5 and not i=1,2,3,4,5, that's might be a problem. If the "number of terms" : "2.000000+2.666667" is two so in the loop your range avec to be until n*2 and not n but careful because the factory will be changed.
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
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)