0

Is there another way to solve this problem?

hey guys, I defined an object under a class and gave different data types as arguments. No doubt code gave the error. class buffalo: def __init__(self, weight, length): self.weight = weight self.lenght = length bholi = buffalo(100kg, 100m) print(bholi.weight) Now one way to solve this problem is I should write 100kg and 100m= "100kg" and "100m" (whole as string) bholi = buffalo("100kg","100m") but if I want to change argument in some order. for ex weight of the animal now is 100kg but after one year lets take ii as 100+5 kg and I want to do this automatically with the help of the program or by manually (user input). In that condition, I have to split that string("100kg") into two parts ("100"+"kg") then I have to convert 100 into 'int' then again convert 100 into a string and get the answer. the process seems lengthy so I am looking for an alternative to perform the same task.

25th Mar 2020, 2:48 AM
Aniket Singh Rana
Aniket Singh Rana - avatar
1 Réponse
0
Store <weight> and <length> as numbers. The respective measurement units can be set when printing the object. In this code __str__ magic method is overridden to display cow's information. https://code.sololearn.com/cq4v7T6CILiG/?ref=app
25th Mar 2020, 4:32 AM
Ipang