首页 > 解决方案 > 在向用户询问循环中的新变量之前在打印后添加一个空行

问题描述

我正在做一些简单的 python 练习,目标是询问用户一个单词,并且该单词在循环中被打印了多次。问题是程序在打印用户给定的变量后不打印空行。

我已经尝试了各种 \n 配置,但都没有成功。

我的程序是:

def printer(a,b):
    i=0
    for i in range(0,b):  
        print(a)
while True:
    text=input("Give text: ")
    if text=="stop":
        print("Stopping.") ##This works fine
        break
    else:
        number=int(input("Give number: "))
        printer(text, number)

现在想要的打印是:

Give text: text
Give number: 2
text
text

Give text: text
Give number: 

但它打印它时没有任何空行:

Give text: text
Give number: 2
text
text
Give text: text
Give number: 

正如我之前提到的,我尝试了各种 \n- 和 ""- 配置,但都没有成功。在向用户询问新变量之前,如何让程序在打印末尾添加一个空行。

标签: python

解决方案


您所需要的只是for循环后的打印:

def printer(a,b):
    i=0
    for i in range(0,b):  
        print(a)
    print()
while True:
    text=input("Give text: ")
    if text=="stop":
        print("Stopping.") ##This works fine
        break
    else:
        number=int(input("Give number: "))
        printer(text, number)

推荐阅读