2012-03-07 7 views
0

2つの異なることをするスクリプトを作成しようとしています。ランダムな単語を選んで行の外に飛び出させます。 "一言で言えば。行内の特定の単語の下にある文字のみを印刷する

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 

上記のコードは実行されますが、テキストを「ジャンプ」させるだけで成功します。誰もこの言葉の列を作成するのに役立つことができますか?

もう一度、助けていただければ幸いです。ありがとうございました!

+0

はそれだけでインデックスを見つけるために、どちらの場合も簡単ではないでしょうあなたが実行したい単語の最初の文字のchar配列のアクション?それでは、単に索引に新しい行を印刷して、ランダムな文字を印刷してください。 – Jkh2

+1

この質問は、http://codereview.stackexchange.com/ –

+0

@StevenRumbalskiの方がいいでしょう。私はそこにそれを掲示します。 – spikem

答えて

0
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 
関連する問題