繁体   English   中英

如何使用 while 循环递增一个值 python

[英]how to increment a value using while loop python

你能告诉我为什么当其中一些值达到 3 时我的程序不增加“bot_points = 0”“your_points = 0”来停止 while 循环吗

import random
rock_paper_scissors = ["ROCK", "PAPER", "SCISSORS"]


bot_points = 0
your_points = 0
while bot_points <= 3 or your_points <= 3:
    user_choice = input('type "rock", "paper" or "scissors":  ').upper()
    bot_choice = random.choice(rock_paper_scissors)
    if bot_choice == "ROCK" and user_choice == "ROCK":
        print("Draw")
    elif bot_choice == "ROCK" and user_choice == "PAPER":
        print("You win")
        your_points = your_points+1
    elif bot_choice == "PAPER" and user_choice == "PAPER":
        print("Draw")
    elif bot_choice == "PAPER" and user_choice == "SCISSORS":
        print("You win")
        your_points= your_points+1
    elif bot_choice == "PAPER" and user_choice == "ROCK":
        print("You lose")
        bot_points = bot_points+1
    elif bot_choice == "SCISSORS" and user_choice == "PAPER":
        print("You lose")
        bot_points = bot_points+1
    elif bot_choice == "SCISSORS" and user_choice == "ROCK":
        print("You win")
        your_points = your_points+1
    elif bot_choice == "SCISSORS" and user_choice == "SCISSORS":
        print("Draw")
    elif bot_choice == "ROCK" and user_choice == "SCISSORS":
        print("You lose")
        bot_points = bot_points+1

两个问题:

  1. 你测试<= 3 ,所以它不会结束,直到有人赢了四次
  2. 你测试bot_points <= 3 or your_points <= 3 ,所以直到两个玩家都至少赢了四次,它才会结束; and在达到阈值时结束

如果您想在一玩家获胜三次时结束,请将测试更改为:

while bot_points < 3 and your_points < 3:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM