我如何将它放在python的循环中,以便它不断询问玩家1是否赢得了比赛,直到它达到了比赛中的游戏数量。我尝试了一段时间但它没有工作:(
Y="yes"
N="no"
PlayerOneScore=0
PlayerTwoScore=0
NoOfGamesInMatch=int(input("How many games? :- "))
while PlayerOneScore < NoOfGamesInMatch:
PlayerOneWinsGame=str(input("Did Player 1 win the game?\n(Enter Y or N): "))
if PlayerOneWinsGame== "Y":
PlayerOneScore= PlayerOneScore+1
else:
PlayerTwoScore= PlayerTwoScore+1
print("Player 1: " ,PlayerOneScore)
print("Player 2: " ,PlayerTwoScore)
print("\n\nPress the RETURN key to end")
答案 0 :(得分:0)
while
循环可以正常工作
while PlayerOneScore < NoOfGamesInMatch:
PlayerOneWinsGame=str(input("Did Player 1 win the game?\n(Enter Y or N): "))
if PlayerOneWinsGame== "Y":
PlayerOneScore= PlayerOneScore+1
else:
PlayerTwoScore= PlayerTwoScore+1
将其他所有内容排除在外。意思是在循环之前保留初始化内容,然后在循环之后报告分数。
答案 1 :(得分:0)
import sys
while PlayerOneScore < NoOfGamesInMatch:
answer = str(input("Did Player 1 win the game?\n(Enter Y or N or Q to quit): "))
if answer.lower() == "q":
sys.exit()
elif answer.lower() == "y":
PlayerOneScore = ( PlayerOneScore + 1 )
elif answer.lower() == "n":
PlayerTwoScore = ( PlayerTwoScore + 1 )
else
print "Valid answers are Y (yes), N (no) and Q (quit).
您现在可以输入q离开游戏。我也是这样做的,所以答案不区分大小写。
只要PlayerOneScore小于NoOfGamesInMatch,游戏就会开始。
您应该将导入系统放在文件的顶部。