python重复错误

时间:2012-09-02 02:38:59

标签: python repeat

我正在编写一个读取三行输入的程序。第一行将是一个单词,然后是一些重复的字符,然后是一些重复。但不幸的是,我无法通过查看我的代码来指导我。感谢

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

1 个答案:

答案 0 :(得分:1)

我担心你做错了什么 我想你有以下错误:

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

由于Python中repeat()没有str方法。

我想你可能想要这个:

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

<强>编辑:

我看到..您的编辑似乎表示您要重复length最后 word ..

然后尝试word + word[-length:] * (count - 1)