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

在 Python 中,如何在循環中獲取總和和平均值

In Python, how to get the sum and average while in a loop(在 Python 中,如何在循環中獲取總和和平均值)
本文介紹了在 Python 中,如何在循環中獲取總和和平均值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我已經設法實現了一個循環,但是當我嘗試 sum 函數時不斷收到語法錯誤.我需要匯總用戶輸入的數字并給出平均值.這必須輸出給用戶.您能否指導我從這里去哪里,謝謝.

這是我到目前為止所做的:

I've managed to implement a loop but keep getting a syntax error when I try the sum function. I need the numbers input by the user to be totalled and the average given as well. This has to be outputted to the user. Could you please guide me on where to go from here, thank you.

This is what I've done so far:

while 1:
    NumCalc = input ("Enter Number :")
    if NumCalc == "done": break

推薦答案

如果您想在循環結束后計算總和和平均值,您可以這樣做:

This is what you can do if you want to compute the sum and the mean after the loop ends:

nums = []
while 1:
    NumCalc = input ("Enter Number:")
    if NumCalc == "done": break
    nums.append(float(NumCalc))

print('Sum:', sum(nums), 'and average:', sum(nums)/len(nums))

<小時>循環中:

s = 0.0
counter = 0

while 1:
    NumCalc = input("Enter Number: ")
    if NumCalc == "done":
        break

    NumCalc = float(NumCalc)
    s += NumCalc
    counter += 1


    print('Sum is', s, 'and the mean is', s/counter)

輸出:

Enter Number: 5
Sum is 5.0 and the mean is 5.0
Enter Number: 2
Sum is 7.0 and the mean is 3.5
Enter Number: 4
Sum is 11.0 and the mean is 3.66666666667
Enter Number: 6
Sum is 17.0 and the mean is 4.25
Enter Number: 2
Sum is 19.0 and the mean is 3.8

這篇關于在 Python 中,如何在循環中獲取總和和平均值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产福利小视频 | 日日做夜夜爽毛片麻豆 | 在线a视频 | 一区二区不卡 | 亚洲精品大全 | 四虎影视1304t | 中文字幕二区三区 | 操射视频 | 日韩免费av网站 | 成人一区二区三区 | 精品免费国产一区二区三区四区介绍 | 91精品国产91久久久久久密臀 | 欧美三级久久久 | 欧美日韩亚洲系列 | 国产欧美一区二区三区久久手机版 | 成人性生交大片 | 91免费福利视频 | 中文字幕在线免费视频 | 一级黄a视频| 国产乱码精品一品二品 | 久久男人 | 日本午夜一区二区三区 | 日韩中文字幕在线播放 | 婷婷免费在线 | 草久视频 | 免费av毛片| www国产精| 九色av | 欧美一区二区三区在线播放 | 国产一级片在线播放 | 欧美激情在线观看一区二区三区 | 在线观看中文字幕视频 | 欧美一区二区三区的 | 欧美亚洲视频在线观看 | 女女爱爱视频 | 日韩中文字幕在线免费 | www亚洲精品 | 免费观看a级毛片在线播放 黄网站免费入口 | 91成人在线视频 | 亚洲综合字幕 | 亚洲精品视频免费观看 |