In: Computer Science
create a python function (only) that generates 10 random numbers within a specific range. Every time a number is generated, it is appended to a list (you need to initialize a list). Then the function counts how many times a number is repeated in the created list (you can choose any number to count). The function returns the list, the number you have chosen and its count.
--define the function here ---
Source code of the program is is given below.Detailed comments are included for better understanding the code.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Source code
# importing random module to generate random numbers
import random
# function definition
def randomCount():
# creating an empty list
randomList = []
# for loop to create 10 random numbers
for i in range(0,10):
# generating random number within the range(1,20)
n = random.randint(1,20)
# appending the number into the list
randomList.append(n)
# prompt user to enter number to be counted
number=int(input("Enter the number to be counted: "))
# initialize count with 0
count=0
# iterate through the list values
for i in range(0,10):
# if the list element is the number,then increment count by 1
if randomList[i]==number:
count=count+1
# return randomList,number and count
return randomList,number,count
# main() function
def main():
# calling randomCount() function
randomNumbers,num,count=randomCount()
# printing the returned values
print("The produced random number list is : ",randomNumbers)
print("The counted number is: ",num)
print("Count is: ",count)
# calling the main() function
main()
Screen shot of the code
Screen shot of the output