Question

In: Computer Science

Do it in python please! also please use this template please I have provided and below...

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:

  • 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

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!


Related Solutions

I have to do a nursing skill template in a few sentences can you please describe...
I have to do a nursing skill template in a few sentences can you please describe the nursing interventions for trach suctioning (pre,intra,post). and the outcome and evaluations of trach suctioning
C++ Please write a exmaple of class template RedBlackTree.h.(It must be template!). I do not mind...
C++ Please write a exmaple of class template RedBlackTree.h.(It must be template!). I do not mind about details but please include the basic functions that are necessary for a RedBlackTree.
What Account Titles do I use to record these transactions??? ALSO I HAVe to Keep a...
What Account Titles do I use to record these transactions??? ALSO I HAVe to Keep a running balance of the average per share amount of the common shares. On January 1, 2017, Hum Enterprises Inc. had 64,000 common shares, recorded at $512,000. The company follows IFRS. During the year, the following transactions occurred: Apr. 1 Issued 5,000 common shares at $10 per share. June 15 Declared a 5% stock dividend to shareholders of record on September 5, distributable on September...
How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
Record your daily intake for 4 weeks. Use the template below (also included as file) or...
Record your daily intake for 4 weeks. Use the template below (also included as file) or you may create your own. The log should be initiated at the start of the course and end at the conclusion of the course. You will notice a trend in your daily habits. This assignment will allow you to: Become more accountable for your eating habits Identify your motivation for eating Focus on your nutritional requirements Know your food triggers, such as for pinpointing...
Week Food Diary Record your daily intake for 4 weeks. Use the template below (also included...
Week Food Diary Record your daily intake for 4 weeks. Use the template below (also included as file) or you may create your own. The log should be initiated at the start of the course and end at the conclusion of the course. You will notice a trend in your daily habits. This assignment will allow you to: Become more accountable for your eating habits Identify your motivation for eating Focus on your nutritional requirements Know your food triggers, such...
PDCA template and how to do it for financial. have to make a template of our...
PDCA template and how to do it for financial. have to make a template of our own thing. i was gonna do financial.
***PYTHON PLEASE*** I'd also like to use decimal to two places when calculating the money $$...
***PYTHON PLEASE*** I'd also like to use decimal to two places when calculating the money $$ values for budget and fee areas. Thank you! -------------------------------------------------------- Create a Park class. Include the following seven data members: Name of Park Location Type of Park (National, State or Local) Fee Number of Employees Number of visitors reported for the past 12 months Annual Budget Write your __init__ class In addition write four separate instance methods that: Return a string containing the name of...
Use the Python programming language to complete below (I need the New Answer for this, and...
Use the Python programming language to complete below (I need the New Answer for this, and please provide the screenshot of the source code. Thank you!): Write a function to seek for all even numbers and odd numbers in the middle of two number A and B. Print even and odd numbers in 1 and 2020 (including both these two numbers)
Use the Python programming language to complete below (I need the New Answer for this, and...
Use the Python programming language to complete below (I need the New Answer for this, and please provide the screenshot of the source code. Thank you!): A website requests an user to input his account password. Write a program to examize the validity of the password. The valid password must consists of: At least 1 letter in [a-z] At least 1 number in [0-9] At least a letter in [A-Z] (capital) At least one special letter in [$#@] At least...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT