为什么我一直得到这个答案?类型错误:Sub的操作数类型不受支持:第8行的"str"one_answers"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)
在python3中,如果您提供输入,它只接受字符串形式的输入。您需要将其转换为int。此外,其他部分的输入是不必要的
#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)
您需要添加int
转换。
totalHours = int(input("Enter the total amount of worked hours:n"))
hourlyWage = int(input("Enter the payrate per hour:n"))
从input
得到的是str
,而不是int
,所以不能用str
和int
进行数学运算。