从文件逻辑错误 Python 中读取不准确



嗨,我有以下文本文件,我正在使用csv阅读器:

number,obstacle,location,message
1,gobacktostart,8,Sorry but you've landed on a dangerous number, back to the start for you!
2,movetosquare42,matrix[1][0],The meaning of life is 42 or so they say, and that's where you're headed

我希望(在最后(从以 1,gobacktostart,8....等开头的行中检索数字 8。

我的代码是:

def gobacktostart():
    with open("obstacles.txt","r") as f:
        idnumber="1"
        fReader=csv.reader(f)
        for row in fReader:
            for field in row:
                if field==idnumber:
                    print(row[3])
                    player1position==row[2]
                    print(player1position)

然而,不需要的输出是:

>>> 
Sorry but you've landed on a dangerous number
1
>>> 

我确实需要将值读取到变量 player1position 中,以便将其传递给程序不同部分的另一个函数。

关于解决这个逻辑错误的任何想法?为什么它打印"1",而行[2]指的是8。此外,Row[3] 似乎在上一行中正确执行。

您正在检查相等而不是分配player position == row[2]

最新更新