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

Please use the python and explain. Also please show python. Suppose that a cashier owes a...
Please use the python and explain. Also please show python. Suppose that a cashier owes a customer some change and that the cashier only has quarters, dimes, nickels, and pennies. Write a program the computes the minimum number of coins that the cashier can return. To solve this problem use the greedy algorithm explained below. PROBLEM STATEMENT: Your program should first ask the user for the amount of money he/she is owed (in dollars). You may assume that the user...
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...
Please use python and run in IDLE Question 1 Write functions that do the following: i)...
Please use python and run in IDLE Question 1 Write functions that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both. Question 2 Write functions: i) One that prompts a user for 2 numbers. ii)...
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...
This is all in Python. Also, the names come from a .txt file that i have...
This is all in Python. Also, the names come from a .txt file that i have created with 3 names on it(Jim,Pam,Dwight). Main Function. Write a program where the user enters a name. Using the read function, check to see if the name is in the text document. If it is, respond back to the user the name is found within the list. Read Function. The read function should return the contents of the text document as a list. EXAMPLE...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT