简体   繁体   English

通过用户输入打印出元组中每次不同的输出

[英]Print out a output that is different each time in a tuple via a user input

I have a piece of code that asks a person to input the hours worked for 6 different workers, each time asking them to import the hours worked.我有一段代码要求一个人输入 6 个不同工人的工作时间,每次都要求他们输入工作时间。 It then asks for the amount of pay per hour.然后它会询问每小时的工资金额。

It should then print out the gross pay for each worker.然后它应该打印出每个工人的总工资。 However it only prints out the last input that has been entered so whatever input for worker 6 gets times and then printed 6 times.然而,它只打印出已输入的最后一个输入,因此工人 6 的任何输入都会获取时间,然后打印 6 次。

Is there anyway to do this and have the number change each time and print the correct amount for each worker without the need to print out 6 different outcomes with a var each?无论如何要这样做并每次更改数字并为每个工人打印正确的数量,而无需打印出 6 个不同的结果,每个结果都有一个 var?

Here is the code这是代码

my_list = [
    ("employee 1"),
    ("employee 2"),
    ("employee 3"),
    ("employee 4"),
    ("employee 5"),
    ("employee 6"),
]
my_list_money = []


for (employee) in my_list:
    user = int(input("Enter the hours worked by {}.".format(employee)))
    
user_ = int(input("Enter the hourly pay rate: "))
total = (user * user_)


for (employee) in my_list:
    print("Gross pay for {}.".format(employee),total)

and this is my output :这是我的输出:

Enter the hours worked by employee 1.7
Enter the hours worked by employee 2.8
Enter the hours worked by employee 3.7
Enter the hours worked by employee 4.6
Enter the hours worked by employee 5.6
Enter the hours worked by employee 6.9
Enter the hourly pay rate: 8
Gross pay for employee 1. 72
Gross pay for employee 2. 72
Gross pay for employee 3. 72
Gross pay for employee 4. 72
Gross pay for employee 5. 72
Gross pay for employee 6. 72

You defined a my_money_list , but you never used it.您定义了一个my_money_list ,但您从未使用过它。

Here is how:方法如下:

my_list = [
    ("employee 1"),
    ("employee 2"),
    ("employee 3"),
    ("employee 4"),
    ("employee 5"),
    ("employee 6"),
]

my_list_money = []

for (employee) in my_list:
    user = int(input(f"Enter the hours worked by {employee}. "))
    my_list_money.append(user)
    
user_ = int(input("Enter the hourly pay rate: "))

for employee, user in zip(my_list, my_list_money):
    total = user * user_
    print(f"Gross pay for {employee}: {total}")

You can even do this with list comprehensions :你甚至可以用列表推导来做到这一点:

my_list = [
    ("employee 1"),
    ("employee 2"),
    ("employee 3"),
    ("employee 4"),
    ("employee 5"),
    ("employee 6"),
]

my_list_money = [int(input(f"Enter the hours worked by {employee}. ")) for employee in my_list]
user_ = int(input("Enter the hourly pay rate: "))

print('\n'.join([f"Gross pay for {employee}: {user * user_}" for employee, user in zip(my_list, my_list_money)]))

Input:输入:

Enter the hours worked by employee 1. 5
Enter the hours worked by employee 2. 2
Enter the hours worked by employee 3. 6
Enter the hours worked by employee 4. 3
Enter the hours worked by employee 5. 1
Enter the hours worked by employee 6. 3

Output:输出:

Enter the hourly pay rate: 23
Gross pay for employee 1: 115
Gross pay for employee 2: 46
Gross pay for employee 3: 138
Gross pay for employee 4: 69
Gross pay for employee 5: 23
Gross pay for employee 6: 69

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM