在运行到26的第二个for循环中,每次从列表中删除一个项目时,我都希望它被设置回零,这样我就可以从一开始检查list1中所有字母表的集合中是否存在字母表。我尝试在for循环中的if语句中添加I=0,但它返回到其范围值,并且不存在重新设置为零。你知道吗
import string
def getAvailableLetters(lettersGuessed):
str1 = string.ascii_lowercase
list1 = [ ]
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
str3 = ""
str4 = str3.join(lettersGuessed)
for j in range(len(str4)):
for i in range(26):
if list1[i] == str4[i]:
list1.remove(list1[i])
lettersGuessed = ['e', 'i', 'k', 'p', 'r', 's']
getAvailableLetters(lettersGuessed)
请尝试以下操作:
你可以试试
while loop
,像这样:这样,您就可以实际地将循环中的
i
和continue
的值从零更改为零。你知道吗Python中的^{} loops 有一个鲜为人知的
else
表达式,可以用来确定整个循环是否完成。这对于重新启动循环很有用。你知道吗相关问题 更多 >
编程相关推荐