1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
devlian [24]
3 years ago
15

Write a Python program that uses function(s) for writing to and reading from a file:

Computers and Technology
1 answer:
swat323 years ago
6 0

import random

def random_number_file_writer(nums):

   f = open("random.txt", "w")

   i = 0

   while i < nums:

       f.write(str(random.randint(1,500))+"\n")

       i += 1

   f.close()

def random_number_file_reader():

   f = open("random.txt", "r")

   total = 0

   count = 0

   for x in f.readlines():

       total += int(x)

       count += 1

   print("The total of the numbers is "+str(total))

   print("The number of random numbers read from the file is "+str(count))

def main():

   random_number_file_writer(int(input("How many random numbers do you want to generate? ")))

   random_number_file_reader()

main()

I hope this helps!

You might be interested in
If you've finished working with a data file but intend to work on it again during your work session, which button would you use
mariarad [96]
Hey there! Hello!

So, not sure if there's actually a button to "close out" a window to where you're able to reopen it again without having to go to where the file is located or go to recents in your application. Keep in mind that I'm on a MacBook, but I have worked on a Windows computer before. 

One thing you can do is minimize the window. Your program will remain open, but your window will remain out of your way until you decide to expand it again. This seems to be the closest thing possible to your description, but if you had something else in mind, I'd love to hear more details.

This won't be a good solution if you're looking to cut down on the energy that certain programs may be taking up while open, even if you're not using them. But in terms of getting the windows off your desktop, minimizing is the way to go. It also allows you to have documents/windows out of the way while still being able to work on other documents/windows in the program (i.e. Microsoft Word, Google Chrome, Pages, etc.)

Hope this helped you out! Feel free to ask me any additional questions you may have. :-)
5 0
2 years ago
Read 2 more answers
Which of the following is the best example of a manager with a delegator leadership style
Vera_Pavlovna [14]
I think the answer should be answer D.
8 0
3 years ago
Read 2 more answers
Which of these statements performs real number quotient division using type casting?
lara [203]

Answer:

Double x = 35 / 10;

Explanation:

Java provides simple data types for representing integers, real numbers, characters, and Boolean types. These types are known as _ or fundamental types.

<3

3 0
2 years ago
Software obtained illegally is called open-source software. true / false
Oxana [17]
False, software obtained illegally is called Pirated Software.
<span>Open source software is software developed by and for the user community, ie. Linux based software is Open Source.</span>
3 0
3 years ago
Using Python I need to Prompt the user to enter in a numerical score for a Math Class. The numerical score should be between 0 a
yaroslaw [1]

Answer:

<em>The program doesn't use comments; See explanation section for detailed line by line explanation</em>

<em>Program starts here</em>

def lettergrade(subject,score):

     print(subject+": "+str(score))

     if score >= 90 and score <= 100:

           print("Letter Grade: A")

     elif score >= 80 and score <= 89:

           print("Letter Grade: B")

     elif score >= 70 and score <= 79:

           print("Letter Grade: C")

     elif score >= 60 and score <= 69:

           print("Letter Grade: D")

     elif score >= 0 and score <= 59:

           print("Letter Grade: F")

     else:

           print("Invalid Score")

maths = int(input("Maths Score: "))

english = int(input("English Score: "))

pe = int(input("PE Score: "))

science = int(input("Science Score: "))

arts = int(input("Arts Score: "))

lettergrade("Maths Class: ",maths)

lettergrade("English Class: ",english)

lettergrade("PE Class: ",pe)

lettergrade("Science Class: ",science)

lettergrade("Arts Class: ",arts)

Explanation:

The program makes the following assumptions:

Scores between 90–100 has letter grade A

Scores between 80–89 has letter grade B

Scores between 70–79 has letter grade C

Scores between 60–69 has letter grade D

Scores between 0–69 has letter grade E

<em>Line by Line explanation</em>

This line defines the lettergrade functions; with two parameters (One for subject or class and the other for the score)

def lettergrade(subject,score):

This line prints the the score obtained by the student in a class (e.g. Maths Class)

     print(subject+": "+str(score))

The italicized determines the letter grade using if conditional statement

<em>This checks if score is between 90 and 100 (inclusive); if yes, letter grade A is printed</em>

<em>      if score >= 90 and score <= 100:</em>

<em>            print("Letter Grade: A")</em>

<em />

<em>This checks if score is between 80 and 89 (inclusive); if yes, letter grade B is printed</em>

<em>      elif score >= 80 and score <= 89:</em>

<em>            print("Letter Grade: B")</em>

<em />

<em>This checks if score is between 70 and 79 (inclusive); if yes, letter grade C is printed</em>

<em>      elif score >= 70 and score <= 79:</em>

<em>            print("Letter Grade: C")</em>

<em />

<em>This checks if score is between 60 and 69 (inclusive); if yes, letter grade D is printed</em>

<em>      elif score >= 60 and score <= 69:</em>

<em>            print("Letter Grade: D")</em>

<em />

<em>This checks if score is between 0 and 59 (inclusive); if yes, letter grade F is printed</em>

<em>      elif score >= 0 and score <= 59:</em>

<em>            print("Letter Grade: F")</em>

<em />

<em>If input score is less than 0 or greater than 100, "Invalid Score" is printed</em>

<em>      else:</em>

<em>            print("Invalid Score")</em>

This line prompts the user for score in Maths class

maths = int(input("Maths Score: "))

This line prompts the user for score in English class

english = int(input("English Score: "))

This line prompts the user for score in PE class

pe = int(input("PE Score: "))

This line prompts the user for score in Science class

science = int(input("Science Score: "))

This line prompts the user for score in Arts class

arts = int(input("Arts Score: "))

The next five statements is used to call the letter grade function for each class

lettergrade("Maths Class: ",maths)

lettergrade("English Class: ",english)

lettergrade("PE Class: ",pe)

lettergrade("Science Class: ",science)

lettergrade("Arts Class: ",arts)

7 0
2 years ago
Other questions:
  • Jason is working on a project that requires him to manage a huge amount of data. The spreadsheet he is working on has data relat
    12·1 answer
  • why is wrong timing, poor technology, bad implementation and politics in business affected the OSI model as a standard
    5·1 answer
  • Which of the following topics should you avoid bringing up during a college interview?
    10·2 answers
  • Which of the following code segments could be used to skip the first two characters of an input line (they may or may not be whi
    13·1 answer
  • To rotate text in a cell select the option in the alignment grouping
    9·2 answers
  • Assume that a 5 element array of type string named boroughs has been declared and initialized. Write the code necessary to switc
    5·1 answer
  • What do you mean by flow of program​
    6·1 answer
  • New and just need help with C coding. I've tried if statements and it outputs the wrong number.
    6·2 answers
  • Write a function sumOfMultiples, that inputs two integers - seed and cap. The function should return the sum of all the multiple
    6·1 answer
  • Which tab do you open to access the Comments feature?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!