Python If/else混淆

Python If/else混淆,python,if-statement,Python,If Statement,所以我的游戏有问题,我在这里复制粘贴了一部分。如您所见,只有当列表位置的前x个元素小于7,并且如果所有其他元素都大于7,并且最后一个元素为0时,此代码才会打印Yeah。但是,正如您在示例中看到的,0不是列表中的最后一个元素,但是我得到了打印结果。为什么?谢谢大家! position=[3,6,4,2,5,0,10,12,7,8] where=1 a=1 for i in range(6-where): if position[i]<7 and position[i]!=0:

所以我的游戏有问题,我在这里复制粘贴了一部分。如您所见,只有当列表位置的前x个元素小于7,并且如果所有其他元素都大于7,并且最后一个元素为0时,此代码才会打印Yeah。但是,正如您在示例中看到的,0不是列表中的最后一个元素,但是我得到了打印结果。为什么?谢谢大家!

position=[3,6,4,2,5,0,10,12,7,8]
where=1
a=1
for i in range(6-where):
    if position[i]<7 and position[i]!=0:
        pass
    else:
        a=0
print(a)
for i in range(6-where,-1):
    if position[i]>6 and position[-1]==0:
        pass
    else:
        a=0
print(a)
print(position[-1])
if a==1:
    print("Yeah")
position=[3,6,4,2,5,0,10,12,7,8]
其中=1
a=1
对于范围内的i(6-其中):
如果位置[i]6和位置[-1]==0:
通过
其他:
a=0
印刷品(a)
打印(位置[-1])
如果a==1:
打印(“是”)

在第一个if语句中,您的位置是[i],而不是位置[-1]

下面是一些改进的、更简单的代码:

position=[3,6,4,2,5,0,10,12,7,8]
x = 5
valid_list = True
for i in range(x):
    if position[i] >= 7 or position[i] == 0:
        valid_list = False

for i in range(len(position) - x - 1):
    if position[x + i] < 7 or position[i] == 0:
        valid_list = False
        
if valid_list and position[-1] == 0:
    print('Yeah')
position=[3,6,4,2,5,0,10,12,7,8]
x=5
有效列表=真
对于范围(x)内的i:
如果位置[i]>=7或位置[i]==0:
有效列表=错误
对于范围内的i(透镜(位置)-x-1):
如果位置[x+i]<7或位置[i]==0:
有效列表=错误
如果有效的_列表和位置[-1]==0:
打印(“是的”)

您的代码看起来有点纠结。首先,为
a
使用一个布尔值和一个适当的名称。例如
listValid=True
。但如果没有它,这也是可能的

position=[3,6,4,2,5,0,10,12,7,8]
splitIndex = 6 - 1
if all([value < 7 for value in position[:splitIndex]]):
     if all([value > 6 for value in position[splitIndex:-1]]):
          if position[-1] == 0:
               print("Yeah")
position=[3,6,4,2,5,0,10,12,7,8]
splitIndex=6-1
如果全部([位置[:splitIndex]]中的值小于7]:
如果全部([splitIndex:-1]]位置的值为[value>6]):
如果位置[-1]==0:
打印(“是”)

您的代码中可能有两个错误:
首先,正如
第一个if语句中的行必须为
如果位置[i]您可以选择将其放入函数中吗?如果是这样,那么提前退出是你的朋友:

def is_winner(nums, middle):
    # The last number must be a zero
    if nums[-1] != 0:
        return False

    # All of the starting numbers must be less than 7
    if not all(num < 7 for num in nums[:middle]):
        return False

    # All of the ending numbers must be at least 7
    if not all(num >= 7 for num in nums[middle:-1]):
        return False

    # If all of those are OK, then we've succeeded
    return True


# This will print False because it doesn't end in 0.
position = [3, 6, 4, 2, 5, 0, 10, 12, 7, 8]
print(is_winner(position, 6))

# This will print True because it meets the requirements.
position = [3, 6, 4, 2, 5, 0, 10, 12, 7, 8, 0]
print(is_winner(position, 6))

# This will print False because a number in the first part is greater than 7
position = [3, 6, 4, 2, 5, 0, 10, 12, 7, 8, 0]
print(is_winner(position, 7))

# This will print False because a number in the second part is not at least 7
position = [3, 6, 4, 2, 5, 0, 10, 12, 7, 8, 0]
print(is_winner(position, 5))
def是优胜者(nums,中间):
#最后一个数字必须是零
如果nums[-1]!=0:
返回错误
#所有起始数字必须小于7
如果不是全部(num<7表示num中的num[:middle]):
返回错误
#所有结束数字必须至少为7
如果不是全部(num>=7表示nums中的num[中间:-1]):
返回错误
#如果所有这些都正常,那么我们已经成功了
返回真值
#这将打印False,因为它不以0结尾。
位置=[3,6,4,2,5,0,10,12,7,8]
打印(是冠军(位置6))
#这将打印为True,因为它满足要求。
位置=[3,6,4,2,5,0,10,12,7,8,0]
打印(是冠军(位置6))
#这将打印为False,因为第一部分中的数字大于7
位置=[3,6,4,2,5,0,10,12,7,8,0]
打印(是否为优胜者(位置7))
#这将打印False,因为第二部分中的数字至少不是7
位置=[3,6,4,2,5,0,10,12,7,8,0]
打印(是否为优胜者(位置5))
看看函数如何变得非常简单和可读?在每一步中,如果不满足要求,则停止。你不必跟踪状态或任何东西;你只需返回False。如果你达到了函数的末尾,并且没有任何测试失败,那么ta da!你是成功的,可以返回真实的


顺便说一句,根据您的示例,第二个要求应该是
x>=7
,而不是
x>7
。如果不正确,请更新代码和示例以匹配。

应该注意的是
范围(6-where,-1)
是一个空范围。如果要倒计时,需要
范围(6-where,-1,-1)