需要减去并删除前导零

2024-09-25 00:24:56 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的程序中,我需要从变量“b”中减去bartype,然后在一段代码中从2.5中去掉前导零

我试过在代码的不同位置移动减法

b=int(input('Please enter weight to load on the bar:'))
bartype=int(input('Please enter bar weight'))
x = bartype
b = (b-x)
print(b//45, "45's")
b = b%45
print(b//25, "25's")
b = b%25
print(b//10, "10's")
b = b%10
# I need to print 2.5 without leading zeros
print(b/2.5, "2.5's")
b = b%2.5

print(b//5, "5's")
b = b%5

所以这里有一个例子365=365-bartype,然后应该分解权重类型(45、25等)。答案应该是6 45's 2 25's。示例320应该等于6 45's和2 2.5's


Tags: theto代码程序inputonbarload
1条回答
网友
1楼 · 发布于 2024-09-25 00:24:56
b = int(input('Please enter weight to load on the bar :  '))-int(input('Please enter bar weight :  '))
print(int(b//45), "45's") # int() to convert float value to int
b = b%45
print(int(b//25), "25's")
b = b%25
print(int(b//10), "10's")
b = b%10
# I need to print 2.5 without leading zeros
print(int(b/2.5), "2.5's")
b = b%2.5
print(int(b//5), "5's")
b = b%5

相关问题 更多 >