因此,该程序应要求用户输入他们的年薪,然后将用户的年薪除以52,以产生周薪,然后显示该薪水。这是我到目前为止编写的代码,每次运行时,都会得到以下runtime error
:
TypeError:只能将str(而不是“ float”)连接到str”
AnnualSalary = float(input("Please enter your Yearly salary: "))
print("Yearly salary is: " + AnnualSalary)
WeeklySalary = AnnualSalary/52
print("According to your yearly salary, Your weekly salary is " + WeeklySalary)
答案 0 :(得分:0)
在打印时,您需要通过将变量包装在str(AnnualSalary)周围来将年薪转换为字符串。那应该解决您的问题。 WeeklySalary也是如此。
因为打印正在寻找字符串。
AnnualSalary = float(input("Please enter your Yearly salary: "))
print("Yearly salary is: " + str(AnnualSalary))
WeeklySalary = AnnualSalary/52
print("According to your yearly salary, Your weekly salary is " + str(WeeklySalary))
答案 1 :(得分:0)
请参见
print("Yearly salary is: " + AnnualSalary)
TypeError: can only concatenate str (not "float") to str"
这里的类型出了点问题,错误是当期望str变为float时,所以我们只需要将float转换为str。
print("Yearly salary is: " + str(AnnualSalary))
print("According to your yearly salary, Your weekly salary is " + str(AnnualSalary/52)