创建一个程序,将随机字符输入到先前创建的列表中,然后能够将它们调出
variables originalText是代表一个字符的数字列表,key是一个让程序知道要输入多少的数字列表
我最初的计划是把原始文本,输入第一个值到一个新的列表中,然后添加x个“stuff”直到它结束。输入“stuff”之后,它将在再次循环之前输入另一个originaltext字符,直到originaltext超出要添加到modifiedText的值
这段代码可以运行,但不一致。我的第一个问题是,所有的原始文本信息都被扔进了modifiedect的末尾,而modifiedect没有让它正确地“放松”(去掉“东西”)
我想这是个数学问题。有什么建议吗
谢谢
def fixStuff(originalText, key, direction):
#0 adds Stuff, 1 removes Stuff
generate = key[4] #= getMod(key,4) #this variable is used for deciding how much Stuff to input.
originalSize = len(originalText)
print(originalSize)
random.randint(32,126) #used to generate random characters.
i = 1 #used for counting
origSpot = 0 #used to track position in original list
modifiedText = []
print(generate)
if direction is 0:
#originalSize = originalSize * generate # the total size of the array after Stuff.
print("inserting Stuff")
while origSpot < originalSize: #whatever this is
#after every originalSize digit, input generate(th) digits
print(origSpot)
modifiedText.insert((i),originalText[origSpot])
x = 0 #used for inserting. It must be reset before this while runs again.
while x <= generate:
modifiedText.insert((i),random.randint(32,126))
x = x + 1
#print(x)
#print(i)
i = i + generate #increment the gradiant so we dump the next load of Stuff after the next real digit
origSpot = origSpot + 1
i = 0
if direction is not 0:
print("remove el Stuffo")
#take every generate(th) digit and put it in a new array.
while origSpot < originalSize: #whatever this is
#after every originalSize digit, input generate(th) digits
print(origSpot)
modifiedText.insert((i),originalText[origSpot])
x = 0 #used for inserting. It must be reset before this while runs again.
while x <= generate:
origSpot = origSpot + 1
x = x + 1
#print(x)
#print(i)
i = i + 1 #increment the gradiant so we dump the next load of Stuff after the next real digit
print("ree!")
return modifiedText
这是你想要的吗
我将工作分为两个功能,因为它们执行两个不同的、不重叠的任务。我使用append和for循环构建了这个列表。你可以用while循环,但是for循环不太容易出错。为了完整起见,以下是while循环版本:
相关问题 更多 >
编程相关推荐