I have this function:
def is_an_oak(name):
"""
Returns True is name starts with 'quercus'
>>> is_an_oak('Fagus sylvatica')
False
>>> is_an_oak('Quercuss petraea')
False
>>> is_an_oak('Quercus petraea')
True
>>> is_an_oak('quercus petraea')
True
"""
return name.lower().startswith('quercus')
But I need it to return "Quercuss" and any other similar spelling mistakes as False, currently it is still registering as True. I know this is because of the .startswith() method but I don't know what to replace this with in order to accept "Quercus" as the only True answer.
CodePudding user response:
Actually, your code is very close, but miss one place in the extra character after the correct word. For example: if you compare 'abc' with 'abcd' - what'll you will expect? (using startswith method, will get True!) See the correction and other version to compare:
def is_an_oak(text):
return text.lower().startswith( 'quercus ') # add space after the word so it can detect the extra "s"
def is_an_oak(text):
words = text.split() # break text into words
return words[0].lower() == 'quercus' # first word
Running some tests:
words = ['Quercuss petrea', 'Quercus petrea']
for w in words:
print(is_an_oak(w))
# Outputs:
# False
# True