你需要的是一種方法來一遍又一遍地讓每個字符您的manipulateinput
串的,所以你不要跑出來的字符。
mystring = 'string'
assert 2 * mystring == 'stringstring'
但有多少次重複:
您可以因此被重複多次的字符串倍增,因爲你需要做到這一點?那麼,你得到使用len
一個字符串的長度:
assert len(mystring) == 6
因此,要確保您的字符串是至少只要另一個字符串,你可以這樣做:
import math.ceil # the ceiling function
timestorepeat = ceil(len(originalinput)/len(manipulateinput))
newmanipulateinput = timestorepeat * manipulateinput
另一種方式不要將它用INT除法,或//
:
timestorepeat = len(originalinput)//len(manipulateinput) + 1
newmanipulateinput = timestorepeat * manipulateinput
現在你可以使用一個for循環不會耗盡字符:
result = '' # start your result with an empty string
for character in newmanipulateinput:
# test to see if you've reached the target length yet
if len(result) == len(originalinput):
break
# update your result with the next character
result += character
# note you can concatenate strings in python with a + operator
print(result)
問題再次出現了什麼?... – 2015-01-26 18:18:21
這是一個家庭作業問題嗎?可以的,如果是的話,但是幫助建議你這樣說在你的問題前面,它可以讓人們更有效地幫助你,另外:試着至少包含一些你試過的代碼,以便對它進行破解 – 2015-01-26 18:18:29
這是我給出的問題:「keywor d重複 足夠的時間以匹配明文消息的長度。「。我可以將其餘的程序添加到我已經擁有的代碼段中,這只是我完成了一些作業:) – KBHO 2015-01-26 18:22:24