我试着写一个脚本,它做两件不同的事情:它选择一个随机的单词并使它“跳出”一行,它也“打开”一个单词。反过来,我的意思是,它会随机选取一个单词,然后只从下面的单词中打印相同数量的字符,而不会像这样:
this is a thing that I am typing
sdfas
wertq
wsvsd
swefs
这就是我所说的“跳出”底线的意思:
^{pr2}$我的问题是我不知道怎么说“打印x下的[x]个字符”
下面是我的代码:
import random
import sys
s = open('sonnets.txt')
counting = 0
for line in s:
line.strip()
randomInt = random.randint(1,100)
counting = counting+1
words = line.split()
test = 1
if (randomInt < 2*counting):
if (len(words) > 0):
r = random.choice(words)
if r in line:
ind = line.index(r)
wordChoice = len(r)
print ((" " * (ind))+r).upper()
whiteSpace = wordChoice+2
newLine = line.replace(r, (" " * whiteSpace))
print newLine
turn = random.choice(words)
if turn in line:
turnCheck = True
ind = line.index(turn)
wordChoice = len(turn)
print ((" " * (ind))+turn)
foo = line[ind:ind+4]
print ((" " * (ind))+foo)
else:
print line
上面的代码运行,但只成功地使文本“jump”。有人能帮我创建这一栏吗?在
再次感谢您的帮助。谢谢您!在
结果:
^{pr2}$世界转折:
结果:
相关问题 更多 >
编程相关推荐