【问题标题】:TypeError: must be real number, not none type plus more errorsTypeError:必须是实数,而不是无类型加上更多错误
【发布时间】:2020-05-07 21:36:27
【问题描述】:

哦,天哪。我已经成为这里的常客了。我有我所有的代码,但这很奇怪,因为我正在尽力而为,但我似乎无法让它工作。我试过重写它的一部分,甚至做了一些小的改动,但它就是行不通。完全没有。

"""
WeeklyPay.py helps calculate the gross pay of multiple employees based on the hours they worked and their hourly rate
"""

    def main():
        """
        call other modules to get the work done
        :return: None
        """
        total_gross_pay = 0
        total_hours_worked = 0
        employee_quantity = 0

        company_employee = input("Did the employee work this week? Y or y for yes: ")
        while company_employee == "y" or "Y":
            employee_quantity += 1
            name, hours, rate = get_info()
            gross_pay = caculate_gross_pay(hours, rate)
            pay_stub(name, hours, rate, gross_pay)

            total_gross_pay += gross_pay
            total_hours_worked += hours

            company_employee = input("Have more employees worked? Y or y for yes: ")
        show_summary(employee_quantity, total_hours_worked, total_gross_pay)


    def get_info():
        """
        This module gets the needed info from employees
        :return: name, hours_worked, pay_rate
        """
        name = input("Enter the employee's name: ")
        hours_worked = float(input("How many hours did the employee work? "))
        pay_rate = float(input("What is the employee's hourly pay rate? "))
        return name, hours_worked, pay_rate


    def caculate_gross_pay(hours_worked, pay_rate):
        """
        Hours above 40 will be paid time and a half (1.5)
        :param hours_worked:
        :param pay_rate:
        :return: gross_pay
        """
        if hours_worked > 40:
            gross_pay = 40 * pay_rate + (hours_worked - 40) * 1.5 * pay_rate
        else:
            gross_pay = pay_rate * hours_worked

            return gross_pay

    def pay_stub(name, hours_worked, pay_rate, gross_pay):
        """
        This module prints the pay_stub
        :param name:
        :param hours_worked:
        :param pay_rate:
        :param gross_pay:
        """
        print("Employee Name: " + name)
        print("Hours Worked: %.2f"%hours_worked)
        print("Hourly Pay Rate: $%.2f"%pay_rate)
        print("Gross Pay: $%.2f"%gross_pay)


    def show_summary(employee_quantity, total_hours, total_pay):
        """
        :param employee_quantity:
        :param total_hours:
        :param total_pay:
        :return: None
        """
        print("Pay Summary ")
        print("Total number of employees: {}".format(employee_quantity))
        print("Total hours worked by all employees {}".format(total_hours))
        print("Total pay given to employees ${:,.2f}".format(total_pay))

   main()

知道我做错了什么吗?我收到这些错误:

Traceback (most recent call last):
  File "C:/Users/inge scool/PycharmProjects/Week1/WeeklyPay.py", line 80, in <module>
    main()
  File "C:/Users/inge scool/PycharmProjects/Week1/WeeklyPay.py", line 20, in main
    pay_stub(name, hours, rate, gross_pay)
  File "C:/Users/inge scool/PycharmProjects/Week1/WeeklyPay.py", line 65, in pay_stub
    print("Gross Pay: $%.2f"%gross_pay)
TypeError: must be real number, not NoneType

我尝试过在线搜索并不断使用 NoneType 获取有关整数的信息,但它并不完全适用于我正在做的事情。我要加数字吗?

【问题讨论】:

  • @Carcigenicate 现在我正在循环询问员工信息
  • pay_stub 方法中尝试print() 输出gross_pay,看看它会输出什么。这是开始调试并弄清楚发生了什么的好方法。

标签: python-3.x typeerror nonetype


【解决方案1】:
   def caculate_gross_pay(hours_worked, pay_rate):
        """
        Hours above 40 will be paid time and a half (1.5)
        :param hours_worked:
        :param pay_rate:
        :return: gross_pay
        """
        if hours_worked > 40:
            gross_pay = 40 * pay_rate + (hours_worked - 40) * 1.5 * pay_rate
        else:
            gross_pay = pay_rate * hours_worked
        return gross_pay ## remove indent here

在回溯之后,您可以看到 gross_payif/else 内返回,并且仅在运行 else 时返回。只需取消缩进它就可以了。

【讨论】:

  • 给我的评论只有一条,是作者写的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 2018-12-09
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 2018-04-26
  • 2021-03-07
相关资源
最近更新 更多