如何在函数中的变量之间添加“.”?

2024-04-26 10:57:11 发布

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

所以我做了以下函数:

def marble_stats(a):
big_box=a//48
small_box=(a-big_box*48)//8
excess=a-((big_box*48)+(small_box*8))
cash=26*big_box+4*small_box-excess*2
return big_box,small_box,excess,cash

final=marble_stats(503)
print(final)

当我执行时,我得到:(0,0,7,-14)

但我需要的是(0..0..7..-14)被打印出来。我试着把变量转换成字符串并用“.”连接起来,但逗号似乎没有消失。我该怎么办?你知道吗


Tags: 函数字符串boxreturndefstatscashfinal
2条回答

给你一个选择,在我看来,这里有一个更为python的方法:

def marble_stats(a):
    big_box=a//48
    small_box=(a-big_box*48)//8
    excess=a-((big_box*48)+(small_box*8))
    cash=26*big_box+4*small_box-excess*2
    return_list = [big_box,small_box,excess,cash]
    return '..'.join(str(x) for x in return_list)

final=marble_stats(503)
print(final)

输出

10..2..7..254
def marble_stats(a):
    big_box=a//48
    small_box=(a-big_box*48)//8
    excess=a-((big_box*48)+(small_box*8))
    cash=26*big_box+4*small_box-excess*2
    return str(big_box)+'..'+str(small_box)+'..'+str(excess)+'..'+str(cash)

final=marble_stats(503)
print(final)

输出:10..2..7..254

相关问题 更多 >