【问题标题】:Trying to get chunk of python code to run a certain number of times试图让大量 python 代码运行一定次数
【发布时间】: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")

【问题讨论】:

  • 您尚未在 while statement 上显示您的失败尝试。像while (player_wins &lt; 4) and (comp_wins &lt; 4): ... 这样的东西应该可以正常工作。

标签: python loops if-statement


【解决方案1】:
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")

我添加了一个带有 cmets 的 while 循环。一开始,我们声明了

comp_wins = player_wins = 0

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

while comp_wins<4 and player_wins<4:

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

【讨论】:

  • 您需要在循环中使用or,而不是and。此外,第一个 if 条件现在与循环条件是多余的。我会为你编辑。
  • 嗨 Guimoute,我没有更改他的任何代码,我只是在 while 循环中添加了一个条件。而对于 while 循环,我正在检查两个分数是否低于 4。如果其中任何一个达到 4,则循环被破坏。我看到了您建议的其余编辑。它们很好,但我无法编辑它,因为我没有足够的分数来进行建议的编辑。
  • 哦,是的,我的错,它确实是and
猜你喜欢
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多