This code needs to calculate an average to retrieve an integer between 0-100. Any suggestions would be great.
Prompt the user for the number of points they expect to receive
for engagement at the end of class. This number should not be
adjusted by the program (engagement is a total of 302).
Calculate the final based on an average of the quiz scores to
date.
Prompt the user for the various scores. -1 = no more scores, 0 -
zero for that assignment. Keep looping for the grade until the user
enters a -1.
Here is my code, I definitely do not understand lists very well. Probably clearly.
list1 = []
g=1
totalnum = 0
total=0
tot = int(input("Total Points? :"))
list1.append(tot)
eng = int(input("How many points do you expect to get in engangement, out of 302?: "))
list1.append(eng)
while g !=0:
num = int(input("Please enter a grade"))
if num == -1:
break
totalnum+=1
total= total+num
list1.append(num)
average= tot/eng+num
counter=0
while counter<totalnum:
print(list1[counter])
counter+=1
print("your average is",average)
If you can use built ins you can just use sum(your_list) / len(your_list) for the average.
If len(your_list) is zero, you will get a ZeroDivisionError, because you just broke math.
It's not clear to me that this is what you're trying to accomplish, but if you just want an average of all the values in the list, you can just do:
average = sum(list)/len(list)
This would return an integer, if you don't want it rounded you could do:
average = sum(list)/float(len(list))
As an aside, you have this:
counter=0
while counter<totalnum:
print(list1[counter])
counter+=1
Which could be made simpler, like:
for e in list1:
print e
Python will do the counting for you. :)
list1 = []
g=1
totalnum = 0
total=0
tot = int(input("Total Points? :"))
eng = int(input("How many points do you expect to get in engangement, out of 302?: "))
list1.append(eng)
while g !=0:
num = int(input("Please enter a grade"))
if num == -1:
break
else:
list1.append(num)
totalnum+=1
total= (num+eng)/tot
print(len(list1))
average = (sum(list1)/tot)*100
counter=0
for e in list1:
print (e)
print("your average is",average)
This is more what I was looking for. Thanks to #Tommy, #SQLnoob, and #Slayer for the tips that I have implemented into the code.
Related
Write a program that always asks the user to enter a number. When the user enters the negative number -1, the program should stop requesting the user to enter a number. The program must then calculate the average of the numbers entered excluding the -1.
This is what I have come up with so far
num = int(input("How many number's are there?"))
total_sum = 0
avg = total_sum / num
for n in range (num):
number = float(input("Enter a number"))
while number <0:
print = ("Average is", avg)
number >0
print(number)
total_sum += number
avg = total_sum / num
print = ("Average is", avg)
I need it to stop at -1 one and still give an average of the numbers listed.
Easy.
emp = []
while True:
ques = int(input("Enter a number: "))
if ques != -1:
emp.append(ques)
continue # continue makes the code go back to beginning of the while loop
else:
if len(emp) != 0:
avg = sum(emp) / len(emp) # average is the sum of all elements in the list divided by the number of elements in said list
print(f"Average of all numbers entered is {avg}")
break
else:
print("Give me at least one number that's not -1")
continue # emp is empty we need at least one number to calculate the average so we go back to the beginning of the loop
I'm a newbie of python and I'm trying to practice some exercises, however I'm stuck with the line "total = 0" in this code. Actually I don't really understand why this value is initialized to 0.
Could anyone please explain it for me ?
def main():
print("This program allows you to total up some numbers")
print()
n = int(input("How many numbers do you have? "))
total = 0
for i in range(n):
num = float(input("Enter a number: "))
total = total + num
print()
print("The sum of the numbers is:", total)
main()
Remove that line and you see you will get an error:
NameError: name 'total' is not defined
This is because both total and num must be initialised before you can use them in total = total + num
You initialise it before the for loop otherwise it would be reset to 0 each time.
This is for one of my assignments.
Here is the question just for clarity on what I am trying to do. Please do not give me the answer, just if you could help me understand what I need to do.
Write a Python program that uses a WHILE loop. The program must prompt the user to enter an integer number. The value must be added to a total. The loop must continue until the total exceeds 45. After the loop, the average of the numbers must be calculated. The program must display each of the input values, as well as the sum of all values and the average value.
*** enhancement, replace the user prompt with a random number selector.
This is the current code I am using:
num = int(input('Enter as many integers as you want: '))
numList =num.split()
print('All entered numbers ', numList)
sum = 0
while num >= 45:
print('Sum of all numbers ', sum)
avg = sum / num
print('Average of all numbers ', avg)
This, of course, is not working, I have figured out how to do it with a for loop ( from the internet ) I just cannot seem to understand how to link the input function with the while loop.
You want to read numbers one at a time, until the sum exceeds 45.
total = 0
num_list = []
while total < 45:
num = int(input(...))
num_list.append(num)
total += num
# Now compute the average and report the sum and averages
To make sure the last number is not added to the list if that would put the total over 45,
total = 0
num_list = []
while True:
num = int(input(...))
new_total = total + num
if new_total > 45:
break
num_list.append(num)
total = new_total
The while loop should be used to get values from the user : While the total of the given values is lower than 45, ask the user for another value
numList = []
total = 0
while True:
num = int(input('Enter an integer: '))
if (total + num) > 45:
break
numList.append(num)
total = total + num
avg = total / len(numList)
print('Sum of all numbers ', total)
print('Average of all numbers ', avg)
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!
So I'm writing some code for my class and have to have a list of floats that are input by a user and print them out with normal iteration and reverse iteration, and I have basically all of it done. But when it should ask multiple times for the user input, it only asks one time then prints everything out and finishes without asking multiple times.
Any help would be appreciated as to why it isnt asking multiple times for input even though I have a for loop?
Is there an easier way to get a list of floats from user input that I don't know about?
Thanks
emptyList = []
userInput = high = low = total = float(input("Input a float > "))
emptyList.append(userInput)
for y in range(len(emptyList)-1):
userInput = float(input("Input a float > "))
emptyList.append(userInput)
total += emptyList
if userInput > high:
high = userInput
if userInput < low:
low = userInput
avg = total / len(emptyList)
above_avg = below_avg = 0
for y in range(len(emptyList)):
if emptyList[y] > avg:
above_avg += 1
if emptyList[y] < avg:
below_avg += 1
I checked your logic and according to that you are running your first for loop to the length of emptyList-1 so, after adding one element your emptylist's length becomes 1 and 1-1=0 so, your for loop will work only to 0th index and after that it will break.
I tried this
a=[]
b = []
t=0
count = 0
total = 0
for i in range(0,5):
x= float(input())
count = count + 1
total = total+ x
a.append(x)
print(count)
for i in range(count-1,-1,-1):
print('Aya')
print (i)
b.append(a[i])
for i in range(0,count-1):
for j in range(i,count):
if(a[i]>a[j]):
t=a[i]
a[i]=a[j]
a[j]=t
print(a)
print(b)
print(total)
print(total/count)
print(a[0])
and found it working perfectly fine for me and my for loop is taking values on every iteration.
You may try like this:
n = int(input("Number of inputs: "))
emptyList = []
while n:
userInput = float(input("Enter a Float > "))
emptyList.append(userInput)
n-=1
print(emptyList)