久久久久久久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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产精品美女在线观看 | 中文字幕一区二 | 亚洲国产精品一区二区三区 | 久久久久久久97 | 国产精品一区一区三区 | 三级免费观看 | 亚洲男人天堂网 | 欧美日韩国产二区 | 亚洲黄色在线 | 国产又黄又爽视频 | 成人一区二区视频 | 日韩欧美在线视频观看 | 欧美xxxx网站| 91亚洲精品在线 | 狠狠操天天操 | 日本a网站 | 亚洲综合视频在线观看 | www.狠狠操 | 91午夜精品亚洲一区二区三区 | 99爱视频| 日本一区二区三区在线视频 | 四虎激情 | 日韩一级淫片 | 中文字幕高清在线 | 日韩精品一区二区在线 | 久久r | 日本亚洲精品 | 91国产丝袜在线播放 | 又黄又爽又色视频 | 亚洲成人国产 | 亚洲在线视频 | www.黄色av | 中文字幕在线观看亚洲 | 亚洲伊人av | 中文字幕观看 | 久久精品福利视频 | 国产精品成人免费一区久久羞羞 | 欧美日韩中文字幕 | 亚洲免费久久 | 日韩黄色在线观看 | 91成人精品一区在线播放 |