【发布时间】:2016-04-22 08:04:55
【问题描述】:
我尝试从输入中编写一个简单的字谜列表生成器。但是在我重写代码后,它只给了我 2 个输出。这是代码
import random
item=input("Name? ")
a=''
b=''
oo=0
while oo<=(len(item)*len(item)):
a=''.join([str(y) for y in random.sample(item, len(item))]) #this line was found on this site
b=''.join([str(w) for w in random.sample(item, len(item))]) #because in no way i had success in doing it by myself
j=[]
j.append(a) #During the loop it should add the anagrams generated
j.append(b) #everytime the loop repeats itself
oo=oo+1
j=list(set(j)) #To cancel duplicates
h=len(j)
f=0
while f<=(h-1):
print(j[f])
但它给出的输出只是一个永远重复的字谜。
【问题讨论】:
-
这是因为在 while 循环中你永远不会增加
f,所以你总是打印j[0]。另外,为什么不遍历for i in j: print(i)之类的项目? -
我真的是编程新手,我只知道基础知识@reti43
标签: python loops random anagram