Issue
i am currently working on a calculator using pandas, and to determine a variable I have used a nested if statement. But unfortunately, I get an invalid syntax error from the below code.
if net_option == 'Y':
if age <= 30:
desired_net = 0.15
elif age >= 31 and <= 60:
desired_net = 0.12
elif age >= 61 and <= 90:
desired_net = 0.11
elif age >= 91 and <= 140:
desired_net = 0.10
elif age >= 141 and <= 180:
desired_net = 0.09
elif age >= 181 and <= 245:
desired_net = 0.08
elif age >= 246 and <= 270:
desired_net = 0.07
elif age >= 271 and <= 365:
desired_net = 0.6
elif age >= 366 and <= 500:
desired_net = 0.3
elif: age >= 501 and <= 700:
desired_net = 0.0
elif age > 700:
desired_net = -0.1
else:
desired_net = 0.1
else:
desired_net = desired_net
Error message:
elif age >= 31 and <= 60:
^
SyntaxError: invalid syntax
I think I am missing something here as I am pretty sure that <= is a valid syntax.
Has anyone got an idea what may be wrong? Let me know if you would like to see more lines.
Solution
elif age >= 31 and age <= 60:
When using the and
operator, you are required to write the variable to compare against again, because each expression before and after and
stands on its own.
Answered By - TuxStash.de
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.