I would expect this to be a duplicate question, yet I couldn't find an answer. I'm sorry.
I have written a simple function that's supposed to return either an empty string or a string of one character. Sometimes, however, it returns None. Even though it should match one of the operands and, in case it doesn't, still output a string.
baseconvert = digits+ascii_lowercase+"0"
def checker(operand, factorlist): # hardcoded interpreter of the calculus converted in base 10. i is equal to the base.
if (operand == '+'):
if (factorlist[0] + factorlist[1] == factorlist[2]):
return baseconvert[i]
elif (operand == '-'):
if (factorlist[0] - factorlist[1] == factorlist[2]):
return baseconvert[i]
elif (operand == '*'):
if (factorlist[0] * factorlist[1] == factorlist[2]):
return baseconvert[i]
elif (operand == '/'):
if (factorlist[0] / factorlist[1] == factorlist[2]):
return baseconvert[i]
else:
return ""
So while typing this post I figured out I could just remove the else statement:
# else: is not necessary
return ""
works fine. I still wonder, however, why this else statement is never triggered.
P.S.: Is there a cleaner way to just interpret the string, for example "10 - 7 = 3" to get a boolean result? I'm relatively new to python and couldn't come up with anything.
Aucun commentaire:
Enregistrer un commentaire