Write a Python program to do the following: (a)Use a for loop and a random integer generator to generate 5 random integers in 1 to 9. Store the random integers in a list. Display the list. (b)Use a for loop and a random integer generator to generate 5 random integers in 2 to 8. Store the random integers in a second list. Display the second list. (c)Use a for loop to compare elements in the two lists in pairs, i.e., compare the first elements in both lists, compare the second elements in the both lists, etc. Display the larger number in each comparison. The following is an example. There is no user input in this program.

Respuesta :

Answer:

//program in Python

#library

import random

#part 1

#empty list 1

list1 = []  

#Generate 5 random numbers for the first list

for i in range(0, 5):

   list1.append(random.randrange(1, 9+1))

#print first list

print("First List: " + str(list1))

#part b

#empty list 2

list2 = []  

#Generate 5 random numbers for the second list

for i in range(0, 5):

   list2.append(random.randrange(2, 8+1))

 

#print the second list

print("Second List: " + str(list2))

#part c

#Compare each element from both  lists and print the larger one

print("Larger number in each comparison:")

for i in range(0, len(list1)):

   if list1[i] >= list2[i]:

       print(list1[i])

   else:

       print(list2[i])

Explanation:

Create an empty list.Generate 5 random number from range 1 to 9 and append them  into first list.Create a second empty list.generate 5 random numbers from range  1 to 8 and append them into second list.Print both the list.Then compare both the list an print Larger in each comparison.

Output:

First List: [3, 1, 4, 9, 8]                                                                                                

Second List: [5, 8, 2, 7, 6]                                                                                              

Larger number in each comparison:                                                                                          

5                                                                                                                          

8                                                                                                                          

4                                                                                                                          

9                                                                                                                          

8