我创建了一个“两个玩家猜一个数字”的原型,但它在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])
答案 0 :(得分: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
此外,您在每个循环中运行两个游戏。 尝试:
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