Answer to Question #310484 in Python for petals

Question #310484

1. Create a new list and populate it with 15 random integers (Tip: use randint() or randrange() from the random module)

2. Write a function to calculate the average of the list values

3. Write a function to test if the list contains a specific value

4. Write a function to reverse the list using .pop() and .append() (you must not use .reverse()).


1
Expert's answer
2022-03-12T18:05:08-0500
import random


def avg(L):
    """Calculate the average of the list values
    """
    s = 0
    for x in L:
        s += x
    return s / len(L)


def test(L, val):
    """Test if the list contains a specific value
    """
    for x in L:
        if x == val:
            return True
    return False


def reverse(L):
    res = []
    while len(L):
        x = L.pop()
        res.append(x)
    return res


L = [random.randint(1, 100) for i in range(15)]
print('Source list:')
print(L)
print('Average  of list is', avg(L))


x = int(input('Enter an element to test: '))
if test(L, x):
    print('Present')
else:
    print('Not present')


L = reverse(L)
print('Reverse list:')
print(L)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS