我正在编写一个子手游戏,并在做出正确的猜测后在线找到了一个用字母替换空格的代码,但不幸的是,它会产生“ NoneType”错误 我不确定该如何更正,也无法将在网上找到的其他答案应用到代码中进行修复
word_list = ['around','august','branch','bright','castle','change','across']
tries = 0
word = random.choice(word_list)
print(word)
blank = print ("_ "*len(word))
while tries < 7:
guess = input("Enter a letter: ")
blank = list(blank)
for k in range(0, len(word)) or []:
if guess == word[k]:
blank[k] = guess
print (''.join(blank))
答案 0 :(得分:0)
您要将打印的返回值而不是打印的值存储到blank
尝试一下:
import random
word_list = ['around','august','branch','bright','castle','change','across']
tries = 0
word = random.choice(word_list)
print(word)
blank = "_ "*len(word) #storing what you want in variable
print(blank) #print the variable
while tries < 7:
guess = input("Enter a letter: ")
blank = list(blank) #here you do list() on the variable and not the return of print >> OK
for k in range(0, len(word)) or []:
if guess == word[k]:
blank[k] = guess
print (''.join(blank))