无法在Python 3.X中解决简单的Python Payroll程序

时间:2016-05-02 02:37:55

标签: python-3.x

这就是问题所在。

编写程序以解决简单的工资核算。查找给定的工资额,工作小时数和每小时费率。 (计算工资单的公式是工资=小时工资*工作小时数。)

What i have so far     def hours():     小时=输入(“你工作了几个小时?:”)     返回时间

def rate():
rate= input("How much is your hourly rate?: ")

def grossPay():
grossPay = hours() * rate()
return grossPay

def main():
print("your gross pay is"), + (grossPay)
return grossPay

def main():
print('Payroll Information')
print hours()
print rate()

main()

1 个答案:

答案 0 :(得分:0)

代码存在一些问题......

  • 您的代码中没有缩进,这是Python所必需的(尽管您可能只是将其粘贴到SO中)
  • 您调用的是小时功能,但代码模糊中不存在一个
  • 意见:对于简单的操作似乎不必要复杂

有很多方法可以做到这一点,但把它全部放在一个函数中对我来说是最自然的。我会做这样的事......

def calc_and_show_pay():
    rate = input("How much is your hourly rate?: ")     #collect the hourly rate
    hours = input("How many hours did you work?: ")     #collect number of hours worked
    gross_pay = float(rate) * float(hours)  #Convert string input to floats so we can do math on it
    print("Payroll Information:")           #print out results, per your format in the example
    print("Your pay is %f"%(gross_pay))     #could also do print("your pay is:", gross_pay)
    return gross_pay    #you don't need to return this unless you want to use this number elsewhere (i.e., you have a bigger program where you'll use this as an input somewhere else).

然后你可以随意调用“calc_and_show_pay()”。