In: Computer Science
Do it in python please! also please use this template please I have provided and below is the activity
def main():
# import the module random
try:
# asking the user to enter a number between 1 and 100
#loop time
while
if
elif
#for loop # generates that number of random integers and stores
them in a list
for x in
# computations
# displays the results on the screen
# call try_Again to give the user the opportunity to try again or to end the program
#bit of error catching
except:
# Defining a function tryAgain()
def try_Again():
while
if
elif
# defining a function endProgram that will be called any time
the user wants to end the program
def endProgram():
# heading
# Calling the main function
if __name__ =='__main__':
main()
# This will keep the console open at the end of the program
# until the end user hits the Enter key.
This activity is worth 10 total points
This lesson's Group Activities are:
We're going to take the Group Activity from last week and tweak it. Instead of storing the random numbers in a list, we're going to store them in a file. Write a program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a file. It should then display the following data to back to the user:
Helpful hint: don't forget about input validation loops and try/catch exceptional handling. Both are very useful when used in conjunction with functions.
CODE:
#import the module random
import random
import sys
def main():
try:
num = 1
numbers = []
#opening the file
file = open('random.txt','w')
#loop time
while(True):
#asking the user to enter a number between 1 and 100
num = int(input('Enter a number: '))
if(num in range(1,101)):
break
print('Number not in range 1,100')
#for loop generates that number of random integers and stores them
in a list
for i in range(num):
random_num = random.randint(0,51)
#appending the numbers to a list
numbers.append(random_num)
#writing the number to the file
file.write(str(random_num)+'\n')
# computations
printList(numbers)
calculate(numbers)
# displays the results on the screen
# call try_Again to give the user the opportunity to try again or
to end the program
try_Again()
#bit of error catching
file.close()
except(FileNotFoundError):
print('FileNotFound')
try_Again()
def printList(numbers):
print('Numbers in the list: ')
for i in numbers:
print(i,end=' ')
print()
def calculate(numbers):
#displaying the results
print("Lowest number in the list: "+str(min(numbers)))
print("Highest number in the list: "+str(max(numbers)))
print("Sum of numbers in the list: "+str(sum(numbers)))
print("Average of numbers in the list:
"+str(sum(numbers)/len(numbers)))
# Defining a function tryAgain()
def try_Again():
while(True):
choice = int(input('Enter 1 to try again 0 to exit: '))
if choice == 1:
main()
elif choice == 0:
endProgram()
# defining a function endProgram that will
#be called any time the user wants to end the program
def endProgram():
sys.exit(1)
# heading
# Calling the main function
if __name__ =='__main__':
main()
______________________________________
CODE IMAGES AND OUTPUT:
________________________________________________
Feel free to ask any questions in the comments section
Thank You!