久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

求和.TypeError:“浮動"對象不可迭代

Finding Sum. TypeError: #39;float#39; object is not iterable(求和.TypeError:“浮動對象不可迭代)
本文介紹了求和.TypeError:“浮動"對象不可迭代的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

今天我嘗試制作一個程序,詢問用戶他/她希望我對系列中的多少項目求和.該系列將用于近似 pi 的值.到目前為止,這是我的代碼:

Today I tried to make a program that would ask a user how many items in a series he/she wanted me to sum. This series would be used to approximate the value of pi. Here is my code thus far:

from math import pi

def pi_sum():
n = eval(input("How many terms do you want to sum: "))
for i in range(n):

    denominator= 2.0 * i + 1
    sign = (-1) ** i
    numerator = 4
    result = (4 * sign) /denominator
    print(result)
    print(sum(result))



pi_sum()

但是,此代碼會生成錯誤消息:

However, this code generates the error message:

File "C:/Users/Crisa/PycharmProjects/untitled/piprogram.py", line 14, in pi_sum
print(sum(result))
TypeError: 'float' object is not iterable

如果我的 for 循環生成浮點數,我如何讓 python 對我的 for 循環中的值求和?我被卡住了:/

How can I get python to sum the values in my for loop if my for loop is generating floats? I am stuck :/

推薦答案

將每次迭代的結果添加到列表中:

Add your results of each iteration into a list:

myresults = []
for i in range(n):
    ...
    myresults.append(result)

然后在最后,得到所有結果的列表總和:

Then at the end, get the sum of the list with all your results:

print(sum(myresults))

這篇關于求和.TypeError:“浮動"對象不可迭代的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Sum values in a dict of lists(列表字典中的總和值)
How to sum numbers from a text file in Python(如何在 Python 中對文本文件中的數字求和)
What is a subtraction function that is similar to sum() for subtracting items in list?(什么是類似于 sum() 的減法函數,用于減去列表中的項目?)
Python equivalent of sum() using xor()(Python 等價于 sum() 使用 xor())
sum matrix columns in python(python中的求和矩陣列)
sum of N lists element-wise python(N個列表元素的總和python)
主站蜘蛛池模板: 欧美黄色三级视频 | 国产不卡在线观看 | 亚洲免费视频一区 | 91看片在线| 伊人网av | 在线观看日韩欧美 | 久久一区| 五月天黄色网 | 久久18| 精品久久国产 | 麻豆视频国产 | 欧美8888| 日韩欧美一级片 | 亚洲欧美另类在线 | 亚洲精品不卡 | 九色精品| 日韩一级免费视频 | 婷婷中文字幕 | 国产精品久久久久久久成人午夜 | 韩日在线视频 | 国产精品欧美精品 | 亚洲国产精品久久久 | 成人黄色免费视频 | 少妇高潮av久久久久久 | 国产精品美女毛片真酒店 | 欧美不卡一区 | 日韩色黄大片 | 国产精品久久久久久久免费看 | 国产a区 | 久久91视频 | 天堂网亚洲| 婷婷综合五月 | 欧美日韩免费 | 毛片网站视频 | 国产天堂网 | 亚洲福利视频一区 | 中文字幕av久久爽av | 婷久久| 国产午夜精品一区二区三区视频 | 日韩精品视频免费播放 | 欧美一级片免费看 |