试图让python代码块运行一定次数

时间:2019-04-14 20:31:06

标签: python loops if-statement

我编写了一个剪刀石头布游戏,并且有一些获胜者。我希望游戏可以重复进行,直到其中一个获胜计数器达到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")

1 个答案:

答案 0 :(得分:0)

import random
import time

moves=["rock", "paper", "scissors"]

# Initially setting the score to 0
comp_wins = player_wins = 0

# while none of them reaches a score of 4 this loop will continue
# if either of them gets a score of 4 we terminate the loop
while comp_wins<4 and player_wins<4:
    play_move = str(input("Please choose rock, paper, or scissors: "))
    computer_move= moves[random.randint(0, 2)]
    print("Ready...")
    # time.sleep(1)
    print("Set...")
    # time.sleep(1)
    print("GO!")
    # time.sleep(1)

    ties= 0

    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")

我添加了一个带有评论的while循环。首先,我们声明

comp_wins = player_wins = 0

这样我们就可以在while循环中访问它们。

while comp_wins<4 and player_wins<4:

现在在while循环中,我们正在检查它们的两个分数是否都低于4。一旦它们中的任何一个达到4,我们就会终止。