1
我正在編寫一些代碼來玩Hangman(Python 3.5.2)。我不想讓自己的代碼永遠運行,例如while 1 < 2:
,但是我開始在沒有while
的情況下正常運行的語句上出現語法錯誤。這裏是我的代碼:將while循環添加到腳本會導致輸入錯誤
with open('dictionary.txt') as f:
words = f.read().splitlines()
alphabet = 'abcdefghijklmnopqrstuvwxyz'
words2 = ''
alphabetCount = []
guesses = []
input = input('Input: ')
for x in range(0, len(words)):
valid = True
if len(input) == len(words[x]):
for y in range(-1, len(input)-1):
if input[y] != words[x][y] and input[y] != '_':
valid = False
if valid:
words2 = words2 + (words[x])
for x in range(0, 26):
alphabetCount.append(0)
for x in range(0, len(words2)):
alphabetCount[alphabet.index(words2[x])] = alphabetCount[alphabet.index(words2[x])] + 1
for z in range(0, 26):
if max(alphabetCount) != 0 and (alphabet[alphabetCount.index(max(alphabetCount))]) not in input:
guesses.append(alphabet[alphabetCount.index(max(alphabetCount))])
alphabetCount[alphabetCount.index(max(alphabetCount))] = 0
print (guesses)
從本質上講,我想循環是這樣的:
while 1 < 2:
with open('dictionary.txt') as f:
words = f.read().splitlines()
alphabet = 'abcdefghijklmnopqrstuvwxyz'
words2 = ''
alphabetCount = []
guesses = []
input = input('Input: ')
for x in range(0, len(words)):
valid = True
if len(input) == len(words[x]):
for y in range(-1, len(input)-1):
if input[y] != words[x][y] and input[y] != '_':
valid = False
if valid:
words2 = words2 + (words[x])
for x in range(0, 26):
alphabetCount.append(0)
for x in range(0, len(words2)):
alphabetCount[alphabet.index(words2[x])] = alphabetCount[alphabet.index(words2[x])] + 1
for z in range(0, 26):
if max(alphabetCount) != 0 and (alphabet[alphabetCount.index(max(alphabetCount))]) not in input:
guesses.append(alphabet[alphabetCount.index(max(alphabetCount))])
alphabetCount[alphabetCount.index(max(alphabetCount))] = 0
print (guesses)
無法重現。沒有語法錯誤。 –
您是否驗證過您沒有混合製表符和空格? –