Context: user inputs employee name, hours worked, and hourly wage. Output is expected to be the entered names, and their total pay. EX: "John $300". Problem is with what I have it just outputs ('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])
CodePudding user response:
print('{} ${}'.format(*employee_list[i]))
CodePudding user response:
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'. 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/
CodePudding user response:
f-strings are your friend:
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
CodePudding user response:
in your last lines:
for i in range(len(employee_list)):
print(*employee_list[i], sep=",")
1
* open a list or tuple as print arguments
2
by sep argument, set how seperate values from each other in console
CodePudding user response:
Print with expression
for i in range(len(employee_list)):
print(F"{employee_list[i][0]}, {employee_list[i][1]}")
CodePudding user response:
As others already pointed out, the best option (and most modern approach) is to use a f-string. f-strings were introduced with 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}")