[英]How do I remove brackets from a printed list?
上下文:用戶輸入員工姓名、工作時間和小時工資。 Output 應該是輸入的名字,以及他們的總工資。 例如:“約翰 300 美元”。 問題是我所擁有的只是輸出 ('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])
正如其他人已經指出的那樣,最好的選擇(也是最現代的方法)是使用 f 弦。 f 弦是在 Python 3.6 中引入的。
for i in range(len(employee_list)):
print(f"{employee_list[i][0]}: ${employee_list[i][1]}")
我想指出一些題外話以外的事情。 如果可以使用集合控制循環,請避免模仿計數控制循環。 這使您的代碼更具可讀性:
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]))
聽起來您得到的元組為 output。您可以使用 f 字符串來格式化該變量,方法是調用大括號內的每個項目,並使用以“f”開頭的字符串。 例如:
tuple = ('text', 100)
print(f'{tuple[0]} ${tuple[1]}')
output: text $100
您可以在輸入 output 或 pep-498 的 python 文檔中閱讀更多信息: https://peps.python.org/pep-0498/
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:
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
在你的最后一行:
for i in range(len(employee_list)):
print(*employee_list[i], sep=",")
*打開列表或元組打印 arguments
通過sep參數,設置在控制台中如何將值彼此分開
用表情打印
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.