Skip to main content
 首页 » 编程设计

python之如何根据计数器标记输出

2025年05月04日11bonelee

我试图将一个句子打印十次左右(根据 range() 中给出的次数),但我希望第一个句子跳格一次,第二个句子跳格两次,等等……

这是我的代码:

count = 0 
 
for i in range(10): 
    print("\t*countPython is fun") 
    count += 1 

目前我得到如下输出,这不是我想要的:

*countPython is fun 
*countPython is fun 
*countPython is fun 
*countPython is fun 
*countPython is fun 
*countPython is fun 
*countPython is fun 

我知道这是在 print() 函数中完成的事情,但我没弄对。我该如何解决这个问题?

请您参考如下方法:

* count 必须在字符串之外:

for i in range(10): 
    print(("\t"*count) + "Python is fun") 
    count += 1