2017-10-11 64 views
0

我創建了一個「兩個玩家猜數字」的原型,但它不會停止時points == 5。當玩家第十次獲勝時結束。 有人知道什麼是錯的?猜猜兩個玩家的數字 - 遊戲不會停止當它應該

def play_game(): 
    p1 = 0 
    p2 = 0 
    shots_taken = 0 

    number = int(input("Graczu pierwszy - wprowadź liczbę od 1 do 20: ")) 
    while number < 1 or number > 20: 
     number = int(input("Zła liczba, podaj prawidłową: ")) 
    while shots_taken < 3: 
     shot = int(input("Graczu drugi - zgadnij: ")) 
     shots_taken += 1 
     if shot < number: 
      print("Więcej") 
     if shot > number: 
      print("Mniej") 
     if shot == number: 
      break 


    if shot != number: 
     print("Gracz pierwszy wygrywa.") 
     p1 += 1 
    else: 
     print("Gracz drugi wygrywa.") 
     p2 += 1 
    return [p1, p2] 

points = [0, 0] 

while points[0] < 5 or points[1] < 5: 
    points = [0, 0] 
    points[0] += play_game()[0] 
    points[1] += play_game()[1] 
    if points[0] == 5 or points[1] == 5: 
     break 

print("Gracz 1 ma punktów", points[0]) 
print("Gracz 2 ma punktów", points[1]) 

回答

2

重置比分回到0:0在每個循環的開始:

points = [0, 0] 

while points[0] < 5 or points[1] < 5: 
    # points = [0, 0] <-- Remove this line 
    points[0] += play_game()[0] 
    points[1] += play_game()[1] 
    if points[0] == 5 or points[1] == 5: 
     break 

此外,您在運行每個循環的兩場比賽。 Try:

points = [0, 0] 

while points[0] < 5 or points[1] < 5: 
    result = play_game() # Result will have the format [x, y] 
    points[0] += result[0] 
    points[1] += result[1] 
    if points[0] == 5 or points[1] == 5: 
     break