Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在Python中遇到以下错误“列表索引必须是整数或片,而不是非非类型”_Python - Fatal编程技术网

我在Python中遇到以下错误“列表索引必须是整数或片,而不是非非类型”

我在Python中遇到以下错误“列表索引必须是整数或片,而不是非非类型”,python,Python,根据您的错误消息类型error,在代码用户输入get As string中执行以下步骤,您必须将其转换为整数 else使用int return选项删除return def display_game(games_list): print('Here is the current list') print(games_list) display_game(game_list) def position_choice(): global choice choice=

根据您的错误消息类型error,在代码用户输入get As string中执行以下步骤,您必须将其转换为整数

else使用int return选项删除return

def display_game(games_list):
    print('Here is the current list')
    print(games_list)

display_game(game_list)

def position_choice():
    global choice
    choice= 'Wrong'
      
    while choice not in ['0','1','2']:
        choice = input("Pick a position (0,1,2): ")
        
        if choice not in ['0','1','2']:
            print("sorry, invalid choice! ")
            
            return int(choice)
                        
position_choice()


def replacement_choice(game_list,position):
    user_placement= input('type a string to place in position: ')

    
    game_list[position]= user_placement
    
    return game_list

replacement_choice(game_list,1)


def gameon_choice():
    global choice
    choice= 'Wrong'
      
    while choice not in ['y', '']:
        choice = input("Please pick the option of (Y or N): ")
        
        if choice not in ['y','n']:
            print("sorry, I dont understand. Plese pick Y or N ")
            
        if choice== 'y':
            return True
        else:
            return False
            
                        

gameon_choice()

# here is where I encounter the problem, putting it all together.

game_on= True
game_list= [0,1,2]

while game_on: 
    display_game(game_list)
    
    position = position_choice()
    
    game_list = replacement_choice(game_list,position)
    
    display_game(game_list)
    
    game_on= gameon_choice()
更改为您的代码

choice=int(input("enter input"))
#chech the varible input type python create automatics data type 
print(type(choice))
print("choice =>",choice) 

查看位置选择是否返回无。。如果选择不在['0'、'1'、'2']范围内,则将返回移动到if选择之外

您认为在行选择=输入后,选择位置0,1,2:不会对if条件产生任何影响吗?如果选择不在['0'、'1'、'2']:???解决了问题。然而如果选择一个数字,例如1,则该数字在符合以下行中设置的参数的范围内如果选项不在['0'、'1'、'2']:打印抱歉,选项无效!然而,我仍然会收到这样一条信息:无论何时,当类型的数字不是0、1或2时,我都可以选择I。我不明白为什么在不考虑0.1或2的预设选项的情况下,它实际上会接受任何整数。我对编码和python是新手,你能给我举个例子说明你的意思吗

def position_choice():
    global choice
    choice= 'Wrong'
      
    while choice not in ['0','1','2']:
        #int function use for auto cast value in int
        choice = int(input("Pick a position (0,1,2): "))
        
        if choice not in ['0','1','2']:
            print("sorry, invalid choice! ")
            
            return int(choice)