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()).
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)
Comments
Leave a comment