简体   繁体   English

用整数在Python中打印注释

[英]Printing Comments in Python with integers

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

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

triangular(4)

I just need help with the print statement because it doesn't work. 我只需要有关打印语句的帮助,因为它不起作用。 I am trying to print the answer is equal to tri 我正在尝试打印答案等于三

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

or 要么

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

or 要么

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

The docs also have some more ways to do this. 文档还具有其他一些方法来执行此操作。

You need to cast your int to a str . 您需要将int转换为str

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM