2016-09-30 110 views
-2

我遇到了麻烦,因为我要求用户输入6个数字到列表中,然后根据用户的输入进行总计和平均。这是我的HWK。请帮忙。Python如何总结列表

x = 0 
list = [] 
while x < 6: 
    user = int(input("Enter a number")) 
    list.append(user) 
    x = x + 1 
numb = input("Do you want a total or average of numbers?") 
numb1 = numb.lower 
if numb1 == "total": 
+2

'sum(list)',但是请不要命名你的列表'list',因为这是一个内置类型。 – smarx

+3

不是说你有[mcve],但我敢打赌'numb1 = numb.lower'不会做你认为它的作用。 – jonrsharpe

回答

0

这里是我的回答:

def numberTest(): 
    global x, y, z 
    L1 = [] 
    x = 0 
    y = 6 
    z = 1 
    while(x < 6): 
     try: 
      user = int(input("Enter {0} more number(s)".format(y))) 
      print("Your entered the number {0}".format(user)) 
      x += 1 
      y -= 1 
      L1.append(user) 
     except ValueError: 
      print("That isn't a number please try again.") 
    while(z > 0): 
     numb = input("Type \"total\" for the total and \"average\"").lower() 
     if(numb == "total"): 
      a = sum(L1) 
      print("Your total is {0}".format(a)) 
      z = 0 
     elif(numb == "average"): 
      b = sum(L1)/ len(L1) 
      print("Your average is {0}".format(round(b))) 
      z = 0 
     else: 
      print("Please try typing either \"total\" or \"average\".") 
numberTest() 

我想这几次,我知道它的工作原理。如果您对部分代码感到困惑,我会添加评论并回答更多问题。