Question

I need help in Python. This is a two step problem So I have the code...

I need help in Python.

This is a two step problem

So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:)

7. Random Number File Writer

Write a program that writes a series of random numbers to a file. Each random number

should be in the range of 1 through 500. The application should let the user specify how

many random numbers the file will hold.

* Make sure your program starts and is driven from a main() function.
* The name of the file generated should be numbers.txt
* Your program should have at least one additional function that you write and use.

This is my code for the first one I need help for the second problem:

# import the random module
import random

def main():
'''
Function that opens the file and calls another function for writing data to it and then closes the file
'''
# open a file for writing and assign it to the variable numbers.
# this will reference the file object
random_numbers = open('numbers.txt', 'w')
  
# get the number of random numbers to be generated
qty_numbers = int(input('Input how many random numbers should be written to the file: '))
  
#Writing random numbers to file
writeRandomNumbers(random_numbers, qty_numbers);
  
# close the file
random_numbers.close()
  
def writeRandomNumbers(random_numbers, qty_numbers):
'''
Function that generates user specified number of random numbers and writes to file
'''
print('Your list of random numbers are: ')
  
# create a loop to generate the random numbers in the quantity specified
for count in range (qty_numbers):
number = random.randint(1,500)
  
# print the list of random numbers
print(number)
  
#convert the numbers
random_numbers.write(str(number)+ '\n')
  
# tell the user
print('Your list of random numbers have been written to the file named numbers.txt');
  
# call the main function
main()

This is the one my struggling with the extra requirements and need help verifying it's correct :)

8. Random Number File Reader

This exercise assumes you have completed Programming Exercise 7, Random Number File

Writer. Write another program that reads the random numbers from the file, display the

numbers, and then display the following data:

• The total of the numbers

• The number of random numbers read from the file

REQUIREMENTS THAT I AM STUCK ON!
*Make sure to use exception handling to check for the existence of the file.
* Make sure your program starts and is driven from a main() function.
* Your program should use the same numbers.txt file from the previous problem.
* Your program should have at least one additional function that you write and use.

This is my code for the second one:

def main():
random_numbers = open('numbers.txt', 'r')
number = 0
total = 0
print("List of numbers:")
for line in random_numbers.readlines():
print(line)
total = total+int(line)
number +=1
print("The total of the numbers = "+str(total))
print("The number of the random numbers read from the file = "+str(number))
print('Please hit enter to continue...')
main()

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
I need help in Python. This is a two step problem So I have the code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================")...

    Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================") print(" Baseball Team Manager") print("MENU OPTIONS") print("1 - Calculate batting average") print("2 - Exit program") print("=====================================================================")    def convert_bat(): option = int(input("Menu option: ")) while option!=2: if option ==1: print("Calculate batting average...") num_at_bats = int(input("Enter official number of at bats: ")) num_hits = int(input("Enter number of hits: ")) average = num_hits/num_at_bats print("batting average: ", average) elif option !=1 and option !=2: print("Not a valid...

  • I am having a little trouble with my Python3 code today, I am not sure what...

    I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...

  • I am writing python code. I submitted an assignment but the professor said it was a...

    I am writing python code. I submitted an assignment but the professor said it was a modularization. Can you help me see what part of the code is modularization? Pseudocode: 1. Decalre MainMethod () function: # A. Parameters: Three numbers # B. Put the three numbers in reverse # 2. Main Program # A. Initialize Varibles # B. Accepts three values from the user # C. Invoke MainMethod () function, passing the three numbers as arguments in reverse # D....

  • Please use Python for this program Random Number File Writer Write a program that writes a...

    Please use Python for this program Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range 1 through 500. The program should let the user specify how many random numbers to put into the file. The name of the file to write to should be 'random.txt'. Submit the random.txt file generated by your program with the assignment. Sample program execution: Python 3.4.3 Shell Eile Edit...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • Hey all, I am writng some code nad this is what I have so far, I...

    Hey all, I am writng some code nad this is what I have so far, I am stuck -Write a function named "write_list" that takes a list of strings as a parameter and writes the contents of the list to a file named "stranger.txt" with one value per line. If a file named "stranger.txt" already exists it must be overwritten. The function should not return any value def write_list(lst): with open ("stranger.txt","w") as file: for string in lst: file.write(string+'\n') write_list(lst)

  • Python Problem Hello i am trying to finish this code it does not save guesses or...

    Python Problem Hello i am trying to finish this code it does not save guesses or count wrong guesses. When printing hman it needs to only show _ and letters together. For example if the word is amazing and guess=a it must print a_a_ _ _ _ not a_ _ _ _ and _a_ _ _ separately or with spaces. The guess has a maximum of 8 wrong guesses so for every wrong guess the game must count it if...

  • I need help writing a very basic code in python. The code should not be very...

    I need help writing a very basic code in python. The code should not be very complicated. The code must have the following things in it: 1. create a function that takes a state and length of characters and makes a license plate and use the function to make a plate based on user input (use the def function and random.choice as well as import random) 2. the license plate should be a mix of random letters and numbers based...

  • I need python help .. I need to know if a user were to input 3...

    I need python help .. I need to know if a user were to input 3 commands to a program and run it such as specify: Usage: ./filemaker INPUTCOMMANDFILE OUTPUTFILE RECORDCOUNT (./filemaker is the program)( (INPUTCOOMANDFUILE= a text file that contains key words that need to be searched through then decided what to do ) (RECORDCOUNT= number) Given an input file that contains the following: That specifies the text, then the output, and the number of times to be repeated...

  • Python Programming 4th Edition: Need help writing this program below. I get an error message that...

    Python Programming 4th Edition: Need help writing this program below. I get an error message that says that name is not defined when running the program below. My solution: def main(): filename = "text.txt" contents=f.read() upper_count=0 lower_count=0 digit_count=0 space_count=0 for i in contents: if i.isupper(): upper_count+=1 elif i.islower(): lower_count+=1 elif i.isdigit(): digit_count+=1 elif i.isspace(): space_count+=1 print("Upper case count in the file is",upper_count) print("Lower case count in the file is",lower_count) print("Digit count in the file is",digit_count) print("Space_count in the file is",space_count)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT