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
Related
The program should read a positive integer n from the user to decide how many number to generate.
The random numbers should be in the intervall [1, 100].
I should then print the average value, smallest value and largest value.
But WITHOUT using lists or any other data structure.
I managed to get the average value but I still need to get smallest and largest. any advise?
here is how my code look like so far
You can use the built in min() and max() functions. Here's a bit of code, should be pretty self explanatory:
import random
n = 20
# set up the values
smallest = 101
biggest = -1
for i in range(n):
x = random.randint(1,100)
# take the smallest of the new random number and the current smallest
smallest = min(x, smallest)
# take the biggest of the new random number and the current biggest
biggest = max(x, biggest)
print(smallest, biggest)
If I understand your question, you are trying to print the smallest and largest number of these five numbers without using a list.
You can easily create variables, and change them, if a higher/lower value is generated, then the highest/lowest value before. Hope it helps:
import random
n = int(input("Enter a number of integers to be generated: "))
if n < 0:
print("Please enter a positive integer!")
else:
highest_value = 0
smallest_value = 100
sum_of_random = 0
for x in range(n):
x = random.randint(1, 100)
sum_of_random += x
if x > highest_value:
highest_value = x
if x < smallest_value:
smallest_value = x
print(x, end = " ")
print("\n")
avg = round(sum_of_random / n, 2)
print(avg)
print(highest_value)
print(smallest_value)
If this answers your question, please mark it as a correct answer.
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)
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'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
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)