[英]How do I remove brackets from a printed list?
Context: user inputs employee name, hours worked, and hourly wage.上下文:用户输入员工姓名、工作时间和小时工资。 Output is expected to be the entered names, and their total pay.
Output 应该是输入的名字,以及他们的总工资。 EX: "John $300".
例如:“约翰 300 美元”。 Problem is with what I have it just outputs ('john', 300.0)
问题是我所拥有的只是输出 ('john', 300.0)
payroll = 0
employee_list = []
print('Enter Information ( Name, hours worked, hourly rate) seperated by commas')
print('Press Enter to stop Enetering Employee Information')
info = input('Enter Employee Information: ')
while info != '':
employee_info = info.split(',')
hours_worked = float(employee_info[1])
hourly_rate = float(employee_info[2])
employee_name = employee_info[0].strip()
employee_info = [employee_name, (hours_worked * hourly_rate)]
employee_list.append(employee_info)
payroll += hours_worked * hourly_rate
info = input('Enter Employee Information: ')
print()
print('Total Payroll ${:.2f}'.format(payroll))
print()
print('Employees with Paychecks')
for i in range(len(employee_list)):
print(employee_list[i])
As others already pointed out, the best option (and most modern approach) is to use a f-string.正如其他人已经指出的那样,最好的选择(也是最现代的方法)是使用 f 弦。 f-strings were introduced with Python 3.6.
f 弦是在 Python 3.6 中引入的。
for i in range(len(employee_list)):
print(f"{employee_list[i][0]}: ${employee_list[i][1]}")
I would like to point out something else but off-topic.我想指出一些题外话以外的事情。 Avoid mimicking count-controlled loops if you can use collection-controlled loops.
如果可以使用集合控制循环,请避免模仿计数控制循环。 This makes your code more readable:
这使您的代码更具可读性:
for employee_info in employee_list:
print(f"{employee_info[0]}: ${employee_info[1]}")
from collections import namedtuple
EmployeeInfo = namedtuple("EmployeeInfo", ["name", "total"])
def parse_input(info: str) -> EmployeeInfo:
name, hours, rate = info.split(",")
return EmployeeInfo(name.strip(), float(hours)*float(rate))
employee_info_list = []
print("Enter Information (name, hours worked, hourly rate) separated by commas")
print("Press Enter to stop Entering Employee Information")
while True:
employee_input = input("Enter Employee Information: ")
if not employee_input:
break
employee_info = parse_input(employee_input)
employee_info_list.append(employee_info)
payroll = sum(employee_info.total for employee_info in employee_info_list)
print()
print(f"Total Payroll: ${payroll:.2f}")
print()
print("Employees with Paychecks:")
for employee_info in employee_info_list:
print(f"{employee_info.name}: ${employee_info.total}")
print('{} ${}'.format(*employee_list[i]))
It sounds like you are getting a tuple as an output. You can use an f string to format that variable by calling each item inside curly brackets with a string prefaced by 'f'.听起来您得到的元组为 output。您可以使用 f 字符串来格式化该变量,方法是调用大括号内的每个项目,并使用以“f”开头的字符串。 For example:
例如:
tuple = ('text', 100)
print(f'{tuple[0]} ${tuple[1]}')
output: text $100
You can read more in the python documentation on input output, or pep-498: https://peps.python.org/pep-0498/您可以在输入 output 或 pep-498 的 python 文档中阅读更多信息: https://peps.python.org/pep-0498/
f-strings are your friend: f-strings 是你的朋友:
payroll = 0
employee_list = []
print('Enter Information ( Name, hours worked, hourly rate) seperated by commas')
print('Press Enter to stop Enterting Employee Information')
info = 'Billy, 1234.3, 42' # input('Enter Employee Information: ')
while info != '':
process = lambda x, y, z: [x.strip(), float(y), float(z)]
employee_name, hours_worked, hourly_rate = process(*info.split(','))
employee_list.append(f'{employee_name}, ${hours_worked * hourly_rate:.2f}')
payroll += hours_worked * hourly_rate
info = '' # input('Enter Employee Information: ')
print()
print(f'Total Payroll ${payroll:.2f}')
print()
print('Employees with Paychecks')
for i in range(len(employee_list)):
print(employee_list[i])
Output: Output:
Enter Information ( Name, hours worked, hourly rate) seperated by commas
Press Enter to stop Enterting Employee Information
Total Payroll $51840.60
Employees with Paychecks
Billy, $51840.60
in your last lines:在你的最后一行:
for i in range(len(employee_list)):
print(*employee_list[i], sep=",")
* open a list or tuple as print arguments *打开列表或元组打印 arguments
by sep argument, set how seperate values from each other in console通过sep参数,设置在控制台中如何将值彼此分开
Print with expression用表情打印
for i in range(len(employee_list)):
print(F"{employee_list[i][0]}, {employee_list[i][1]}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.