ineedtomakeaprograminpythonthatgeneratestenrandomnumbersfrom1- 100 thatstoresitinalistusingaloop.thenasecondlooopshouldispla thencalculatethesumsoftheevenandoddelementstodisplaythem.thisiswhatihavesofar,anyhelpisgreatlyappreciated.thatihavesofar
导入随机
defmain(: )
forrinrange(10 ) :
是打印(numlist )
list_length=len(numlist )
打印(thenumberofelementsinthelistis ),list_length ) ) )。
morespecificallythisistheparti ' mstuckon.ihavetoaddthesumsoftheoddandthenelements.everyworkaroundi ' vetryedhasonlygily
forxinrange (0,10,2 ) :
打印(thesumoftheoddnumbersis (,sum ) (numlist ) ) ) ) )。
主() )
nums=[ random.randint (1,100 ) for_inrange ) 10]
You can use lambdas and filter
evensum=sum (过滤器(lambdai : I %2==0,nums ) )
od sum=sum (过滤器(lambdai : I % 2,nums ) )
Or make some quick helper functions
EFisEven(x ) :
返回x %2==0
EFisodd(x ) :
返回x %2==1
evensum=sum (过滤器(is even,nums ) )
od sum=sum (过滤器(is odd,nums ) )