I have an assignment to write a BMI
class with the following instance variables: first_name
, last_name
, identity_card
, height
, and weight
. The I must calculate the BMI value. I also need to write a program that will read a file into the BMI
class instances. The program will calculate the BMI value and print all the original data plus the calculated BMI for a new file.
For example, with the file:
Yossi Bar 012345678 178 70
Yuri Gagarin 123456789 157 50
Dana Kama 234567890 160 60
Yona Anerox 345678901 165 43
Dina Dot 456789012 170 80
Rubi Rasmos 567890123 180 100
Rodika Rambam 678901234 170 60
My problem is that I can't understand how to add the BMI calculation into the new file.
Here is what I have:
class BMI:
def __init__(self, first_name, last_name, identity_card, height, weight):
self._first_name = first_name
self._last_name = last_name
self._identity_card = identity_card
self._height = height
self._weight = weight
def GetName(self):
return self._first_name + " " + self._last_name
def GetID(self):
return self.identity_card
def GetHeight(self):
return self.height
def GetWeight(self):
return self.weight
def GetBMI(self):
return (self._weight) / self._height ** 2
def makebmi(infostr):
first_name, last_name, identity_card, height, weight = infostr.split("\t")
return BMI(first_name, last_name, identity_card, height, weight)
def main():
filename = input("enter the file name: ")
newfilename = input("enter the new file name: ")
with open(filename,'r') as firstfile, open(newfilename,'a') as secondfile:
# read content from first file
for line in firstfile:
# append content to second file
secondfile.write(line)
if __name__ == '__main__':
main()
I modified ur code a little bit. try it and tell me is that what u wanted.
class BMI:
def __init__(self, first_name, last_name, identity_card, height, weight):
self._first_name = first_name
self._last_name = last_name
self._identity_card = identity_card
self._height = height
self._weight = weight
def GetName(self):
return self._first_name + " " + self._last_name
def GetID(self):
return self.identity_card
def GetHeight(self):
return self.height
def GetWeight(self):
return self.weight
def GetBMI(self):
return round(int(self._weight) / int(self._height) ** 2,3)
def makebmi(infostr):
first_name, last_name, identity_card, height, weight = infostr.split()
return BMI(first_name, last_name, identity_card, height, weight)
def main():
filename = input("enter the file name: ")
newfilename = input("enter the new file name: ")
with open(filename,'r') as firstfile, open(newfilename,'a') as secondfile:
# read content from first file
for line in firstfile:
# append content to second file
secondfile.write(line.rstrip()+" " + str(makebmi(line).GetBMI())+"\n")
if __name__ == '__main__':
main()
or if u want to sort it from largest to smallest bmi.This is one way.
class BMI:
def __init__(self, first_name, last_name, identity_card, height, weight):
self._first_name = first_name
self._last_name = last_name
self._identity_card = identity_card
self._height = height
self._weight = weight
def GetName(self):
return self._first_name + " " + self._last_name
def GetID(self):
return self.identity_card
def GetHeight(self):
return self.height
def GetWeight(self):
return self.weight
def GetBMI(self):
return round(int(self._weight) / int(self._height) ** 2, 3)
def makebmi(infostr):
first_name, last_name, identity_card, height, weight = infostr.split()
return BMI(first_name, last_name, identity_card, height, weight)
def main():
filename = input("enter the file name: ")
newfilename = input("enter the new file name: ")
with open(filename, 'r') as firstfile, open(newfilename, 'a') as secondfile:
# read content from first file
ls = []
for line in firstfile:
# append content to second file
ls.append(str(makebmi(line).GetBMI()) + "-" + line.rstrip())
ls.sort(reverse=True)
for ele in ls:
secondfile.write(' '.join(ele.split('-')[1:])+" " +ele.split('-')[0]+'\n')
if __name__=='__main__':
main()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.