检查清单

时间:2014-02-21 10:01:28

标签: python list if-statement

if not bingo_num in player_1_num:
    print(bingo_num, "is not in", player1,"s list")
if not bingo_num in player_2_num:
    print(bingo_num, "is not in", player2,"s list")

我正在制作一个宾果游戏,这就是我的代码。我试过了

if not bingo_num in player_1_num:
    print(bingo_num, "is not in", player1,"s list")

以及

if bingo_num not in player_1_num:
    print(bingo_num, "is not in", player1,"s list")

但输出总是说

挑选的数字为54

在John的名单中找到了54个!

54不在Arjun的名单

54不在John的列表中

我想知道如何解决这个错误

我的整个代码。我只是一个初学者,所以它可能很乱,有些部分可能是不必要的

print("BINGO")
print("You must pick 6 numbers each in the range 1-100")
player_1_num = []
player_2_num = []
import random

player1 = input("Player 1 enter your name")
player2 = input("Player 2 enter your name")
print()
print("Time for ", player1, "to enter their numbers")
p1_num1 = int(input("Enter your first number"))
p1_num2 = int(input("Enter your second number"))
p1_num3 = int(input("Enter your third number"))
p1_num4 = int(input("Enter your fourth number"))
p1_num5 = int(input("Enter your fifth number"))
p1_num6 = int(input("Enter your sixth number"))
print()
print("Time for ", player2, "to enter their numbers")
p2_num1 = int(input("Enter your first number"))
p2_num2 = int(input("Enter your second number"))
p2_num3 = int(input("Enter your third number"))
p2_num4 = int(input("Enter your fourth number"))
p2_num5 = int(input("Enter your fifth number"))
p2_num6 = int(input("Enter your sixth number"))

player_1_num.append(p1_num1)
player_1_num.append(p1_num2)
player_1_num.append(p1_num3)
player_1_num.append(p1_num4)
player_1_num.append(p1_num5)
player_1_num.append(p1_num6)

player_2_num.append(p2_num1)
player_2_num.append(p2_num2)
player_2_num.append(p2_num3)
player_2_num.append(p2_num4)
player_2_num.append(p2_num5)
player_2_num.append(p2_num6)

print()
print()
print(player1, "your numbers are", player_1_num)
print(player2, "your numbers are", player_2_num)


while len(player_1_num) > 0 or len(player_2_num) > 0:
    print()
    input("Press enter to roll a number")
    bingo_num = random.randint(1,100)
    print("The number picked was ",bingo_num)
    if bingo_num in player_1_num:
        print(bingo_num, "was found in", player1,"s list!")
        player_1_num.remove(bingo_num)
    if bingo_num in player_2_num:
        player_2_num.remove(bingo_num)
        print(bingo_num, "was found in", player2,"s list!")
    if not bingo_num in player_1_num:
        print(bingo_num, "is not in", player1,"s list")
    if not bingo_num in player_2_num:
        print(bingo_num, "is not in", player2,"s list")
    if len(player_1_num) == 0:
        print(player1.upper()," HAS WON THE GAME")
        break
    if len(player_2_num) == 0:
        print(player2.upper()," HAS WON THE GAME")
        break
    print(player1, "your numbers are", player_1_num)
    print(player2, "your numbers are", player_2_num)


input("Press enter to exit")

1 个答案:

答案 0 :(得分:0)

问题是,您检查该号码是否在列表中,然后将其删除,然后检查列表中的不是。当然不是,因为你刚刚删除它!请改用if/else

if bingo_num in player_1_num:
    print(bingo_num, "was found in", player1,"s list!")
    player_1_num.remove(bingo_num)
else:
    print(bingo_num, "is not in", player1,"s list")

(类似于玩家2)