【问题标题】:How To Use A For Loop Range And List to create a List Of Lists To Display Different String Outputs In This Context如何使用 For 循环范围和列表创建列表列表以在此上下文中显示不同的字符串输出
【发布时间】:2021-12-13 00:06:23
【问题描述】:

我正在尝试创建一个需要两个 prmtrs 的函数,num_of_employeeshrly_rate

该列表应包含每个员工的工作小时数和工资。列表的大小应与员工人数相同。它提示用户输入每个员工的工作小时数,计算工资,并创建一个小时数列表和将其添加到列表中的工资。

如果人数少于 40,则按标准支付:工作小时数 * 时薪 如果数字大于 40,则工资为 40 小时以上的小时费率为 1.5。

代码应该显示这个输出: #假设员工人数只有2人,:

insert the number of worked hours: 60
insert the number of worked hours:20
the employee worked for 60 hours and the earned is 700.00$
the employee worked for 20 hours and the earned is 200.00$

但这是我的代码:

def calculate_total_pay_cad(num_employees, hrly_rate_CAD):
    rng = range(0, num_employees)
    for n in rng:
        num_hrs_worked = int(input(('Input the number of worked hours: ')))
    if num_hrs_worked < 40:
        pay = num_hrs_worked * hrly_rate_CAD
    if num_hrs_worked > 40:
        pay = num_hrs_worked * (hrly_rate_CAD * 1.5)
    for n in rng:
        print(f"the employee worked for {num_hrs_worked} and has earned {pay}")

num_employees = int(input(('enter number employees')))
hrly_rate_CAD = int(input(('enter hourly rate: ')))

calculate_total_pay_cad(num_employees,hrly_rate_CAD)

这是我的输出:

 Input the number of worked hours: 50
Input the number of worked hours: 20
the employee worked for 20 and has earned 300
the employee worked for 20 and has earned 300

总结 = 我的代码仅输出我的输入列表中的最后一个插入。我如何让它伴随正确的小时数并为每条线路计算工资?

非常感谢。

【问题讨论】:

    标签: python for-loop input integer range


    【解决方案1】:

    这是因为您的计算是在 for 循环之外 完成的。这意味着它将仅使用最后输入的值进行所有计算。相反,将所有内容移动到循环内,因为您想要输出 per 员工:

    def calculate_total_pay_cad(num_employees, hrly_rate_CAD):
        rng = range(0, num_employees)
        for n in rng:
            num_hrs_worked = int(input(('Input the number of worked hours: ')))
            pay = 0.0
            if num_hrs_worked < 40:
                pay = num_hrs_worked * hrly_rate_CAD
            if num_hrs_worked > 40:
                pay = num_hrs_worked * (hrly_rate_CAD * 1.5)
            print(f"the employee worked for {num_hrs_worked} and has earned {pay}")
    

    根据您的评论,如果您想先输入所有工作时间然后输出工资,您应该使用list。在这里,您将首先提示并计算所有工资,将其保存到列表中,然后循环显示显示工资的列表。

    def calculate_total_pay_cad(num_employees, hrly_rate_CAD):
        rng = range(0, num_employees)
        pay_list = []
        for n in rng:
            num_hrs_worked = int(input(('Input the number of worked hours: ')))
            if num_hrs_worked < 40:
                pay_list.append((num_hrs_worked * hrly_rate_CAD, num_hrs_worked))
            if num_hrs_worked > 40:
                pay_list.append((num_hrs_worked * (hrly_rate_CAD * 1.5), num_hrs_worked))
        for pay, num_hrs_worked in pay_list:
            print(f"the employee worked for {num_hrs_worked} and has earned {pay}")
    

    【讨论】:

    • 有没有办法像第一次显示输出一样按顺序显示所有输入中的更多!e:insert the number of worked hours: 60 insert the number of worked hours:20 the employee worked for 60 hours and the earned is 700.00$ the employee worked for 20 hours and the earned is 200.00$
    • @jump 在这种情况下,您应该使用list。请参阅我的编辑以获取更多信息。
    猜你喜欢
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多