我已經遇到了麻煩特定打印在Python 3.4 輸入:打印Python代碼多次(NO LOOPS0
str=input("Input Here!!!:")
num = len(str)
x = num
print (((str))*x)
,但我在尋找,打印海峽x倍的輸出,而不。使用循環
例如,如果我輸入:
Input Here!!!: Hello
我會得到:
>>>Hello
>>>Hello
>>>Hello
>>>Hello
>>>Hello
我已經遇到了麻煩特定打印在Python 3.4 輸入:打印Python代碼多次(NO LOOPS0
str=input("Input Here!!!:")
num = len(str)
x = num
print (((str))*x)
,但我在尋找,打印海峽x倍的輸出,而不。使用循環
例如,如果我輸入:
Input Here!!!: Hello
我會得到:
>>>Hello
>>>Hello
>>>Hello
>>>Hello
>>>Hello
你需要,如果你想在不同的行輸出添加新行:
In [10]: n = 5
In [11]: s = "hello"
In [12]: print((s+"\n")* n)
hello
hello
hello
hello
hello
這是不可能得到的輸出,就好像每個字符串是一個新的命令的輸出。最接近您的預期輸出將是上面的代碼。
最後還有一個額外的換行符。嘗試使用rstrip來刪除它。 –
@MalikBrahimi,除非他自己補充說,看起來並非如此。 – garg10may
@MalikBrahimi,是'((s +「\ n」)* n).rstrip()'如果這是個問題,將會被移除 –
您不應該使用內置關鍵字,變量名稱的類型。 str
是一個像列表,int等內置類型。下次你會嘗試使用它,會給你錯誤。
防爆 -
>>> str = 'apple'
現在,讓我們嘗試構建編號S作爲字符串的簡單列表。
>>> [ str(i) for i in range(4)]
Traceback (most recent call last):
File "<pyshell#298>", line 1, in <module>
[ str(i) for i in range(4)]
類型錯誤: '海峽' 對象不是可調用
因爲我們已經代替了我們的STR用字符串。它不能被調用。
因此,讓我們使用的',而不是 '海峽'
s=input("Input Here!!!:")
print (s * len(s))
如果你想在不同的線路
print ((s+"\n")* len(s))
可以使用join
與列表理解輸出:
>>> s='string'
>>> print('\n'.join([s for i in range(5)]))
string
string
string
string
string
從技術上講,列表理解是一個'循環'我想,但你還沒有明確表示你的意思是什麼b Y「不使用循環」
您也可以使用字符串在Python格式:
>>> fmt='{0}\n'*5
>>> fmt
'{0}\n{0}\n{0}\n{0}\n{0}\n'
>>> print(fmt.format('hello'))
hello
hello
hello
hello
hello
但(作爲任何使用*n
會),將有一個最後的額外\n
正如Tim在評論中指出的那樣:
>>> print('\n'.join([s]*5))
string
string
string
string
string
問題最好的...
'\ n'.join([s] * 5)''怎麼樣? –
我不知道你到底想要達到什麼目的。我們可以隨時replicate looping
與recursive function
。
def input(s):
print(s)
def pseudo_for(n, my_func, *args):
if n==0:
return
else:
'''
Write function or expression to be repeated
'''
my_func(*args)
pseudo_for(n-1, my_func, *args)
pseudo_for(5, input, "Hello")
不使用變量名的關鍵字。 –
問題是什麼?看起來你已經做到了。 –
你想在單獨的行中得到你好,你給出的輸出是預期的輸出?請包含更多信息。 – garg10may