如何在我的数组中添加货币符号?
Posted
技术标签:
【中文标题】如何在我的数组中添加货币符号?【英文标题】:How to add a currency symbol to my array? 【发布时间】:2021-12-21 11:23:28 【问题描述】:目标:
用£符号显示我的佣金支付清单。
问题的解释:
在显示结果时,它不会打印整个结果,而是仅打印 £ 符号。
我尝试过的:
正如您在代码中看到的那样,我尝试将数组转换为字符串(将其放入自己的变量中),然后将货币符号 (£) 添加到其中并要求调用佣金 2。但是,它没有显示。
输出:
Name Id Houses Sold Commission
£1000
ki 2 2 £
(1000英镑只是为了表明数据确实存在,但是在列表中打印时由于某种原因没有显示出来……)
所需输出:
Name Id Houses Sold Commission
ki 2 2 £1000
我已经做了好几个小时了,所以任何帮助都将不胜感激!
发生错误的代码:
def print_entered_info(names, ids, num_sold_houses):
print()
row_width = 12
comission_per_house = 500
header = ['Name', 'Id', 'Houses Sold', 'Commission']
print(' '.join(f'h:<row_width' for h in header))
commission = [n * comission_per_house for n in num_sold_houses]
commission2 = commission
commission2 = ''.join(str(e) for e in commission)
commission2= "£" + commission2
print(commission2)
for values in zip(*[names, ids, num_sold_houses, commission2]):
print()
print(' '.join(f'v:<row_width' for v in values))
我的完整代码:
def get_int_input(prompt: str) -> int:
num = -1
while True:
try:
num = int(input(prompt))
break
except ValueError:
print('Error: Enter an integer, try again...')
return num
def get_yes_no_input(prompt: str) -> bool:
allowed_responses = 'y', 'yes', 'n', 'no'
user_input = input(prompt).lower()
while user_input not in allowed_responses:
user_input = input(prompt).lower()
return user_input[0] == 'y'
names = []
ids = []
num_sold_houses= []
def get_single_employee_info(names, ids, num_sold_houses):
names.append(input('What is the employee\'s name?: '))
ids.append(get_int_input('What is the employee\'s id?: '))
num_sold_houses.append(get_int_input('How many houses did the employee sell?: '))
def get_houses_sold_info(names, ids, num_sold_houses):
get_single_employee_info(names, ids, num_sold_houses)
add_another_employee = get_yes_no_input('Add another employee [yes/no]?: ')
while add_another_employee:
get_single_employee_info(names, ids, num_sold_houses)
add_another_employee = get_yes_no_input(
'Add another employee [yes/no]?: ')
def print_entered_info(names, ids, num_sold_houses):
print()
row_width = 12
comission_per_house = 500
header = ['Name', 'Id', 'Houses Sold', 'Commission']
print(' '.join(f'h:<row_width' for h in header))
commission = [n * comission_per_house for n in num_sold_houses]
commission2 = commission
commission2 = ''.join(str(e) for e in commission)
commission2= "£" + commission2
print(commission2)
for values in zip(*[names, ids, num_sold_houses, commission2]):
print()
print(' '.join(f'v:<row_width' for v in values))
print()
total_commission = sum(commission)
print(f'Total Commission: £total_commission.00 (before bonus)')
print()
bonus = max(commission)
if bonus >= max(commission):
bonus = bonus*0.15
bonus = (int(max(commission)) + bonus)
commission = bonus
print("The person at the top of ranking gets: " + "£" + str(commission)+"0")
print()
rankings = sorted(zip(num_sold_houses, names), reverse=True)
print('Ranking:')
for houses_sold, name in rankings:
print(f'name - houses_sold')
def main() -> None:
print('Houses Sold Tracker')
print('===================')
names, ids, num_houses_sold = [], [], []
get_houses_sold_info(names, ids, num_houses_sold)
print_entered_info(names, ids, num_houses_sold)
if __name__ == '__main__':
main()
【问题讨论】:
【参考方案1】:更换
commission2 = ''.join(str(e) for e in commission)
到
commission2 = ["£" + str(e) for e in commission]
并删除它下面的行。您的 ''.join 采用 2 个列表元素并将它们强制转换为单个字符串,这不是您想要的逻辑。您想要一个commission2 变量的列表输出,因此我们创建一个列表并将字符串化的值与货币符号一起附加到它。
输出:
Name Id Houses Sold Commission
Kevin 1 3 £1500
Stacey 2 5 £2500
Total Commission: £4000.00 (before bonus)
The person at the top of ranking gets: £2875.00
Ranking:
Stacey - 5
Kevin - 3
【讨论】:
工作就像一个魅力,感谢您的建议!以上是关于如何在我的数组中添加货币符号?的主要内容,如果未能解决你的问题,请参考以下文章