Question

Write a Python program n_kids.py that reads the data from n_kids.txt, does some math, then displays...

Write a Python program n_kids.py that reads the data from n_kids.txt, does some math, then displays the following:

Total number of families:

Total number of kids:

Average number of kids per family:

Maximum number of kids in a family:

Minimum number of kids in a family:

Extra credit (2 pts): write the above results to a different file (results.txt, not to the input file n_kids.txt) in addition to displaying them on the screen.

Do not use built-in min and max functions. << Mostly need help in finding Min Max without the functions

** So n.kids,txt is just a list of numbers. **

But I need help getting the Min and Max values from the list of numbers.

Any assistance will be appreciated. Will like and comment  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

l = []
#taken an empty list named "l"

#opened the file named "n_kids.txt" and appended each line in it to the list
with open ("n_kids.txt", 'r') as x:
    for row in x:
        l.append(int(row))

#using for calculating number of families      
f=0
for i in l:
    f+=1

#using for calculating number of kids
k =0
for s in l:
    k+=s
  
#calculating average number of kids per family
average = (k//f)

#calculating maximum kids using for loop and index
maximum = l[0]
for m in l:     
    if m > maximum:
      maximum = m
#calculating minimum kids using for loop and index
minimum = l[0]
for n in l:     
    if n < minimum:
      minimum = n

#printing the output
print("Total number of families:",f)
print("Total number of kids:",k)
print("Average number of kids per family:", average)
print("Maximum number of kids in a family:",maximum)
print("Minimum number of kids in a family:",minimum)

n_kids.txt:

2
3
3
4
2
2
1
4
3
2
3

Note: As you asked in the question, I didn't used min() and max() functions

addition to that I didn't used builtin function to calculate other things as well.

Please comment If you want the other things i.e, no of kids, no of families by using builtin function

Screenshot of the code:

Screenshot of txt file:

Screenshot of Output:

Add a comment
Know the answer?
Add Answer to:
Write a Python program n_kids.py that reads the data from n_kids.txt, does some math, then displays...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (statistics.py) Write a program that reads data from the file provided, data.txt, into a list. Once...

    (statistics.py) Write a program that reads data from the file provided, data.txt, into a list. Once the data are in a list, calculate and print the following: sum = 69.28 mean = 3.46 min = 0.19 max = 8.29 You may use the Python built-ins, min(), max(), and sum(). Format the numbers using '.2f'.

  • Write the PYTHON code that displays the following numbers: The program should have a header that...

    Write the PYTHON code that displays the following numbers: The program should have a header that displays "Number   Doubled Tripled" above the start of the numbers. If putting all numbers on the same line, then they will need to be separated so you can see he individual number.(https://www.programiz.com/python-programming/methods/built-in/print) The program should display Every whole number from 1 through 35. The program should display that number doubled. The program should also display that number tripled. Make a working version of this...

  • python Write a program that reads the name of fruits and the colors from an input...

    python Write a program that reads the name of fruits and the colors from an input file fruit. txt. Each fruit name and the color is given on a line as shown below separated by space Apple Red Orange Orange Grape Green Now write output to an output file (fruit2.txt) as shown below Color of Apple is Red Color of Orange is Orange Color of Grape is Green The program has to be general to read in as many lines...

  • Write a program that reads the scores from the file and displays their total and average.

    Suppose that a text file Lab2.txt contains an unspecified number of scores. Write a program that reads the scores from the file and displays their total and average.In the text file, the scores are separated by blanks.The program should contain two methods with these signatures (it can contain other methods besides these if you think appropriate):public static double totalScores(File inFile)public static double averageScores(File inFile)The output from the program should look like this successful sample run from my solution:The total of...

  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • Write a Python program that reads from the user 4 item prices then displays the total...

    Write a Python program that reads from the user 4 item prices then displays the total and average price. The program should also display the taxes ( using the TAX_RATE=8.75) and the total after tax.

  • 1.Write a python program that writes a series of random numbers to a file named random.txt....

    1.Write a python program that writes a series of random numbers to a file named random.txt. Each random number should be in the range of 1 through 300. The application should let the user specify how many random numbers the file will hold. 2. Write another program that reads the random numbers from the random.txt file, displays the numbers, then displays the following data: I. The total of the numbers II. The number of random numbers read from the file

  • Use Python Write a script to perform various basic math and string operations. Use some functions...

    Use Python Write a script to perform various basic math and string operations. Use some functions from Python's math module. Generate formatted output. Basic math and string operations Calculate and print the final value of each variable. a equals 3 to the power of 2.5 b equals 2 b equals b + 3 (use +=) c equals 12 c = c divided by 4 (use /=) d equals the remainder of 5 divided by 3 Built-in functions abs, round, and...

  • Write a Python program that reads text from a file, encrypts it with a Caesar Cipher,...

    Write a Python program that reads text from a file, encrypts it with a Caesar Cipher, and displays the encrypted text. Do not process punctuation. Convert the original string to all lower-case before encrypting it.

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