首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python不会计算特定的“if”条件

Python不会计算特定的“if”条件
EN

Stack Overflow用户
提问于 2022-05-30 07:18:52
回答 1查看 55关注 0票数 -3

在我的源代码中有3 'if‘语句。假设我在第一个循环中输入'no‘,Python只计算第二个'if’条件,而不是第三个'if‘。如果我改变了第二和第三条if语句的顺序,就会有区别。

我该怎么做才能得到第三个“如果”语句来评估呢?下面是我的代码:

代码语言:javascript
复制
grid = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]

def fill_grid(grid,pos,player):
    grid[pos[0]][pos[1]]=player
    print_grid(grid)

def print_grid(grid):
    for row in grid:
        for row_item in row:
            print(f'[{row_item}]',end=' ')
        print()

while True:
    continue_filling = input('Want to fill?')
    if (continue_filling == 'yes'):
        player=input('Which player?')
        row_col = [int(num_str) - 1 for num_str in input('Enter row and column:').split(',')]
        fill_grid(grid, row_col, player)
    elif (continue_filling != 'yes' or continue_filling != 'no'):
        print('Type yes or no!')
    elif (continue_filling == 'no'):
        break

print('Fill ended!')

以下是产出:

想填吗?不

输入是或否!

想填吗?不

输入是或否!

要填吗?

输入是或否!

想填吗?是的

哪个玩家?

EN

回答 1

Stack Overflow用户

发布于 2022-05-30 07:25:04

如果你仔细看第二个If声明,

continue_filling != 'yes' or continue_filling != 'no'

对于第一个条件,值no被计算为true,因为continue_filling不是'yes‘。因此,第三个IF语句永远不会到达。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72430475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档