python - 我在石头剪刀布游戏中的 while 循环不起作用。我应该如何纠正它?
问题描述
当人类或计算机达到 5 分时,游戏应该结束。这个游戏以前只是一个功能。然后我添加了输入函数和 while 循环,以使其更高效和用户友好。
import random
choices=('rock','paper','scissor')
HUMAN_SCORE=0
COMPUTER_SCORE=0
while COMPUTER_SCORE<5 or HUMAN_SCORE<5:
computer=random.choice(choices)
human=input("Choose from rock,paper or scissor")
print("You picked:")
print(human)
print("Computer picked:")
print(computer)
if human == "rock" and computer == "rock":
print("play again")
elif human == "rock" and computer == "paper":
COMPUTER_SCORE=COMPUTER_SCORE+1
print("sorry,you lost!Better luck next time!")
elif human == "rock" and computer == "scissor":
HUMAN_SCORE=HUMAN_SCORE+1
print("Congratulations,you won!")
elif human == "paper" and computer == "paper":
print("play again")
elif human == "paper" and computer == "scissor":
COMPUTER_SCORE = COMPUTER_SCORE + 1
print("sorry,you lost!Better luck next time!")
elif human == "paper" and computer == "rock":
print("Congratulations,you won!")
HUMAN_SCORE = HUMAN_SCORE + 1
elif human == "scissor" and computer == "scissor":
print("play again")
elif human == "scissor" and computer == "rock":
print("sorry,you lost!Better luck next time!")
COMPUTER_SCORE = COMPUTER_SCORE + 1
elif human == "scissor" and computer == "paper":
print("Congratulations,you won!")
HUMAN_SCORE = HUMAN_SCORE + 1
else:
print("try again!Choose from the options above!")
print("Human_Score=")
print(HUMAN_SCORE)
print("Computer_Score=")
`print(COMPUTER_SCORE)
解决方案
while COMPUTER_SCORE<5 or HUMAN_SCORE<5:
很可能这总是正确的(人或计算机不能同时击中 5,所以必须是正确的)
你需要确保它们都小于 5and
while COMPUTER_SCORE<5 and HUMAN_SCORE<5:
推荐阅读
- django - 在 Django 中使用 ajax 从前端删除特定帖子(基于类的视图)
- macos - 使用 Finder Sync 扩展对 Electron 应用程序进行公证
- html - Tab键按下不关注angularjs中的下一个输入
- python - 来自 json 的响应即将到来
- javascript - React - 如果键存在,则过滤 JSON 数组
- flutter - 如何在颤振中添加入职屏幕?
- java - 如何在我的 Android 应用程序中运行 Linux 命令?
- r - r 替换低于矩阵中每列平均值的值
- python - Python中具有顺序的自相关矩阵
- firebase - 递归 Firebase Firestore 规则