首页 > 解决方案 > Python Card 游戏有助于简化 if/elif 语句

问题描述

如果您想学习如何玩它,我制作了一个翻牌游戏,请访问 https://www.youtube.com/watch?v=CCxs-tu8tOU

numbers = [0, 1, 0, 0, 1, 1, 0]

options = []

for num in range(0, len(numbers)):
    if numbers[num] == 1:
        options.append(num)

choice = int(input("Here's a list of cards: {}, your options are {}: ".format(numbers, options)))

下面的代码太长了,我需要帮助来简化它

if choice in options:
    numbers[choice] = '.'
    if numbers[choice-1] == 0:
        numbers[choice-1] = 1
    elif numbers[choice-1] == 1:
        numbers[choice-1] = 0
    if numbers[choice+1] == 0:
        numbers[choice+1] = 1
    elif numbers[choice+1] == 1:
        numbers[choice+1] = 0

标签: python-3.x

解决方案


推荐阅读