(Python) Stuck on skipping range values for the sum of a randint list

痴心易碎 提交于 2019-12-13 04:55:47

问题


I need to make a program in python that generates ten random numbers from 1-100 that stores it in a list using a loop. Then a second loop should display said list, then calculate the sums of the even and odd elements to display them. This is what I have so far, any help is greatly appreciated. Thanks

import random

def main():
    numlist = [0] * 10

    for r in range(10):
        numlist[r] = random.randint(1,100)
    print(numlist)

    list_length = len(numlist)
    print('The number of elements in the list is', list_length)

More specifically this is the part I'm stuck on. I have to add the sums of the odd and then even elements. Every work around I've tryed has only given me the sum of the total elements.

    for x in range(0, 10, 2):
        numlist[x] = numlist 

    print('The Sum of the odd numbers is ', sum(numlist))


main()

回答1:


import random
nums = [random.randint(1,100) for _ in range(10)]

You can use lambdas and filter

evenSum = sum(filter(lambda i : i%2 == 0, nums))
oddSum = sum(filter(lambda i : i%2, nums))

Or make some quick helper functions

def isEven(x):
    return x % 2 == 0

def isOdd(x):
    return x % 2 == 1

evenSum = sum(filter(isEven, nums))
oddSum = sum(filter(isOdd, nums))



回答2:


Using your own code:

def main():
    numlist = [] # create empty list to add randints to 
    for r in range(10):
        numlist.append(random.randint(1,100)) # append to the list
    list_length = len(numlist)
    print('The number of elements in the list is', list_length)
    odd = 0
    even = 0
    for num in numlist:
        if num % 2: # if num mod 2 has a remainder, num is odd
            odd += num
        else:  # else num is even
            even += num
    print('The Sum of the odd numbers is {} and even numbers is {}'.format(odd,even))

You can replace the first loop with a list comp:

numlist = [random.randint(1,100) for _ in range(10)]



回答3:


Can't Understand the actual problem (hahehehe !) As far as I have understood, you want to print the sum of odd and even numbers of the list that is generated from randint(). Well I have just edited your code ;)

so here is the simple done code !

Vote If It Helps !

        import random
        def main():
            odd_sum=0
            even_sum=0
            numlist = [0] * 10
            for r in range(10):
                numlist[r] = random.randint(1,100)
            print numlist
            list_length = len(numlist)
            print('The number of elements in the list is', list_length)

            for i in numlist:
                if (i%2 == 1):            #the remainder is 1 if the number is odd
                    odd_sum = odd_sum + i  #add the odd_numbers
                elif(i%2 == 0):           #the remainder is 0 if the number is even     
                    even_sum = even_sum + i #add the even_numbers
                else:
                    print "No need of this else part !"
            print "Odd_sum = ",odd_sum
            print "Even_sum = ",even_sum
        main()


来源:https://stackoverflow.com/questions/26265103/python-stuck-on-skipping-range-values-for-the-sum-of-a-randint-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!