-1

here is my current code:

total = 0.0
count = 0
data = input("Enter a number or enter to quit: ")
while data != "":
    count += 1
    number = float(data)
    total += number
    data = input("Enter a number or enter to quit: ")
    average = total / count
    if data > 100:
        print("error in value")
    elif data < 0:
        print("error in value")
    elif data == "":

print("These", count, "scores average as: ", average)

The only problem now is "expected an indent block"

4

3 回答 3

1

我会做一些很酷的事情

my_list = list(iter(lambda: int(input('Enter Number?')), 999)) # Thanks JonClements!!
print sum(my_list)
print sum(my_list)/float(len(my_list))

如果你想做条件,这样的事情会起作用

def getNum():
   val = int(input("Enter Number"))
   assert 0 < val < 100 or val == 999, "Number Out Of Range!"
   return val

my_list = list(iter(getNum, 999)) # Thanks JonClements!!
print sum(my_list)
print sum(my_list)/float(len(my_list))
于 2013-09-27T23:48:57.103 回答
0

To calculate an average you will need to keep track of the number of elements (iterations of the while loop), and then divide the sum by that number when you are done:

total = 0.0
count = 0
data = input("Enter a number or enter 999 to quit: ")
while data != "999":
    count += 1
    number = float(data)
    total += number
    data = input("Enter a number or enter 999 to quit: ")
average = total / count
print("The average is", average)

Note that I renamed sum to total because sum is the name of a built-in function.

于 2013-09-27T23:39:27.553 回答
0
total = 0.0
count = 0
while True:
    data = input("Enter a number or enter 999 to quit: ")
    if data == "999":
        break
    count += 1
    total += float(data)
print(total / count)
于 2013-09-28T00:01:30.977 回答