Question
Python coding I need this code to format the output for this code to have :.2f in it somewhere the numbers need to output the
Python coding
I need this code to format the output for this code to have :.2f in it somewhere the numbers need to output the numbers in the format of 000.00 but I have no idea how to do that in my code.
my code:
fname = input('Enter the file name: ')
# print the header information
print('%-20s%-20s%-20s'%('Name', 'Hours', 'Total Pay'))
# open file to read
with open(fname, 'r') as f:
# read each line of the file in loop
for line in f:
# split the line into list of words
words = line.split()
# extract name
name = words[0]
# extract hours and convert to int
hours = int(words[1])
# extract wage and convert to int
wage = float(words[2])
# total pay will be hourly wage times no. of hours worked.
total_pay = hours*wage
# print the output lines
print('%-20s%-20s%-20s'%(name, hours, total_pay))
print (' ')
to make this code run you need a data.txt file with this in it.
Lambert 34 10.50 Osborne 22 6.25 Giacometti 5 100.70
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started