首页 > 解决方案 > 为什么我的 while 循环不会结束我的石头、纸、剪刀游戏?

问题描述

我无法弄清楚为什么我的 while 循环不会在user_countor之后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())

标签: python

解决方案


user_score < 3 or comp_score < 3直到两个分数都大于或等于 3 才会为真。您要and确保两者都低于 3:

while user_score < 3 and comp_score < 3:

推荐阅读