学习了Python相关数据类型,函数的知识后,利用字符串的分割实现了输入任意多个数据,并计算其平均值的小程序。思路是接收输入的字符串,以空格为分隔符,将分割的数据存入列表(lst1)中,将lst1中的数据转存入另一个空列表(lst)中,转存时将字符串转化为整型,从而利用函数求出lst中数的和、平均值。
print("-----求平均值,可输入任意多个数-------") lst = [] #定义一个空列表 str = raw_input("请输入数值,用空格隔开:") lst1 = str.split(" ")#lst1用来存储输入的字符串,用空格分割 i = 0 while i <= len(lst1)+1: lst.append(int(lst1.pop()))#将lst1的数据转换为整型并赋值给lst i += 1 #print(lst) def sum(list): "对列表的数值求和" s = 0 for x in list: s += x return s def average(list): "对列表数据求平均值" avg = 0 avg = sum(list)/(len(list)*1.0) #调用sum函数求和 return avg print("avg = %f"%average(lst))
运行结果:
请输入数值,用空格隔开:21 32 45 65 avg = 47.333333
Atas ialah kandungan terperinci 如何用python求平均值. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Python adalah bahasa yang ditafsirkan, tetapi ia juga termasuk proses penyusunan. 1) Kod python pertama kali disusun ke dalam bytecode. 2) Bytecode ditafsirkan dan dilaksanakan oleh mesin maya Python. 3) Mekanisme hibrid ini menjadikan python fleksibel dan cekap, tetapi tidak secepat bahasa yang disusun sepenuhnya.

UseAforLoopWheniteratingOvereForforpecificNumbimes; Useaphileloopwhencontinuinguntilaconditionismet.forloopsareidealforknownownsequences, sementara yang tidak digunakan.

Pythonloopscanleadtoerrorslikeinfiniteloops, pengubahsuaianListsduringiteration, off-by-oneerrors, sifar-indexingissues, andnestedloopinefficies.toavoidthese: 1) use'i

Forloopsareadvantageousforknowniterationsationship, menawarkanMenghentianmentability, whileopsareidealfordynamicconditionsandunknowniterations, providingcontrolovertermination.1) forloopsareperfectfectfectfectfectfectfectoVeratingOverlists, tuples, orstrings, secara langsung

Pythonusesahybridmodelofcompilationandinterpretation: 1) thepythoninterpretercompilessourcodcecodeintoplatform-independentbytecode.2) thepythonvirtualmachine (PVM) thenexecutesthisbytecode, BalantingeaseOfusoWithperformance.

Pythonisbothinterpretedandandcompiled.1) it'scompiledtobytecodeforporabilityAcrossplatforms.2) theBytecodeistheninterpreted, membolehkanfordynamictypingandrapiddevelopment, walaupunItmayBeslowerLowerWanLelyCiledlanguages.

ForloopsareidealwhenyonesshenumberofiterationsationseSinadvance, whilewhileloopsarebetterforsituationshipheryouneedtoloopuntilaconditionismet.forloopsaremoreeficientablyandable, yang sesuai, manakala whileloopsoffermorecontrolandareusefereficeficeficeficeficient,

Forloopsareusedwhenthenumberofiterationsisknowninadvance, whilewhileloopsareusedwhenTheiterationsdependonacondition.1) forloopsareidealforiteratingoversequencesLikeListsorArrays.2)


Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Alat panas

EditPlus versi Cina retak
Saiz kecil, penyerlahan sintaks, tidak menyokong fungsi gesaan kod

PhpStorm versi Mac
Alat pembangunan bersepadu PHP profesional terkini (2018.2.1).

SublimeText3 Linux versi baharu
SublimeText3 Linux versi terkini

Versi Mac WebStorm
Alat pembangunan JavaScript yang berguna

ZendStudio 13.5.1 Mac
Persekitaran pembangunan bersepadu PHP yang berkuasa
