簡體   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