I'm trying to calculate an average of the numbers that are inputted following the "Enter the number" part. I don't know how to add the inputted numbers as they aren't set variables. When I run it, it says 'int object is not iterable'
I thought about asking for each number separately, but I wouldn't know how to make it repeat the set number of times for each time the question is asked. So I use the for loop, but by using the for loop, I can't set numbers to variables, so I can't add them.
Apparently sum function can help, but nothing I've looked at shows me how to use it very well.
how_many = int(input("How many numbers are there?"))
for counter in range (1, (how_many + 1)):
numbers = int(input("Enter number:"))
sum1 = sum(numbers)
av = sum1 / how_many
The result of the code is supposed to show the average of the numbers inputted but I can't figure out how to work out the total?
You can do it like this.
how_many = int(input("How many numbers are there?"))
total_sum=0
for counter in range (1, (how_many + 1)):
number = int(input("Enter number:"))
total_sum = total_sum+number
avg = total_sum / how_many
print(avg)
sum calculates the total in an iterable, eg list.
If we loop, and add the number to a list each time, we can calculate the average at the end.
This should do the trick:
how_many = int(input("How many numbers are there?"))
numbers = []
for counter in range (how_many):
numbers.append(int(input("Enter number:")))
total = sum(numbers)
av = total / how_many
print("Average:", av)
Output:
How many numbers are there?5
Enter number:1
Enter number:2
Enter number:3
Enter number:4
Enter number:5
Average: 3.0
Related
I'm trying to get the sum of numbers that a user inputs in a loop, but I can't get it to include the first number input - here's what I have so far
number = int(input("Enter a number"))
total = 0
while number != -1:
number = int(input("Enter another number"))
total += number
else:
print(total)
Probably something easy I'm missing but I'm stumped ( i am a beginner as you can tell)
I have tried changing the name of the first variable number but I end up in a constant loop even when number = -1
number = int(input("Enter a number"))
total = 0
while number != -1:
total += number
number = int(input("Enter another number"))
else:
print(total)
Just move the summation one line above.
A programme that asks the user to enter a number, multiply it by 2, and multiply the answer by 2, and so on, as many times as you want
Lets say the number is 100
Expected result
100×2= 200
200×2=400
400×2=800
800×2=1600,
and as many times i want
number = input("What number?")
iteration = input("How many times?")
for i in range(iteration):
new_number = number * 2
print(new_number)
number = new_number
What I want to do, is to write a program that given a sequence of real numbers entered by the user, calculates the mean. Entering the sequence should be finished by inputting ’end’. To input more than 1 number I try:
num=input("Enter the number: ")
while num!="end":
num = input("Enter the number: ")
But I don't want the program to "forget" the first 'num', because I'll need to use it later to calculate the mean. What should I write to input and then use more than one value, but not a specific number of values? User should can enter the values until they enter 'end'. All of values need to be use later to calculate the mean. Any tip for me?
First, define an empty list.
numbers = []
Then, ask for your input in an infinite loop. If the input is "end", break out of the loop. If not, append the result of that input() to numbers. Remember to convert it to a float first!
while True:
num = input("Enter a number: ")
if num == "end":
break
numbers.append(float(num))
Finally, calculate the mean:
mean = sum(numbers) / len(numbers)
print("Mean: ", mean)
However, note that calculating the mean only requires that you know the sum of the numbers and the count. If you don't care about remembering all the numbers, you can just keep track of the sum.
total = 0
count = 0
while True:
num = input("Enter a number: ")
if num == "end":
break
total += float(num)
count += 1
print("Mean: ", total / count)
Mean is sum of all nos/total nos. Let's take input from user and add it to list, because we can easily use sum() function to find sum and len() function to find total nos. Your code:
obs=[]
while True:
num=input("Enter number: ")
if num=="end":
break
else:
obs.append(int(num))
print("Mean: ",(sum(obs)/len(obs)))
I am creating a python program, to allow a user to input numbers and find the total, average, highest and lowest of the numbers inputted. my teacher has told me to improve the program by removing the first and last number inputted into the list.I am confused on how doing so,as the list does not exist until the user has inputted the numbers into it.
THIS IS MY CODE SO FAR:
totalList=[]
totalList = totalList[1:-1]
TotalNum = int(input("Please enter how many numbers are to be entered:"))
Change=TotalNum
Input=0
Total=0
while TotalNum>0:
TotalNum = TotalNum - 1
Input = int(input("Enter Number: "))
totalList.insert(-1,Input)
Total = Total +Input
print("Total:" ,Total)
print("Average:",Total/Change)
print("Highest:",max(totalList))
print("Lowest:",min(totalList))
I want to store integers and return the sum, using random.randint() for random values.
The code looks like this:
import random
val = int(input('How many numbers?: '))
for i in range(val):
print(random.randint(1,99))
I need to print the integers to the console and then sum them and return the final sum.
Example:
How many numbers?: 4
84
50
35
35
Final number: 204
I need it to work for an infinite amount of numbers too.
right now you are not keeping track of the numbers you are printing out in val.
In order to keep track and sum up all of the numbers, you need to store them in an array.
import random
val = int(input('How many numbers?: '))
#random number is stored as val
for i in range(val):
#this will run from 1-val
print(random.randint(1,99))
#these numbers are simply being printed, not stored
So what we could do is store the numbers, and then print them
sum=0
for i in range(val):
num=(random.randint(1,99))
sum=sum+num
print"random number:", num
print"The total sum is:", sum
Use a list to store the numbers.
Use sum() to add them up.
Use join() to print them out.
import random
val = int(input('How many numbers?: '))
numbers = [random.randint(1,99) for i in range(val)]
print('\n'.join(str(i) for i in numbers))
print('Final number: {}'.format(sum(numbers)))
Example output:
How many numbers?: 5
60
70
51
65
18
Final number: 264
You will not be able to provide a sum for an infinite series of random numbers, nor will you be able to store them.
This solution only covers dealing with no end point, there are good solution already for the other aspects of the question.
There is no way to sum up an infinite amount of numbers but you can just keep generating numbers until the program receives a KeyboardInterrupt
nums = []
try:
while True:
nums.append(random.randint(1,99))
except KeyboardInterrupt:
pass
total = sum(nums)
amount_finished = len(nums)
Or to support a potential but not necessary limit you can use:
str_limit = input("enter the limit (accepts 'inf') ")
if str_limit=="inf":
limit = float("inf")
print ("you will need to stop the program with a KeyboardInterrupt")
else:
limit = int(str_limit)
nums = []
try:
while limit>0:
nums.append(random.randint(1,99))
limit-=1
except KeyboardInterrupt:
pass
total = sum(nums)
amount_finished = len(nums)