2012-09-02 38 views
-3

我在寫一個讀取三行輸入的程序。第一行是一個單詞,然後是一些重複的字符,然後是重複的重複。但不幸的是,我無法做到這一點,任何人都可以通過查看我的代碼來指導我。謝謝python重複錯誤

word = raw_input("Enter the word: ") 
length = int(raw_input("Enter the repeat length: ")) 
count = int(raw_input("Enter the repeat count: ")) 
print word.repeat() * count 
I want this sort of output: 
Enter the word: banana 
Enter the repeat length: 2 
Enter the repeat count: 3 
banananana 
+4

你s總是給出一個你得到的輸出(或錯誤)的例子,並描述它與你想要的不同。在你的例子中,你甚至不使用「長度」變量。你期望它做什麼? – BrenBarn

回答

1

恐怕你做錯了什麼。
我想你得到了以下錯誤:

AttributeError: 'str' object has no attribute 'repeat'

由於是在Python str沒有repeat()方法。

我猜你可能想這一點:

# gives the first `length` characters in `string` to repeat for `count` times 
word[:length] * count 

編輯:

我看。您的編輯似乎是說要重複word最後length ..

然後試試word + word[-length:] * (count - 1)

+0

是的,這就是我想要的感謝 – jaddy123

+0

沒有它不工作的pengyu! – jaddy123

+0

@ jaddy123我編輯了這篇文章。再次檢查。 – starrify