TypeError:第8行的Sub:'str'和'int'的操作数类型不受支持

2024-10-02 00:34:52 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么我总是得到这个答案? TypeError:第8行Sub:'str'和'int'的操作数类型不受支持

#Define payment, knowing that up to 40 hours it is normal rate, and above that every hour is paid at 150%.
totalHours = input("Enter the total amount of worked hours:\n")
hourlyWage = input("Enter the payrate per hour:\n")
if totalHours <= 40:
    regularHours = totalHours
    overtime = 0
else:
    overtime = float(input(totalHours - 40))
    regularHours = float(input(40))
payment = hourlyWage*regularHours + (1.5*hourlyWage)*overtime
print (payment)

Tags: the答案inputthatispaymentfloatenter
2条回答

您需要添加int转换。你知道吗

totalHours = int(input("Enter the total amount of worked hours:\n"))
hourlyWage = int(input("Enter the payrate per hour:\n"))

input得到的是str而不是int,因此不能用strint进行数学运算。你知道吗

在python3中,如果您提供输入,它只接受字符串形式的输入。你需要把它转换成int。另外,else部分的输入是不必要的

#Define payment, knowing that up to 40 hours it is normal rate, and above that every hour is paid at 150%.
totalHours = int(input("Enter the total amount of worked hours:\n"))
hourlyWage = int(input("Enter the payrate per hour:\n"))
if totalHours <= 40:
    regularHours = totalHours
    overtime = 0
else:
    overtime = float(totalHours - 40)
    regularHours = float(40)
payment = hourlyWage*regularHours + (1.5*hourlyWage)*overtime
print (payment)

相关问题 更多 >

    热门问题