好的,我有一个编制汽车销售计划的任务,该程序可以计算销售人员一周内的销售额。我已经知道所有汽车销售的数量以及他赚多少佣金。这是我的代码:
def main():
print ('This program will compute the comission earned for the week based on your sales for the week.')
car_number = float(input('Enter number of cars sold :'))
def calculate_total(car_number,price,commission_rate):
price = 32,500.00
commission_rate = .025
calculate_total = car_number * price * commission_rate
return calculate_total(car_number)
print('The weekly gross pay is $',calculate_total)
main()
该计划由于某种原因不起作用,但无论如何我决定将它提交给我的教授。然后他回答说我没有被要求创建一个新功能,我必须删除它并在main中工作。有人可以告诉我这意味着什么吗?
答案 0 :(得分:1)
两件事:
'在主要工作',因为你的教授说,这意味着你没有定义任何功能。您的所有代码都位于文件中,没有任何def ...
语句。我知道这可能不太清楚。这是一个例子:
import os
print "Your current working directory is:"
print os.getcwd()
这种编程更多的是“脚本”的感觉 - 你没有定义你将不止一次使用的程序部分,而且你没有煞费苦心地分解程序可以用于单一功能。
其次,你已经以这样的方式输入价格,即Python认为你创建了一个数字元组而不是单个值。
Python将{p>price = 32,500.00
解释为创建元组,其中包含值32
和500.00
。你真正想要的是:price = 32500.00
。
我崩溃了,为你完成了这个过程。
print ('This program will compute the comission earned for the week based on your sales for the week.')
car_number = float(input('Enter number of cars sold :'))
price = 32500.00
commission_rate = .025
calculate_total = car_number * price * commission_rate
print('The weekly gross pay is $',calculate_total)
答案 1 :(得分:0)
抱歉,我之前没有看到完整的问题,但无论如何这是没有功能的正确答案
关键字try
和except
用于处理错误。如果你输入一些无效的东西让我们说一个字母而不是数字会抛出一条消息
(Could not convert input data to a float.
)
def main():
print ('This program will compute the comission earned for the week based on your sales for the week.')
try:
#before: car_number = float(raw_input('Enter number of cars sold :'))
car_number = float(input('Enter number of cars sold :'))
except ValueError:
#before: print 'Could not convert input data to a float.'
print('Could not convert input data to a float.')
print('The weekly gross pay is ${}'.format(car_number * 32500.00 * 0.025 )))
main()
如果你甚至不想main()
这里的功能就是答案:
print ('This program will compute the comission earned for the week based on your sales for the week.')
try:
car_number = float(input('Enter number of cars sold :'))
except ValueError:
print('Could not convert input data to a float.')
print('The weekly gross pay is ${}'.format(car_number * 32500.00 * 0.025 )))