Apologies, first time poster, and beginner python user.
The problem asks the following: Write a Python program that allows the user to enter any number of nonnegative floating-point values. The user terminates the input list with any negative value. The program then prints the sum, average (arithmetic mean), maximum, and minimum of the values entered. The terminating negative value is not used in the computations.
I pretty much have most of it, have tried a few different ways, and can not seem to get it to compute the average and sum correctly. (can not get it without the negative number to terminate, or also makes the average negative, or the same as the sum value)
I am considering starting over and using a def function call. ?? I also was starting to get somewhere with try-except statements, but that fell apart as well.
Any advice in the right direction is appreciated!
First...
num = []
tot = 0
big = None
small = None
while True:
numbers = float(input('Enter a positive number, negative to stop: '))
tot += numbers
if big is None or numbers > big:
big = numbers
if small is None or numbers < small:
small = numbers
if numbers < 0:
break
avg = tot / numbers
print('Sum is:', tot)
print('Average is:',avg)
print('Maximum is:',big)
print('Minimum is:',small)
and if I change a few things I get the average to be positive instead of negative, but still is the same value as minimum, just some snippets...
tot = 0
big = None
small = None
count = 0
while True:
length = count + 1
#etc, etc...then trying to do...
avg = tot/length
#or...
avg =str(tot/length)
but still stuck.
num = []
tot = 0
small = None
avg = None
big = None
while True:
numbers = float(input('Enter a positive number, negative to stop: '))
if numbers < 0:
break
tot +=numbers
num.append(numbers)
if small== None and big == None and avg == None:
small=numbers; big=numbers; avg = numbers
else:
small = min(small, numbers)
small = max(big, numbers)
if len(num) > 0:
avg = tot / len( num)
print('Sum is:', tot)
print('Average is:',avg)
print('Maximum is:',big)
print('Minimum is:',small)
num = []
tot = 0
small = None
avg = None
big = None
while True:
numbers = float(input('Enter a positive number, negative to stop: '))
if numbers < 0:
break
tot +=numbers
num.append(numbers)
if len(num) > 0:
avg = tot / len( num)
big = max(num)
small = min(num)
print('Sum is:', tot)
print('Average is:',avg)
print('Maximum is:',big)
print('Minimum is:',small)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.