python中设置整数后While或loop不停止
试图制作一个石头剪纸游戏,玩家对电脑进行版本更新,以更新编码知识,但我似乎无法在while循环中找到导致问题的原因。我把它设置为当计算机或个人达到3分时完成,但当总分等于8分时停止。完整代码如下所示python中设置整数后While或loop不停止,python,while-loop,Python,While Loop,试图制作一个石头剪纸游戏,玩家对电脑进行版本更新,以更新编码知识,但我似乎无法在while循环中找到导致问题的原因。我把它设置为当计算机或个人达到3分时完成,但当总分等于8分时停止。完整代码如下所示 import random rock = "rock" paper = "paper" scissors = "scissors" humanscore = int(0) compscore = int(0) limit = int(3)
import random
rock = "rock"
paper = "paper"
scissors = "scissors"
humanscore = int(0)
compscore = int(0)
limit = int(3)
comp = ["paper", "rock", "scissors"]
while humanscore <= limit or compscore <= limit:
human = str(input("Choose rock, paper, or scissors, first to 3 wins! "))
answer = random.choice(comp)
if human == answer:
print("Tie, Computer chose, ", answer)
if answer == rock:
if human == paper:
humanscore += 1
print("You Win!")
elif human == scissors:
compscore += 1
print("Computer Won! Try again")
if answer == paper:
if human == rock:
compscore += 1
print("Computer Won! Try again")
elif human == scissors:
humanscore + 1
print("You Win!")
if answer == scissors:
if human == paper:
compscore += 1
print("Computer Won! Try again")
elif human == rock:
humanscore += 1
print("You Win!")
print("\n Computer Chose: ", answer, "computer score: ", compscore, "Human Score:", humanscore)
正如在评论中已经说过的,问题在于或处于您的中断状态。只要其中一个分数低于或等于3,它就会继续运行 例如,如果humanscore=3和comp score=2,则条件的两个部分都是真的,这意味着它将继续
人类一旦得分4,CopSc==4,一旦你整理出了循环,这两个HuMADS
——如果你只想在分数低于两个极限的情况下继续前进,则根据SHILI的答案来修正——你可能想考虑一个数据结构建议:
做一个记录什么选择胜过某个给定选择的口述: whatBeats={石头:布,布:剪刀,剪刀:石头} 然后在做出选择之后。。。 如果答案==吃什么[人]: 计算机获胜 elif human==吃什么[回答]: 人类胜利 其他: 画几十年前,在我了解任何索引结构(如数组)之前,我编写了一个排序程序,您当前的方法让我想起了这类工作。这里虽然humanscore不相关,但:要初始化int变量,只需执行以下操作:limit=3,无需使用int3等。此外,输入总是返回str;在值上调用str是不可行的。啊,我现在明白了,我一直在读它,好像或者如果,并没有按照我最初打算的方式比较两者。谢谢
while humanscore < limit and compscore < limit: