【问题标题】:I am using if/else statements to calculate salary and am receiving this error: TypeError: '<=' not supported between instances of 'str' and 'float'我正在使用 if/else 语句来计算薪水并收到此错误:TypeError: '<=' not supported between 'str' and 'float' instances
【发布时间】:2021-05-17 20:16:48
【问题描述】:
我正在尝试计算双周工资,计算工资增加时的税率。当我使用 if/else 语句更改不同工资的税率时,它给了我一个错误。
salary = (input('Enter salary: '))
marital_status = input('Enter marital status M for married or S for other: ')
if marital_status is 'S':
if salary <= 10000.00:
print(salary * .05)
else salary <= 50000.00:
print(500.00 + (salary * .105)
else salary > 50000.00
print(4700.00 + (salary * .2025)
【问题讨论】:
标签:
python
if-statement
boolean
typeerror
branch
【解决方案1】:
使用“float(string)”函数转换您通过输入接受的字符串。
【解决方案2】:
除了 Roger 的评论之外,您的代码中还有一些未闭合的括号。
另外,也许您应该使用 elif 而不是 else 作为您的 else 语句测试条件。
【解决方案3】:
如果我相信,你应该使用 elif 代替 else。我不知道为什么,但问题是由你的循环中的浮点数引起的,并且如前所述,你的代码中有未闭合的括号。无法解释解决方案,但这是工作代码。
salary = float(input("Enter salary: "))
marital_status = input("Enter marital status M for married or S for other: ")
if marital_stat == "S":
if salary <= 10000:
print(salary * 0.05)
elif salary <= 50000:
print(salary * 0.105)
elif salary > 50000:
print(4700 + (salary*0.2025))