【发布时间】:2019-04-14 20:31:05
【问题描述】:
我编写了一个剪刀石头布游戏,并且有一些获胜计数器。我希望游戏重复进行,直到其中一个获胜计数器达到 4,但我想不出一个好的方法来做到这一点。
我尝试使用 while 循环,但无法让它一遍又一遍地停止重复,也找不到停止它的方法。
import random
import time
moves=["rock", "paper", "scissors"]
play_move=str(input("Please choose rock, paper, or scissors: "))
computer_move= moves[random.randint(0,2)]
print("Ready \n")
time.sleep(1)
print("Set \n")
time.sleep(1)
print("GO! \n")
time.sleep(1)
comp_wins=0
player_wins=0
ties= 0
if player_wins <= 4 or comp_wins <=4:
if play_move == computer_move:
print("Both players chose", play_move, "and there is a tie, please play again")
ties= ties+1
elif play_move == "rock":
if computer_move == "paper":
print(computer_move, "beats", play_move, "You have lost!")
comp_wins= comp_wins+1
else:
print(play_move, "beats", computer_move, "You have won!")
player_wins=player_wins+1
elif play_move == "paper":
if computer_move == "scissors":
print(computer_move, "beats", play_move, "You have lost!")
comp_wins= comp_wins+1
else:
print(play_move, "beats", computer_move, "You have won!")
player_wins=player_wins+1
elif play_move == "scissors":
if computer_move == "rock":
print(computer_move, "beats", play_move, "You have lost!")
comp_wins= comp_wins+1
else:
print(play_move, "beats", computer_move, "You have won!")
player_wins=player_wins+1
else:
print("Your selection was invalid please try again")
【问题讨论】:
-
您尚未在
whilestatement 上显示您的失败尝试。像while (player_wins < 4) and (comp_wins < 4): ...这样的东西应该可以正常工作。
标签: python loops if-statement