将数据附加到 python 中的列表时出现问题

Issue with appending data to a list in python

我目前正在创建一个“刽子手”游戏,但我似乎无法使用追加功能将数据添加到我的列表中,因为它一直出现错误:

**

AttributeError: 'NoneType' object has no attribute 'append'

**

这是我的代码供参考:

import random

name_list = ['jason', 'enrique']
current_ans = []

i = 0
while i < len(name_list[1]):
    current_ans = print("_ ", end=' ')
    i = i + 1

print("\nThis is a", len(name_list[1]), "letter word")

while True:

    if current_ans != name_list[1]:
        print('Enter a letter')
        guess = input()

        if guess in name_list[1]:
            print('Correct!')
            current_ans.append(guess)

            for letter in name_list[1]:

                if guess == letter:
                    print(str(current_ans), end=' ')

                else:
                    print('_ ', end=' ')

        else:
            print('Incorrect!')
    else:
        print('Congratulations!!! You guessed the correct word')
        break


# Cross checks input with name list & Swaps out _ with correct input

通过设置

    current_ans = print("_ ", end=' ')

您正在将 current_ans 从列表更改为 print() return,即 None

print() 没有 return 任何东西

在 while 循环中你有赋值,

current_ans = print("_ ", end=' ')

它returns current_ans = None

发生错误是因为您试图将元素附加到 None 对象。

current_ans.append(guess)