Python average input not working - python

I am trying to get average times of the speed skaters with Python and my code will not work. I have searched all over trying to find an answer and I am completely stumped.
print("Enter the skater times in seconds. Input -1 to finish and calculate.")
count = 0
sum = 0.0
number = 1
while number != -1:
number = input("Enter skater time in seconds: ")
if number != 0:
count = count + 1
sum = sum + number
print ("The number of skaters was: ", count)
print ("The average skater time was:", sum / count)

The if clause must be:
if number > 0:
Otherwise you are considering the last one, whose value is -1
Edit
I think it has nothing to do with integer division. It is only the if clause.

You can add from __future__ import division and it will make all divisions be floats.

Related

How do you Multiply numbers in a loop in Python from a list of user inputs

I need to create something that can multiply values no matter how many values there are. My idea is to use a list and then get the sum of the list with the sum function or get the first value of the list and set it as the total and then multiply the rest of the list by that total. Does anyone have a way to do it?
Here was my original idea:
total = 0
while True:
number = float(input("Enter a number and I’ll keep multiplying until you enter the number 1: "))
if number == 1:
break
else:
total *= number
print(f"The total is: {total}")
however as you may have guessed it just automatically multiplies it to 0 which equals zero. I would also like the code to work for subtraction and division (Already got Addition working)
thanks!
Thanks to the comments I worked out that the way to do it is by changing the beginning total to a 1 when fixed it is this:
total = 1
while True:
number = float(input("Enter a number and I’ll keep multiplying until you enter the number 1: "))
if number == 1:
break
else:
total *= number
print(f"The total is: {total}")
Since you are multiplying, you must start with 1 cause anything multiplied by 0 is 0. And idk why you need sum()
total = 1
while True:
number = float(input("Enter a number and I’ll keep multiplying until you enter the number 1: "))
if number == 1:
print(f"The total is: {total}")
break
else:
total *= number

Calculating Running Total with a Validation Loop in python

I am new to python and trying to calculate a running total while validating the user input. If the user inputs invalid data I am showing an error and asking the user to input the correct data. My issue is I am unable to determine how to calculate only the valid data input from the user instead of all values.
# This program calculates the sum of a series
# of numbers entered by the user.
#Initialize an accumulator variable.
total = 0
#Request max value from user.
max_value = int(input("Enter a value for the maximum number: "))
#Request user to enter value between 1 and max value.
number = int(input("Enter a number between 1 and " + str(max_value) + " or negative number to end: "))
#Calculate total of user input values.
while number > 0:
total = total + number
number = int(input("Enter a number between 1 and " + str(max_value) + " or negative number to end:"))
#Display error if user entered invalid value.
if number == 0 or number > max_value:
print("Number entered is invalid!")
#Print sum of valid user input data.
else:
print("Sum of all valid numbers you entered:", total)
I can see that you have added too much unnecessary code and this is a very bad practice in python. I believe that this is the answer you are looking for:
f"" = is called a formatted string and
the while loop is unnecessary
so after that, you should import sys and use the exit function under a while loop and exit under a for loop
from sys import exit as exit1
while True:
max_value = int(input("Enter a value for the maximum number: "))
if max_value<0:
break
number = int(input(f"Enter a number between 1 and {max_value} or negative number to end: "))
total = max_value + number
if number == 0 or number > max_value:
print("Number entered is invalid!")
else:
print("Sum of all valid numbers you entered:", total)
So after reevaluating the code I wrote the first time I came up with the below and it functions as I would like. For a beginner that has only learned three chapters I think it is good. Current knowledge base is Input, Processing, Output, Decision Structures, Boolean Logic, and Repetition Structures.
# This program calculates the sum of a series
# of numbers entered by the user.
#Initialize an accumulator variable.
total = 0
# Request max value from user.
max_value = int(input("Enter a value for the maximum number: "))
#Request number between 1 and max value.
number = int(input("Enter a number between 1 and " + str(max_value) + " or negative
number to end: "))
#Validate the user input is between 1 and max value
# if so accumulate the number.
while number >= 0:
if number > 0 and number <= max_value:
total = total + number
elif number == 0 or number > max_value:
print("Number entered is invalid!")
number = int(input("Enter a number between 1 and " + str(max_value) + " or negative number to end: "))
#Once user select a negative number, print total.
else:
print("Sum of all valid numbers you entered:", total)

"Variable not defined" error in total and average computation

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))

Take the average and go through the list again. Count the amount of numbers greater than the average and the amount of numbers less than the average

I'm trying to figure out the 2nd half of this question using python 3 but can't seem to figure it out. The first half of the code that I have is the following below...
A = [1,2,3,4]
print(average(A))
2.5
num = int(input('How many numbers: '))
total_sum = 0
for n in range(num):
numbers = float(input('Enter number : '))
total_sum += numbers
avg = total_sum/num
print('Average of ', num, ' numbers is :', avg)
number = int(input("Enter number: "))
if number < 2.5:
print("Your number is smaller than 2.5")
else:
print("Your number is greater than 2.5")
Save the numbers in list. Sort the list and check the number which is equivalent to or greater than an average number. U can then easily find the count of numbers less than and greater than avarage number.

Average program using while loop. Won't calculate correctly

i = 0
result = 0
while i < 10 :
result += eval(input("Enter a number: "))
i += 1
if result < 1 :
break
average = result / i
print (average)
I am making a program to calculated average of 10 numbers but it will terminate if a negative number is entered. The problem is that if a negative number is enter the program won't stop until the negative number is greater than all the other numbers that have been entered.
This code doesn't necessarily halt on a negative number. It does this only if that negative number brings the running total to a non-positive amount. For example, if the user enters the following numbers:
4
-2
4
Then the running totals are:
4
2
6
At no point is the running total (result) non-positive. So the condition for that break statement won't be true.
If you want to terminate any time a negative number is entered (or, rather, a non-positive number based on your logic), you need to check that number itself, not the running total. Something like this:
i = 0
result = 0
value = 0;
while i < 10 :
value = eval(input("Enter a number: "))
if value < 1 :
break
i += 1
result += value
average = result / i
print (average)

Categories