Печатать только символы ниже определенного слова в строке - PullRequest
0 голосов
/ 08 марта 2012

Я пытаюсь написать скрипт, который делает две разные вещи: он выбирает случайное слово и заставляет его «выпрыгивать» из строки, а также «включает» слово.В свою очередь, я имею в виду, что оно выбирает случайное слово, а затем печатает только то же количество символов из этого слова под ним, и ничего больше, например так:

this is a thing that I am typing
          sdfas
          wertq
          wsvsd
          swefs

Это то, что я имею в виду, делая его "выпрыгнуть "из строки:

          thing
this is a       that I am typing

Моя проблема в том, что я не могу понять, как сказать" напечатать число [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 

Вышеприведенный код запускается, но только в тексте «прыгает».Кто-нибудь может помочь с созданием этого столбца слов?

Опять же, любая помощь, которую вы могли бы оказать, была бы очень признательна.Спасибо!

1 Ответ

0 голосов
/ 08 марта 2012
txt='this is a thing that I am typing'

jumpword='thing'
jumpind=txt.find(jumpword)
newtxt=str(txt)
if jumpind>=0:
    newtxt=newtxt.replace(jumpword," "*len(jumpword),1)
    newtxt=" "*jumpind+jumpword+'\n'+newtxt 
print newtxt

Результат:

          thing
this is a       that I am typing

Слово поворачивается:

import random
def randword(length,sourcelist):
    # get random word: (move this out of the function if you need to keep it the same)
    rword=random.sample(sourcelist,1)[0]
    # get length number of letters from it.
    return "".join(map(lambda dummy:random.sample(rword,1)[0],range(length)))

print txt
# make 5 turns:
for i in range(5):
    print " "*jumpind+randword(len(jumpword),txt.split())

Результат:

this is a thing that I am typing
          IIIII
          aaaaa
          IIIII
          yngtt
          hthti
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...