print("%-10s %-15s $%8.2f %8.2f %10.2f" % (first_name[i], last_name[i],
pay_rate[i], hours[i], calculate_pay(hours[i], pay_rate[i])))
如果已经回答了,请保持温和。我找不到它并且是 Python 和编程的新手。
我试图在数字( float )旁边得到一个美元符号。这是在填充内,而不是在“$”和数字之间有一个空格。
期望的输出:
Please enter your first name(leave blank to quit): daniel
Please enter your last name: johnson
Please enter your hourly pay: 15
Please enter your hours: 40
Please enter your first name(leave blank to quit):
John Smith $10.00 40.00 $400.00
Daniel Johnson $15.00 40.00 $600.00
请您参考如下方法:
我只想做一个辅助函数。还建议使用 format ,而不是使用 % 的旧式 python 格式:
def my_format(fn, ln, pr, h, cp):
return "{:10s} {:>10s} {:>8s} {:8>.2f} {:<8s}".format(
fn, ln, '$'+'{:0.2f}'.format(pr), h, '$'+'{:0.2f}'.format(cp))
print(my_format('John', 'Smith', 10, 40, 400.00))
print(my_format('Daniel', 'Johnson', 15, 50, 60))
结果:
John Smith $10.00 40.00 $400.00
Daniel Johnson $15.00 50.00 $60.00
您可以根据需要调整格式数字和格式。
