Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Sorry if it's an easy question. But what I need to do is have a script which inputs 5 numbers into an array. For instance the user will be asked 5 times to input numbers from which the mean and sum will be calculated. Is placing the users inputs into an array the easiest way to do it and if so how?
counter = 0
number_array = []
while counter < 5:
num = float(input("Enter a number: "))
number_array.append(num)
counter += 1
total = 0
for number in number_array:
total = total + number
average = total / 5
print("The sum is: ", total)
print("The average is ", average)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
Im already done with the part before the highlighted part, but i can't understand the part which is highlighted and i dont want to look at the code.
Something like that:
number = int(input("Your number: "))
while number != 1 :
number = collatz(number)
As I understand, it should be something along the lines:
def collatz(number):
if number // 2 == 0:
print(number // 2)
return number // 2
else:
print(3 * number + 1)
def iterate_program:
number = int(input())
while number != 1:
number = collatz(number)
And then you just call iterate_program.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
please tell my mistake
i m getting the index of the number whose leftsum=rightsum wrong
L=[]
n=int(input("enter the number of values to be added to the list: "))
for i in range(0,n):
k=int(input("enter the values: "))
L.append(k)
for i in range(0,n):
if sum(range(L[0],L[i])) == sum(range(L[i+1],L[-1])):
print(L.index(L[i]))
break
else:
print("0")
For starters, you've used range(a,b), which returns all the numbers from a to b-1
Try this:
L=[]
n=int(input("enter the number of values to be added to the list: "))
for i in range(0,n):
k=int(input("enter the values: "))
L.append(k)
for i in range(1,n):
if sum(L[0:i]) == sum(L[i+1:]):
print(L.index(L[i]))
break
elif i==n-1:
print(0)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
It's my 2nd week in programming in Python and have never programmed anything before. appreciate step by step.
I don't know where to start.
try:
count = 0
while True:
user = int(input('Insert Number: '))
count += user
if user == 0:
break
print(count)
except ValueError:
print('Please Insert Numbers Only!')
Here a start, use a while loop for the input. I'll leave the summation part for you unless you need further help:
cc = int(input("Enter an integer to sum. Press 0 to to end and start the summation:"))
while cc != 0:
cc = int(input("Enter an integer to sum. Press 0 to to end and start the summation:"))
def is_int(num):
try:
return int(num)
except:
return False
total = 0
while True:
in_user = is_int(input("input integer: "))
if in_user is not False:
total += in_user
if in_user is 0:
break
print(total)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Okey, I actually don't really know how to print a pyramid in python and even worse I need to get this:
Pyramid of n numbers
The length of the pyramid is asked to the user
Here's the code.
n = int(input("\n Enter the lenght of the triangle.. "))
for i in range(n):
for j in range(n - i):
print(" ", end = " ")
for k in range(i):
print("*", end = " ")
print()
Here is the output
One of the options:
import numpy as np
a = int(input())
def pyram(a):
n = np.zeros(a)
hal = int((a+2)/2)+1
n[:(hal-1)] = range(1,hal)
n[(hal-1):] = np.array(range(1,hal-1))[::-1]
print(n)
for i in range(1,a*2,2):
pyram(i)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am wondering how to create different output with one input statement in a for loop (see my code below). For instance, how should I make each input statement read Enter number 1:, Enter number 2: and so on. Should I have only one input statement or multiple?
times = int(input("Enter how many numbers you want to sum"))
sum = 0
for i in range(0, times):
numInput = int(input("Enter number"))
sum = sum + numInput
print("The total is", sum)
One input is enough because you can change the string in your input prompt like this:
times = int(input("Enter how many numbers you want to sum? "))
sum = 0
for i in range(1, times+1):
numInput = int(input("Enter number {}:".format(i)))
# or use below code:
# numInput = int(input("Enter number %d:" % (i)))
sum = sum + numInput
print("The total is", sum)
To learn more about formatting python strings please refer to this link:
https://pyformat.info/