In: Computer Science
Do it in python please
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.
Before making above program, we see the algorithm for this:
CODE:
while 1:
try:
a=int(input())
if a>=0 and a<=50:
f=open("filename.txt","a") # creating the object of file
and it will append the end of the file...
f.write(str(a)) # write into the file and typecast to
string because it didn't take input as integer it has compulsory to
give input as character or string...
f.close() # close the object of file...
f1=open("filename.txt","r") # creating the
object...
nums=list(map(int,f.readline().split())) # creating list
copy all the data from file into list, data as split by space and
also typecast into integer because we take character as input from
file...
f.close() # close the object of file...
print(nums) # print all the values entered by the
user....
print(min(nums)) # print lowest value in user
inputs...
print(max(nums)) # print highest value in user
inputs...
print(sum(nums)) # print sum of the all
numbers...
print(sum(nums)/len(nums)) # print average of all the
numbers...
else if a==-1: # Termination of the
program...
break;
except NameError: # exception handling....
print("Entered value is not number.")
except: # exception handling...
print("Eneterd value is not number.")
Please, don't forget to like this question. Thank you!