I'm a total newbie on Python and I would like to calculate the arithmetic average.
a = [int(i) for i in input().split()]
average=sum(a)/len(a)
print('The average is:' ,average)
I'm aware that such code do solve my problems but that is not exactly what I'm looking for.
I want the user to be able to type the number of terms of the arithmetic average and I would like him to be able of typing them separatley on different lines. So I thought the right thing to use was For Loop. I came out with something like this:
n = input('Number of terms')
for i in range (1,int(n)+1):
a=input('Term number '+str(int(i))+': ')
I know that all I need to do know is to find a way to sum all values of a typed on each loop and divide this number by int(n) but I have no idea how to do that.
Can you guys help me with that?
Thanks everyone!
n = input('Number of terms')
acc = 0
for i in range(1,int(n)+1):
a=input('Term number '+str(int(i))+': ')
acc += float(a)
print('The average is ',acc/int(n))
The idea is to create an accumulator variable acc to which the entered numbers are added. After the loop acc is equal to the sum of all numbers entered. Divide it by the number of terms and you get the arithmetic average.
Try:
n = int(input('Number of terms'))
sum = 0
for i in range (1,n+1):
a=int(input('Term number '+str(i)+': '))
sum += a
avg = sum/n
Related
my 6th grade teacher asked me to code a program in python that input any amount of a number and it outputs the mean so i tried and theirs a problem with the print statement here's the code.i even tried to replace the float(b) statements to float(1) statements but that didn't work either
n = int(input("Enter number of elements :"))
b = input("\nEnter the numbers :")
sum = (float(b)+float(b)+float(b) / float(n)
print("\nThe mean is -",sum())
Please give space separated input here :
input = list(map(float ,input('Enter number of elements : ').split(' ')))
mean = sum(input)/len(input)
print(mean)
: Output :
Enter number of elements : 10 20 30
20.0
i would do something like this.
I assumed you wanted the arithmetic mean and not the geometric one, so the first thing to do is to get the number of values the user wants to input and store it in the variable n. The you iterate over a range n and you sum the inputs to k.
The you divide k by n.
n, k = int(input("Enter number of elements:")), 0
for i in range(n):
k += float(input("Enter value number {}".format(i+1)))
k /= n
print(k)
if you want to use the "cool" way you can do something like this:
n = int(input("Enter number of elements:"))
k = sum([float(input("Enter value number {}".format(i+1))) for i in range(n)]) / n
print(k)
There is an syntax error and logical error in this code.
You haven't closed the opening bracket, It should be like this
sum = (float(b)+float(b)+float(b)) / float(n)
If you want to calculate the mean of 3 values why are you adding the same value for three times here
(float(b)+float(b)+float(b))
Insted of using same values you can get each values from for loop like this
for i in range(3):
num = float(input())
total += num
I want to calculate the median of a range of numbers I input. The range should be between 1 and n+1 (n is the input in my case). I made some research and saw that you can use the inbuild statistics.median. But I can't make it work.
import.statistics
n = int(input("Please input your number: "))
range_1 = range(1, n+1)
list_1 = list(range_1)
I created a list with all values between 1 and n+1 and now I want to calculate the median.
print(statistics.median(list_1))
This is my error I don't know how to print the median. What am I doing wrong? I can feel that it's really simple. Thank you for your help.
Do it like this.
import statistics
number = int(input("enter the number :"))
range1 = list(range(1, number+1))
median1 = statistics.median(range1)
print("Median of the given range is :", median1)
This is a work in progress code. As you can tell, I am a complete beginner and been trying new things. I usually get an error and I try and fix that but here this one is not giving me any error. It's just that the answer to the average is a wrong number. Without any error, its hard for me to know what's wrong with the code. Any suggestion?
sum = 0.0
count = 0
ids = [ ]
scores = [ ]
ids = eval(input("Enter an id number:"))
if ids <= 0.0:
print("Can't be 0 or a negative number, please enter a number greater than 0")
exit(1)
scores = eval(input("Enter a score:"))
while ids >= 0.0:
ids = eval(input("Enter another id number (or 0 to quit):"))
if ids <= 0.0:
break
scores = eval(input("Enter another score:"))
sum = sum + scores
count = count + 1
avg = sum / count
for i in range(ids):
print(i)
print("The average of the scores is", avg)
There are several mistakes in your code.
First of all in python you must use right indentation.
Next you have declared ids = [ ] and scores = [ ] without any reason
Why do you use ids?
Instead of eval(input()), use int(input())
A simple code to calculate average:
scores = list(map(int, input('Enter Space Separated Elements : ').split()))
print('Average is ' + str(sum(scores) / len(scores)))
Explanation:
Learn about input here
Learn about input().split() here
Learn about map() here
As map returns a map object, we must convert it to a list
Learn about sum, len and other built-in functions here
Hope this helps!
I wrote this program to find sum and average, and got "Variable not defined" error. Can anyone solve it?
a=int(input("Total Numbers you will input for calculation: "))
Sum = 0 # Running Totalis b
for x in range (a-1):
c=int(input("Enter your input number {0}.".format(x+1)) # c is for next number
Sum = Sum + c
Total=b/10 # For Total
print("Total sum of the number you entered is {0} and their average is {1},".format(b,d))
In your code that prints out the sum and average, you refer to the variables b and d, neither of which actually exist. That is the cause of your immediate problem, the "Variable not defined" issue.
The sum of the numbers is stored in the Sum variable which you should use rather than b. The average is not calculated anywhere unless your calculation for Total is meant to do this but, if that's the case, you should choose a more appropriate name and calculate it correctly (dividing by a rather than by ten).
On top of that, your use of range is incorrect. You should be using a rather than a-1, since range(n) already gives you the values 0 .. n-1, a toal of n different items.
So, with all that in mind, and using some better (self-documenting) variable names (and catching the problem of entering a non-positive count), you could opt for something like:
count = int(input("Total Numbers you will input for calculation: "))
if count > 0:
sumOfNums = 0
for x in range(count):
num = int(input("Enter your input number {0}.".format(x+1))
sumOfNums += num
avgOfNums = sumOfNums / count
print("Total sum of the number you entered is {0} and their average is {1},".format(sumOfNums, avgOfNums))
Have a look at below:
a=int(input("Total Numbers you will input for calculation: "))
sum = 0 # Running Totalis b
for x in range (a):
c=int(input("Enter your input number {0}.".format(x+1)))
sum +=c
avg=sum / a # For Total
print("Total sum of the number you entered is {0} and their average is {1},".format(sum,avg))
I am planning to create a program which basically lists the Fibbonacci sequnce up to 10,000
My problem is that in a sample script that I wrote I keep getting the error 'int' object is not iterable
My aim is that you enter a number to start the function's loop.
If anyone would help out that would be great
P.S I am a coding noob so if you do answer please do so as if you are talking to a five year old.
Here is my code:
def exp(numbers):
total = 0
for n in numbers:
if n < 10000:
total = total + 1
return total
x = int(input("Enter a number: "), 10)
exp(x)
print(exp)
numbers is an int. When you enter the number 10, for example, the following happens in exp():
for n in 10:
...
for loops through each element in a sequence, but 10 is not a sequence.
range generates a sequence of numbers, so you should use range(numbers) in the for loop, like the following:
for n in range(numbers):
...
This will iterate over the numbers from 0 to number.
As already mentioned in comments, you need to define a range -- at least this is the way Python do this:
def exp(numbers):
total = 0
for n in range(0, numbers):
if n < 10000:
total = total + 1
return total
You can adjust behavior of range a little e.g. interval is using. But this is another topic.
Your code is correct you just need to change :
for n in numbers:
should be
for n in range(0, numbers)
Since you can iterate over a sequence not an int value.
Good going, Only some small corrections and you will be good to go.
def exp(numbers):
total = 0
for n in xrange(numbers):
if n < 10000:
total += 1
return total
x = int(input("Enter a number: "))
print exp(x)