我有一個程序需要一個字符串輸入和一個整數輸入。 整數決定了2件事情,打印字符串的次數以及字符串中的哪個字符是可見的,否則用「。」代替。Python:根據輸入替換字符串中的字符
例如 - 字符串:你好世界 整數:3
應顯示爲:
H..l..W..l.
.e..o..o..d
..l.. ..r..
基本上打印對角線文本。
我該如何解決字符串中顯示的每個第三個字符,否則用「。」替換?無論輸入的字符串是什麼,都從第一個開始。
我只需要這個函數,我得到了其餘的代碼。
到目前爲止的代碼來是這樣的(不緊湊):
stringVar = input("String: ")
countVar = input("Count: ")
countInt = int(countVar)
stringInt = len(stringVar)
if countInt > stringInt:
print("Number should be between 1 and the lenght of the string!")
exit()
if countInt < 1:
print("Number should be between 1 and the lenght of the string!")
exit()
count = 1
while count <= countInt:
print(stringVar)
count = count + 1
我只是需要一種方法來在STRINGVAR也替換的字符。
你能告訴我們你到目前爲止所嘗試過的嗎? – aestrivex
你有沒有試圖自己寫這個?如果是這樣,你能提供代碼嗎? – Ffisegydd
剛剛添加了代碼。 – Sinfamy