Reputation: 11
Basically im making a small piece of code to remove the end character of a string until the string passes .isdigit
.
def string_conversion(string):
for i in string:
if i.isdigit == False:
string = string[0:len[string]-1]
print(string) #This is just here to see if it goes through
test = "60+"
string_conversion(test)
I used http://www.pythontutor.com/visualize.html to see why I wasn't getting any output, it passed a + symbol as a digit, just like the numbers 6 and 0.
Am I doing something wrong or does python treat +
as a digit?
Upvotes: 0
Views: 80
Reputation: 152765
str.isdigit
is a method not an attribute, so (as was already mentioned in the comments) you need to call it.
Also you check each character (starting from the left side) for isdigit
but if it's not passing the isdigit()
test you remove a character from the right side. That doesn't seem right. You probably wanted to iterate over the for i in reversed(string)
(or using slicing: for i in string[::-1]
).
But you also could simplify the logic by using a while
loop:
def string_conversion(string):
while string and not string.isdigit():
string = string[:-1]
return string
Upvotes: 2
Reputation: 6385
def string_conversion(string):
for i, s in enumerate(string):
if s.isdigit() == False:
string = string[:i]
break
print(string) #This is just here to see if it goes through
test = "60+"
string_conversion(test)
Try This
Upvotes: 1