2016-09-30 1 views
-2

私はユーザに6つの数字をリストに入力してから、ユーザの入力に応じて合計して平均化することを頼んでいるので問題があります。それは私のHWKです。助けてください。リストを合計する方法

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

が、私はこの数回試したと私はそれが動作します知っています。コードの一部について混乱している場合は、コメントを追加してさらに質問に回答します。

関連する問題