Reference no: EM132677782
Practical - Files
Greater-Than Counter
Write a program that reads a file of floating-point numbers, and counts how many of those numbers are larger than a user-specified lower limit.
Example file:
Copy this text into a new file called recentrain.txt,
or download it here.
12.4
10.0
10.1
0
8.4
13.9
19.1
141.0
33.5
1.2
Example output:
Filename: recentrain.txt
Threshold: 10
Processing...
6 out of 10 (60.0%) of values are greater than 10.0.
BMI File
File: bmi_files.py
Write a program that writes to and reads from bmis.txt (not at the same time).
Reuse your code from Prac 7 where you calculated a person's BMI based on height and weight.
Prac 7 :Write a new main program with a loop that runs through a number of weights for a 1.75m person, as below.
value = 3.1415926535
print(f"{value:.1f}") # prints 3.1
The : separates the variable name (value) from the "format specifier".
In this case, .1f means to produce only 1 decimal place for our float.
Another common format specifier is just a number, like:
value = 17
print(f"!{value:4}!") # prints ! 17! (that is, 17 takes up 4 spaces)
OK, let's do this:
Height 1.75m, Weight 50kg = BMI 16.3, considered underweight
Height 1.75m, Weight 52kg = BMI 17.0, considered underweight
Height 1.75m, Weight 54kg = BMI 17.6, considered underweight
Height 1.75m, Weight 56kg = BMI 18.3, considered underweight
Height 1.75m, Weight 58kg = BMI 18.9, considered normal
Height 1.75m, Weight 60kg = BMI 19.6, considered normal
Height 1.75m, Weight 62kg = BMI 20.2, considered normal
Height 1.75m, Weight 64kg = BMI 20.9, considered normal
Height 1.75m, Weight 66kg = BMI 21.6, considered normal
Height 1.75m, Weight 68kg = BMI 22.2, considered normal
Height 1.75m, Weight 70kg = BMI 22.9, considered normal
Height 1.75m, Weight 72kg = BMI 23.5, considered normal
Height 1.75m, Weight 74kg = BMI 24.2, considered normal
Height 1.75m, Weight 76kg = BMI 24.8, considered normal
Height 1.75m, Weight 78kg = BMI 25.5, considered overweight
Height 1.75m, Weight 80kg = BMI 26.1, considered overweight
Height 1.75m, Weight 82kg = BMI 26.8, considered overweight
Height 1.75m, Weight 84kg = BMI 27.4, considered overweight
Height 1.75m, Weight 86kg = BMI 28.1, considered overweight
Height 1.75m, Weight 88kg = BMI 28.7, considered overweight
Height 1.75m, Weight 90kg = BMI 29.4, considered overweight
Height 1.75m, Weight 92kg = BMI 30.0, considered obese
Height 1.75m, Weight 94kg = BMI 30.7, considered obese
Height 1.75m, Weight 96kg = BMI 31.3, considered obese
Height 1.75m, Weight 98kg = BMI 32.0, considered obese
Height 1.75m, Weight 100kg = BMI 32.7, considered obese
Got it? Good. Now, let's try it with varying heights as well as weights, like:
Hint: 1.5m is 150cm; useful to know since range only works with integers.
Notice also that the values line up nicely. You don't have to do this, but see if you can.
Height 1.5m, Weight 50kg = BMI 22.2, considered normal
Height 1.5m, Weight 60kg = BMI 26.7, considered overweight
Height 1.5m, Weight 70kg = BMI 31.1, considered obese
Height 1.5m, Weight 80kg = BMI 35.6, considered obese
Height 1.5m, Weight 90kg = BMI 40.0, considered obese
Height 1.5m, Weight 100kg = BMI 44.4, considered obese
Height 1.6m, Weight 50kg = BMI 19.5, considered normal
Height 1.6m, Weight 60kg = BMI 23.4, considered normal
Height 1.6m, Weight 70kg = BMI 27.3, considered overweight
Height 1.6m, Weight 80kg = BMI 31.2, considered obese
Height 1.6m, Weight 90kg = BMI 35.2, considered obese
Height 1.6m, Weight 100kg = BMI 39.1, considered obese
Height 1.7m, Weight 50kg = BMI 17.3, considered underweight
Height 1.7m, Weight 60kg = BMI 20.8, considered normal
Height 1.7m, Weight 70kg = BMI 24.2, considered normal
Height 1.7m, Weight 80kg = BMI 27.7, considered overweight
Height 1.7m, Weight 90kg = BMI 31.1, considered obese
Height 1.7m, Weight 100kg = BMI 34.6, considered obese
Height 1.8m, Weight 50kg = BMI 15.4, considered underweight
Height 1.8m, Weight 60kg = BMI 18.5, considered normal
Height 1.8m, Weight 70kg = BMI 21.6, considered normal
Height 1.8m, Weight 80kg = BMI 24.7, considered normal
Height 1.8m, Weight 90kg = BMI 27.8, considered overweight
Height 1.8m, Weight 100kg = BMI 30.9, considered obese
Height 1.9m, Weight 50kg = BMI 13.9, considered underweight
Height 1.9m, Weight 60kg = BMI 16.6, considered underweight
Height 1.9m, Weight 70kg = BMI 19.4, considered normal
Height 1.9m, Weight 80kg = BMI 22.2, considered normal
Height 1.9m, Weight 90kg = BMI 24.9, considered normal
Height 1.9m, Weight 100kg = BMI 27.7, considered overweight
The first part of your program should ask the user for a number of people, then repeatedly ask for the details for that many people. (You did just think of what kind of loop to use, didn't you?)
For each person, calculate their BMI, but don't print it to the screen... write it to the file "bmis.txt".
Don't forget to close the file when you have finished.
Test this and confirm it works as you expect.
Then, write the second part, which should read the file and display the BMI and weight category similar to how we've done it before:
BMI 23.5, considered normal
BMI 25.5, considered overweight
BMI 24.2, considered normal
BMI 16.3, considered underweight
BMI 32.0, considered obese
File Filter
File: file_filter.py
Write a program that reads a file and "filters" it, writing only certain lines to another file.
In the lecture, we wrote a program that read letter.txt and printed every line that started with a capital.
For this program, write a program that asks the user for three things:
• input file name
• output file name
• a search string to look for lines that start with
Read the input file, and then for each line that starts with the search string, write those to the output file.
Attachment:- Practical 10.rar