我希望单词 car 被写的次数与单词 hello 的字母数一样多。
word = str(input('Enter something to secure: ')) #answer: car
password = str(input('Enter password: ')) #answer: hello
y = []
for character in word:
while True:
i = character
y.append(i)
if len(y) == len(password):
break
print(y)
我要打印这个:
['c','a','r','c','a']
不幸的是,这将被打印出来:
['c','c','c','c','c']
请您参考如下方法:
使用itertools.cycle
例如:
from itertools import cycle
word = str(input('Enter something to secure: ')) #answer: car
password = str(input('Enter password: ')) #answer: hello
y = []
word = cycle(word)
for character in password:
y.append(word.next())
print(y)
或
y = [word.next() for character in password]
