繁体   English   中英

如何在同一行打印这些字符?

[英]How do I print these characters on the same line?

我如何让这个函数在同一行重复用户给出的相同字符? 相反,它在每一行上单独打印。

character_to_enter = input('What character would you like to repeat?: ')
character_count = input("How many " + character_to_enter + "'s would you like to enter?: ")


def repeater(character_count):
    for repeats in range(int(character_count)):
        print(character_to_enter)


repeater(character_count)

输出看起来像上面的当前代码

What character would you like to repeat?: f
How many f's would you like to enter?: 2
f
f

Process finished with exit code 0

我需要它看起来像这样

What character would you like to repeat?: f
How many f's would you like to enter?: 4
ffff

Process finished with exit code 0

实现这一点的最简单和最简单的方法之一是使用 print 的“结束”参数。 像这样:

def repeater(character_count):
    for repeats in range(int(character_count)):
        print(character_to_enter, end="")


repeater(character_count)

这种方式不是用默认的换行符结束每次打印,而是不添加任何字符。

暂无
暂无

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

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