Python 我刚开始学习,第一次作业就被难倒了
我的任务是制作一个简单的工资计算器。当我运行它时,它会要求输入。但当它到达执行计算的部分时,我得到:Python 我刚开始学习,第一次作业就被难倒了,python,Python,我的任务是制作一个简单的工资计算器。当我运行它时,它会要求输入。但当它到达执行计算的部分时,我得到: 回溯(最近一次呼叫最后一次): 文件“C:/Users/werpo/AppData/Local/Programs/Python/Python38-32/Wage Calculator.py”,第17行,在 打印(总工时*小时工资)+(每周销售额*收入*佣金) TypeError:不支持+:“非类型”和“浮点”的操作数类型 这是实际的代码 #Ask how many hours the empl
回溯(最近一次呼叫最后一次):
文件“C:/Users/werpo/AppData/Local/Programs/Python/Python38-32/Wage Calculator.py”,第17行,在
打印(总工时*小时工资)+(每周销售额*收入*佣金)
TypeError:不支持+:“非类型”和“浮点”的操作数类型
这是实际的代码
#Ask how many hours the employee worked this week and assign that value to a varaible
prompt = "How many hours did you work this week?"
total_hours = eval(input(prompt))
#Ask how much revenue the employees total sales for the week brought in
prompt = "How much revenue did your weekly sales bring in?"
weekly_sales_revenue = eval(input(prompt))
#assign hourly wage and commision rate to their own variables
hourly_wage = (20)
commission = (.10)
#perform calculation for total pay as number of hours worked times hourly wage plus commision revenue times commission rate
print (total_hours*hourly_wage)+(weekly_sales_revenue * commission)
就像trincot评论的那样,您需要调用
print
,作为带括号的函数。还有一些其他的注释
20
和.10
eval
来解析输入,而应该使用float
或int
print
,整个过程都需要括号)print(总时数*小时工资)
调用print
函数并返回None
,这对于print
是正常的,然后Python尝试将返回的None
添加到第二对括号(每周销售收入*佣金)
由于不允许添加None
和float
而引发错误
当您这样重新格式化它时,这一点更加清楚
打印(总工时*小时工资)+(每周销售额*收入*佣金)
以下是更改笔记后的代码:
#Ask how many hours the employee worked this week and assign that value to a varaible
prompt = "How many hours did you work this week?"
total_hours = float(input(prompt))
#Ask how much revenue the employees total sales for the week brought in
prompt = "How much revenue did your weekly sales bring in?"
weekly_sales_revenue = float(input(prompt))
#assign hourly wage and commision rate to their own variables
hourly_wage = 20
commission = .10
#perform calculation for total pay as number of hours worked times hourly wage plus commision revenue times commission rate
print(total_hours*hourly_wage + weekly_sales_revenue * commission)
您应该在
print
之后用括号将表达式括起来——这对于python 2是不正确的,但是在python 3上,print
需要用括号括住整个表达式。谢谢!我不敢相信这有多简单。你是个救生员!停止对输入使用eval()
。使用int(输入(…)
转换为整数(不带小数的数字)或float(输入(…)
转换为浮点数。@juanpa.arrivillaga错误是因为调用print
后出现+(…)
。很明显,他们打算将整个表达式作为打印的参数,并且没有在其周围加括号。@NamGVU不,不会的。您必须编写(20,)
来创建一个元组。如果您解释了这导致他们出现错误的原因,这将非常有用。谢谢大家。你帮了大忙。我的指导是一门在线课程,不太深入。“你们已经教会了我很多有用的东西。”巴尔马同意了,编辑了。哦,好的。这是有道理的。我明白为什么我最后会有计算错误。但我不确定我是否理解你为什么说不要使用eval?我实际上是在4小时前开始的,这正是他们在示例中使用的内容。@Esostack哦,好吧,这很有意义。谢谢你的澄清。