2017-04-15 65 views
0

因此,我的代碼的第一部分所做的就是執行行中的每個單詞,然後再執行14次。我需要它一次垂直執行一行一行。第二部分,我承認我沒有太多進展,它只是打印報價,但我需要它一次垂直打印每個字母。如何在verticaly中一次執行一行一個字並在python中水平執行一行一行字?

Quote = "It was a bright cold day in April and the clocks were striking thirteen." 

for letter in Quote: 
    print Quote[0:2] 
    print Quote[3:6] 
    print Quote[7:8] 
    print Quote[9:15] 
    print Quote[16:20] 
    print Quote[21:24] 
    print Quote[25:27] 
    print Quote[28:33] 
    print Quote[34:37] 
    print Quote[38:41] 
    print Quote[42:48] 
    print Quote[49:53] 
    print Quote[54:62] 
    print Quote[63:71] 

print "-----" 
print Quote 

回答

1

這是因爲for letter in Quote通過Quote每個字符迭代。對於每次迭代,您都手動打印每個字Quote

如果你要打印的Quote每個單詞,那麼你可以做,而不是:

for word in Quote.split(): 
    print word 

現在你可以打印單詞的每個字母:

for word in Quote.split(): 
    print "Word: " + word 
    for letter in word: 
     print letter, 
    print "" 

print letter,後面的逗號。這使得不添加換行符。您也可以使用sys.stdout.write(letter)

1

你應該試試這個

for word in Quote.split(): 
    print word