python - 为什么我的 while 循环不会结束我的石头、纸、剪刀游戏?
问题描述
我无法弄清楚为什么我的 while 循环不会在user_count
or之后comp_count
终止我的游戏3
。
有人可以提供一些建议吗?我没有看到我缺少什么,因为我在 while 循环下缩进了所有内容,并且我每播放一回合都会增加分数 +=1。
import random
rock_paper_scissor_list = ['ROCK', 'PAPER', 'SCISSORS']
def comp_turn():
comp_choice = random.choice(rock_paper_scissor_list)
return comp_choice
def user_turn():
raw_user_input = input('Choose rock/paper/scissors: ').lower()
if raw_user_input in ['rock', 'r']:
user_choice = 'ROCK'
elif raw_user_input in ['paper', 'p']:
user_choice = 'PAPER'
else:
user_choice = 'SCISSORS'
return user_choice
def play_game(user_choice, comp_choice):
user_score = 0
comp_score = 0
print('User choice is: ' + user_choice)
print('Comp choice is: ' + comp_choice + '\n')
while user_score < 3 or comp_score < 3:
if comp_choice == 'ROCK' and user_choice == 'ROCK':
print("It's a tie!")
elif comp_choice == 'PAPER' and user_choice == "ROCK":
print('Comp wins this round!')
comp_score += 1
elif comp_choice == 'SCISSORS' and user_choice == "ROCK":
print('You win this round!')
user_score += 1
elif comp_choice == 'ROCK' and user_choice == "PAPER":
print('You win this round!')
user_score += 1
elif comp_choice == 'PAPER' and user_choice == "PAPER":
print("It's a tie!")
elif comp_choice == 'SCISSORS' and user_choice == "PAPER":
print('Comp wins this round!')
comp_score += 1
elif comp_choice == 'ROCK' and user_choice == "SCISSORS":
print('Comp wins this round!')
comp_score += 1
elif comp_choice == 'PAPER' and user_choice == "SCISSORS":
print('You win this round!')
user_score += 1
elif comp_choice == 'SCISSORS' and user_choice == "SCISSORS":
print("It's a tie!")
print('\nUser score is: ' + str(user_score))
print('Comp score is: ' + str(comp_score))
play_game(user_turn(), comp_turn())
解决方案
user_score < 3 or comp_score < 3
直到两个分数都大于或等于 3 才会为真。您要and
确保两者都低于 3:
while user_score < 3 and comp_score < 3:
推荐阅读
- python - 抓取非格式化的json
- scala - 如何将列表值转换为Key、值格式?
- linq - 如何在 LINQ 中从具有多个 ID 的记录中查询名称
- java - 从 onpostexecute 发送值
- python - 需要更新每一行的数组或数据帧上的 Python 操作
- drupal - 无法检查树枝中的标题值,Drupal 8
- mysql - 将两个具有多个条件和一个最大条件的 sql 表组合在一起的最佳方法
- java - 多重 Map 的第二个键值对不考虑作为 httpHeader spring 4.1.7 中的 Header
- apache-spark - 无法在 Databricks 中创建 H2OContext - 使用 pysparkling
- c# - 如何对数据进行分组。我想显示一个列表,单击时包含 foo 的数据,单击时包含 boo 的数据