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)
4 Respostas
+ 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.
+ 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.
+ 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
0
Thnx lpang ,Russ . I learn . Your guidance will helpful for me in future also