0

How to make floordiv , pls.correct the code

class SpecialString: def __init__(self, cont): self.cont = cont def __floordiv__(self, other): div = "cont"\\ return (self.cont,other.cont) A = SpecialString("10") B = SpecialString ("4") print (A \ B)

8th Feb 2020, 9:52 AM
sushil mundhra
sushil mundhra - avatar
4 odpowiedzi
+ 4
First up, you seem to be confusing "\"s with "/"s. Floor division uses // as its symbol. Your line div = "cont"\\ is confusing as I'm not sure what you're trying to do here. The syntax is incorrect and you create a variable div but don't use it anywhere.
8th Feb 2020, 9:59 AM
Russ
Russ - avatar
+ 3
You can cast the two string variable <self.cont> and <other.cont> into `int` and return the floor division result of the two `int`. class SpecialString: def __init__(self, cont): self.cont = cont def __floordiv__(self, other): return int(self.cont) // int(other.cont) A = SpecialString("10") B = SpecialString ("4") print (A // B) (Edit) Please tag Python, \\ in your tags didn't help improve context clarity.
8th Feb 2020, 10:05 AM
Ipang
+ 2
The return value of your __floordiv__ function is where you define the behaviour of the strings when they encounter a // operator. I have attached a simple example where // simply concatenates the two strings. https://code.sololearn.com/cdBM35dBSdAd/?ref=app
8th Feb 2020, 10:08 AM
Russ
Russ - avatar
0
Thnx lpang ,Russ . I learn . Your guidance will helpful for me in future also
9th Feb 2020, 4:59 AM
sushil mundhra
sushil mundhra - avatar