任务是这样的:
让我们整理一下之前打印的支票。所有行的长度必须为 35 个字符。
有必要做一些类似检查的事情,对齐线条,同时它们不应超过 35 个字符。
我的决定:
product_name = input()
product_price = int(input())
product_weight = int(input())
user_money = int(input())
print('================Чек================'[:35])
print(f"Товар: {product_name: >28}"[:35])
print(f"Цена: {product_weight: >15}кг * {product_price}руб/кг"[:35])
print(f"Итого: {product_weight * product_price: >25}руб"[:35])
print(f"Внесено: {user_money: >23}руб"[:35])
print(f"Сдача: {user_money - (product_weight * product_price): >25}руб"[:35])
print('==================================='[:35])
而且它没有被检查系统接受。明白为什么它不起作用。
在网上找到并接受的解决方案:
name = input()
price = int(input())
total = int(input())
money = int(input())
print("================Чек================")
print(f"Товар:{' ' * (29 - len(name))}{name}")
print(f"Цена:{' ' * (19 - len(str(price)) - len(str(total)))}{total}кг * {price}руб/кг")
print(f"Итого:{' ' * (26 - len(str(total * price)))}{total * price}руб")
print(f"Внесено:{' ' * (24 - len(str(money)))}{money}руб")
print(f"Сдача:{' ' * (26 - len(str(money - total * price)))}{money - total * price}руб")
print("===================================")
看到第二个解决方案,我的身体受到了伤害。我完全不明白数字从何而来,从中减去值的长度 \u200b\u200bis,总的来说一切看起来都很可怕..我的解决方案与从互联网上获取的解决方案有何不同?也许有人能够告诉我一些我的解决方案可能会失败的边缘情况。☹️