Reputation: 97
The purpose of my code is to separate the digits before, and after my code.
for x in NumberStr: if x == '.': DigitsBeforeDP = NumberStr[:(x-1)] DigitsAfterDP = NumberStr[(x+1):]
This is the code I've written, and I have no idea why I get the error:
Traceback (most recent call last): File "...", line 101, in ConvertToText() File "...", line 97, in ConvertToText DigitsBeforeDP = NumberStr[:(x-1)] TypeError: unsupported operand type(s) for -: 'str' and 'int'
It might have something to do with the fact the NumberStr
is stored as a string?
Can anyone explain what I have done wrong, and tell me how to improve my code.
Upvotes: 1
Views: 1918
Reputation: 4445
You can do this with the str.split()
method.
before, after = number_str.split('.')
Also, the reason you were getting that error is because you were doing (x-1)
but at that point in time x
is a string, '.'
, so you are telling the interpreter to do ('.' - 1)
, which is why it tells you it does not support -
for str
and int
.
You probably meant to have (NumberStr.indexOf(x) - 1)
Upvotes: 1