2014-02-21 113 views
-2
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

54約翰的名單被發現!

54不是阿俊的名單

54不是約翰的名單

我想知道我怎麼能解決這個錯誤

我的整個碼。我只是一個初學者所以它可能是混亂和某些部分可能是不必要的

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

粘貼所有的代碼和你的輸入 – zhangxaochen

+2

你需要給我們的樣本數據了。很可能你在混合字符串和整數值。 –

+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類似)

+0

非常感謝!太簡單了 – ArjunLotay