Question

In: Computer Science

Do it in python please This lesson's Group Activities are: We're going to take the Group...

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:

  • The list of integers
  • The lowest number in the list
  • The highest number in the list
  • The total sum of all the numbers in the list
  • The average number in the list
  • At a minimum, the numbers should range from 0 to 50, but you can use any range you like.

Helpful hint: don't forget about input validation loops and try/catch exceptional handling. Both are very useful when used in conjunction with functions.

Solutions

Expert Solution

Before making above program, we see the algorithm for this:

  1. Start
  2. Take input from user in a specified range. Here, I am taking the (0-50) range.
  3. If user input beyond this range or give some alphanumeric or strings as an input then we gives warning message like "you didn't enter specified value.".
  4. After getting the specified value, we have to make an object for File.
  5. Through File object we open or create a new file with "write" access mode (like read, write, append, etc. many more access mode present).
  6. After opening the file, we store the value of user into the file and close the file object.
  7. To read the file, again we have to create an object for File.
  8. Now, this time in "read" mode,we open the file.
  9. After opening the file we copy the content into the list and print maximum value, lowest value, total sum and average.
  10. Repeat these steps until the user doesn't want to end the procedure. To end this procedure, the user must give "-1" as input.

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!


Related Solutions

This week, we're going to be discussing Amazon and it's ascent to become one of the...
This week, we're going to be discussing Amazon and it's ascent to become one of the largest global online retailers. Get ready for another great set of conversations! https://www.youtube.com/watch?v=YlgkfOr_GLY Why do you think has Amazon succeeded when so many other companies have failed? How did their First Mover's Advantage factor into their ubiquity? How long did it take for Amazon to become profitable? What’s next for Amazon... Cloud computing? Deliveries by drone? Travel? Retail? You should research the company and...
For this assignment we're going to make another calculator. This one will be a simple four...
For this assignment we're going to make another calculator. This one will be a simple four function (add, subtract, multiply, and divide) calculator, but it will have state. Specifically, the calculator will keep track of the result of the most recent operation and use that value as the first operand for the next operation. Take a look at the sample output below if this doesn't quite make sense. Your new calculator class should have the following fields and methods: fields:...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly as difficult as it may appear. The way hangman works (for this assignment - we are doing a simplified game) is as follows: the computer will choose a word. (For this version, a word is selected from a static list encoded into the program -- so the words are pretty limited). Let's say the word is "cocoa". the computer shows the user how many...
n this problem, we're going get a rough estimate the amount of uranium fuel it would...
n this problem, we're going get a rough estimate the amount of uranium fuel it would take if the US recieved all its electrical power from nuclear power plants. The size of a power plant in normally given as the about of electrical power it can produce when running a full capacity. This electrical power produced can be very different than the mechanical or thermal power that was required to produce this electricity. For example, power plant might have a...
n the instructions for this Task C, we're going to assume that you have completed your...
n the instructions for this Task C, we're going to assume that you have completed your script for Task B above; i.e. that you have a working dictionary-based version of the functions (originally named addToRecord(), etc. in Task A) now named addToHistogram(), etc. Let's assume that your dictionary-based function (from Task B above) that creates a histogram is named makeHistogram(). You are going to use your makeHistogram() in the following sub-tasks. In fact, you're welcome to include any function you...
4) We're going to test the same hypothesis four ways. Assume the people in the dataset...
4) We're going to test the same hypothesis four ways. Assume the people in the dataset in armspanSpring2020.csv are a random sample of all adults. For each test, report the test statistic and the p-value. With a 5% significance level, give the conclusion of each test. a) Test the hypothesis that the mean difference between armspan and height it not equal to 0, using the data in armspanSpring2020.csv. Do this by creating a new variable named diff = (armspan -...
In python please: You’re going to program a simulation of the following game. Like many probability...
In python please: You’re going to program a simulation of the following game. Like many probability games, this one involves an infinite supply of ping-pong balls. No, this game is "not quite beer pong." The balls are numbered 1 through N. There is also a group of N cups, labeled 1 through N, each of which can hold an unlimited number of ping-pong balls (a;ll numbered 1 through N). The game is played in rounds. A round is composed of...
PLEASE Fill in the blanks: 1) The group responsible for overseeing the corporation's activities is /are...
PLEASE Fill in the blanks: 1) The group responsible for overseeing the corporation's activities is /are the ________. 2) A corporation is responsible for its own acts and debts because it is considered a ________. 3) The number of shares that a corporation's charter allows it to sell is the ________ stock. 4) ________ bonds can be exchanged for a fixed number of shares of the issuing corporation's common stock. 5) ________ bonds have an option exercisable by the issuer...
Python(please take a screen shot!): 1. hamming distance: write a function distance that take two bits...
Python(please take a screen shot!): 1. hamming distance: write a function distance that take two bits strings, you can assume each strings only contains 0's and 1's. (Note: the two strings might have the same length or not!) for example: hamming('010001001111', '01010100') should return 5(1 bit changed plus 4 bits "lost" from the end). 2. write a main function that ask user for two file names, open files and read the 1st line of each, and compares them using Hamming...
PLEASE ONLY ATTEMPT IF YOU ARE GOING TO DO ALL QUESTIONS; IF NOT WILLING TO DO...
PLEASE ONLY ATTEMPT IF YOU ARE GOING TO DO ALL QUESTIONS; IF NOT WILLING TO DO ALL QUESTIONS THEN DO NOT ATTEMPT THIS AS THERE ARE OTHER PEOPLE THAT WILL. THIS IS MY LAST QUESTION I CAN NOT AFFORD TO ASK ANOTHER ONE SO PLEASE DO ALL QUESTIONS! THANK YOU What is the difference between absorption costing and variable costing? - Are selling and administrative expenses treated as product costs or as period costs under variable costing? - Explain how...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT