【问题标题】:Why does my code not end even when directly prompted to?为什么即使直接提示我的代码也没有结束?
【发布时间】:2018-04-30 03:10:58
【问题描述】:

所以我的其余代码是正确的,但它并没有结束,我不确定为什么。它不应该是无限的,因为当任何一个变量超过 5 时会提示它结束但它会继续。有答案吗?

import random
Round = 0
Player_Score = 0
Computer_Score = 0
while Player_Score < 5 or Computer_Score < 5:
    Player_object = input("Would you like to choose R, P, or S?")
    Computer_object = random.sample("RPS", 1)[0]
    if Player_object == "R" or Player_object == "r":
        if Computer_object == "R":
             print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have tied with the Computer and neither of you have scored a point.")
        elif Computer_object == "P":
            Computer_Score += 1 
            print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have been beaten by the Computer and it has scored a point.")
        else:
            Player_Score += 1
            print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have beaten the Computer and you have scored a point.")


    if Player_object == "P" or Player_object == "p":
        if str(Computer_object) == "R":
            Player_Score += 1
            print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have beaten the Computer and you have scored a point.")
        elif str(Computer_object) == "P":
           print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have tied with the Computer and neither of you have scored a point.")
        else:
            Computer_Score += 1
            print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have been beaten by the Computer and it has scored a point.")


    if Player_object == "S" or Player_object == "s":
        if str(Computer_object) == "R":
             print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have been beaten by the Computer and it has scored a point.")
        elif str(Computer_object) == "P":
            Computer_Score += 1
            print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have beaten the Computer and you have scored a point.")
        else:
            Player_Score += 1
            print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have tied with the Computer and neither of you have scored a point.")
if Computer_Score == 5 and Player_Score != 5:
    print("The Computer has won!")
if Player_Score == 5 and Computer_Score != 5:
    print("You have won and beaten the computer")

【问题讨论】:

  • 您是否尝试过跟踪变量值?以印刷品为例?
  • 你的意思是while Player_Score &lt; 5 and Computer_Score &lt; 5 -- and,而不是or

标签: python infinite


【解决方案1】:

你的循环在两者都高于 5 时结束,如果你希望它在任何一个高于 5 时停止,你需要在你的主 for 循环中包含以下内容:

while Player_Score < 5 and Computer_Score < 5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2016-12-31
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2017-06-22
    相关资源
    最近更新 更多