I was trying to print the index of the values in a string but I keep on running to the error NameError: name 'findWithException' is not defined
but I defined it right below the class. What is causing this? Any help is appreciated. Thanks in advance.
class MyString(str):
def findWithException(s, c):
try:
x = s.index(c)
print(x)
except:
print("Not found")
s = MyString("abcdef")
findWithException(s, "c")
I guess that's what you wanted to acheive:
class MyString(str):
def find_with_exception(self, c):
try:
x = self.index(c)
print(x)
except:
print("Not found")
s = MyString("abcdef")
s.find_with_exception("c")
Edit , after you clarified your issue:
def findWithException(s, c):
if c in s:
x = s.index(c)
print(x)
else:
print("Not found")
That's just what you seem to need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.