在Python中用整数打印注释

2024-04-27 03:10:43 发布

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

def triangular(n):
    tri = 0
    for i in range(1, n+1):
        tri = tri + i

    print ("The answer is equal to" +(tri))

triangular(4)

我只是需要帮助打印报表,因为它不起作用。我想把答案打印出来


Tags: theto答案answerinfor报表is
2条回答
print("The answer is equal to", tri)

或者

print("The answer is equal to %i"%tri)

或者

print("The answer is equal to {}".format(tri))

docs还有更多的方法可以做到这一点。你知道吗

你需要把你的int转换成str。你知道吗

# str is optional here because print will call str on its arguments for you
print("The answer is equal to", str(i)) 

或者

# str is not optional here because you are concatenating
print("The answer is equal to " + str(i))

相关问题 更多 >