我写了一个所得税计算器的代码,该代码根据一个人的性别计算一个人的所得税。执行时,它将引发错误。我不知道为什么它显示错误。我是初学者,因此请忽略代码中是否存在任何愚蠢的错误
以下是错误的附件图片
def incometax_calc():
income = int(input("Enter your total annual income in lakhs: "))
gender = str(input("Enter m if Male, f if Female: "))
if gender == m:
if 1<(income)<(2.5):
tax = 0
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
if 2.5<=(income)<5:
tax = 10
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
if 5<=(income)<10:
tax = 20
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
if (income)>10:
tax = 30
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
elif gender == f:
if 1<(income)<(2.5):
tax = 0
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
if 2.5<=(income)<5:
tax = 5
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
if 5<=(income)<10:
tax = 10
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
if (income)>10:
tax = 20
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
else:
print("Wrong input!!")
print(incometax_calc())
答案 0 :(得分:2)
您的字符串比较错误。应该是
gender == 'm'
否则,解释器认为它是变量而不是字符串。
答案 1 :(得分:2)
if gender == m:
在这里,您正在与名为m的变量进行比较,而不是字符串'm'。
要解决此问题,请将该变量变成字符串,如下所示:
if gender == 'm':
答案 2 :(得分:1)
Python认为m
是一个变量名。用引号将变量gender
与值'm'
进行比较。
if gender == 'm':
...
答案 3 :(得分:1)
错误在这里:
if gender == m:
如果您以此方式编写,他会查看m
是否为变量,但是您希望"m"
为字符串。
答案 4 :(得分:1)
从堆栈跟踪中:
第8行:未定义名称“ m”
语句if gender == m
引发错误,因为程序正在寻找未定义的变量m
。如果要比较变量性别和字符串值m
,则应将其括在双引号或单引号中。
答案 5 :(得分:0)
这是一个简单的定义错误。
df<-data.frame()
sample <- c(8.93,9.11,9.12,9.05,8.87,8.95,9.02)
combn(sample,2) -> com1
for (i in 1:ncol(com1)){
com1[,i]
setdiff(sample,com1[,i]) -> com2
combn(com2,2) -> com3
for (j in 1:ncol(com3)){
setdiff(com2,com3[,j]) -> com4
c(com1[,i],com3[,j],com4) -> de
df <- rbind(df,de)
}
}
df
变量if gender == m:
在使用前尚未在代码中定义。
尝试类似的东西:
m
OR
m = "m"
if gender == m
如果m是您在此方法中引用的global变量,请指定
if gender == 'm'
在global m
首次使用之前。
答案 6 :(得分:0)
m
未定义。
在这里,您将m作为python对象调用。
您正在寻找的是"m"
您的代码将如下所示。
def incometax_calc():
income = int(input("Enter your total annual income in lakhs: "))
gender = str(input("Enter m if Male, f if Female: "))
if gender == "m":
if 1<(income)<(2.5):
tax = 0
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
if 2.5<=(income)<5:
tax = 10
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
if 5<=(income)<10:
tax = 20
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
if (income)>10:
tax = 30
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
elif gender == "f":
if 1<(income)<(2.5):
tax = 0
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
if 2.5<=(income)<5:
tax = 5
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
if 5<=(income)<10:
tax = 10
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
if (income)>10:
tax = 20
tax_ded = ((tax/100)*income)
final_amt = (income)-(tax_ded)
print("Your annual income: ",income)
print("Tax percentage applied: ",tax)
print("Tax deducted: ",tax_ded)
print("Final amount: ",final_amt)
else:
print("Wrong input!!")
print(incometax_calc())