2017-11-18 14 views
-2

到目前爲止..我這個編碼:你如何計算用戶輸入的整數的均值可以被數字整除?

While True: 
    div_5 = [] 
    x = float(input("Please input an integer(negative if you want to stop: ")) 
    x = int(x) 
    length = len(div_5) 
    total_sum = sum(div_5) 
    average = total_sum/length 
    if x % 5: 
     div_5.append(x) 
    if x < 0: 
     break 
print(average) 

當我運行的代碼,它說,7號線有ZeroDivisionError。我究竟做錯了什麼?我添加了x,如果它能被列表5整除,爲什麼這麼說? 任何提示將有助於我是新的python。

這是我需要做的

enter image description here

我一直在這一個星期,小時的時間。我似乎無法弄清楚它的正確性。

+0

'div-5'和'div_5'是兩回事。小心! – tadman

+0

當然,你會得到'ZeroDivisionError',因爲'total_sum'是'div_5'元素的總和,它是一個空列表。 –

+0

在第一次迭代中,除以0,因爲len([])是0. – 2017-11-18 05:21:20

回答

0
nums = [] 
while True: 
    x = int(input("Please input an integer(negative if you want to stop: ")) 

    if x < 0: 
     div_5 = [x for x in nums if x % 5 == 0] 
     try: 
      print(sum(div_5)/float(len(div_5))) 
     except ZeroDivisionError: 
      print("No number divisible by 5") 
     break 

    nums.append(x) 

事實上,你不需要保留不能被5整除的數字。下面是一個忽略零的運行平均值的方法。

count = 0. 
average = 0. 
while True: 
    x = int(input("Please input a positive integer(negative if you want to stop: ")) 

    if x < 0: 
     print(average) 
     break 
    if x % 5 == 0 and x != 0: 
     count += 1 
     average = average * (count - 1)/count + (1./count) * x 
+0

非常感謝!如果您不介意,你能解釋一下爲什麼你把「除ZeroDivisionError之外:」它做什麼? – Madison

+0

它捕捉到你除以零以後會發生的異常,當div_5列表中沒有任何值時會發生。 –

+1

非常感謝您的幫助!我計劃在此時間內採取這種行動,以便真正瞭解它。感謝您爲我解決問題。我很感謝! – Madison

0

你可以暗示從這個代碼,但因爲這似乎是一個學校的功課,所以我建議你應該嘗試自己,如果你想提高你的編碼。

user_input=[] 
while True: 

    x = int(input("Please input an integer(negative if you want to stop: ")) 
    if x>0: 
     user_input.append(x) 
    else: 
     break 

div_5=[item for item in user_input if item%5==0] 
print('The mean is {}'.format(sum(div_5)/len(div_5))) 

輸出:

Please input an integer(negative if you want to stop: 1 
Please input an integer(negative if you want to stop: 2 
Please input an integer(negative if you want to stop: 3 
Please input an integer(negative if you want to stop: 4 
Please input an integer(negative if you want to stop: 5 
Please input an integer(negative if you want to stop: 6 
Please input an integer(negative if you want to stop: 7 
Please input an integer(negative if you want to stop: 8 
Please input an integer(negative if you want to stop: 9 
Please input an integer(negative if you want to stop: 15 
Please input an integer(negative if you want to stop: -12 
The mean is 10.0 
+0

@ PM2Ring修復它。謝謝指出。 –

+0

這僅僅是我的CSC課程的一些練習!我一直試着反覆數小時,每天試着弄清楚我自己,我只是不能!謝謝您的幫助! – Madison

+0

@Madison當然如果你面對的問題是最後一行是列表理解,我可以解釋它。 –

0

正如我在評論中說:

  1. 您需要while循環開始之前創建div_5列表。
  2. 你應該讀取整數輸入,所以float調用是無用的。
  3. 如果x在讀取後立即爲負數,則測試它。
  4. 在跳出循環之前不要計算平均值,並且如果列表中沒有數據,則不計算平均值。

而且,你的可分性測試是倒退:x % 5是零,如果x是5整除,而零被認爲是假。

下面是您的代碼的修復版本。

div_5 = [] 
while True: 
    x = int(input("Please input an integer (negative if you want to stop): ")) 
    if x < 0: 
     break 
    if x % 5 == 0: 
     div_5.append(x) 

if div_5: 
    average = sum(div_5)/len(div_5) 
else: 
    average = 0 
print(average) 

演示

Please input an integer (negative if you want to stop): 1 
Please input an integer (negative if you want to stop): 5 
Please input an integer (negative if you want to stop): 6 
Please input an integer (negative if you want to stop): 10 
Please input an integer (negative if you want to stop): -4 
7.5 

當我們跳出主循環測試if div_5測試是否有在div_5列表中的任何號碼,只要有執行部門。如果列表中沒有數字,則平均值爲零。

相關問題