在Python中的石头剪刀布游戏中出现的问题。

huangapple go评论73阅读模式
英文:

Problem With A Rock Paper Scissors Game In Python

问题

调用所有的程序员,祝愿你们在各自的编程之旅中一切顺利!

我目前正在用Python制作一个石头、剪刀、布游戏(传统的,我知道)。到目前为止,一切都按预期进行,除了一件事。在编码时,我想我应该为玩家和CPU都设置一个得分限制,即5分(如下所示),以防止半无限循环的发生。我已经这样做了。但是,当电脑的分数或玩家的分数达到5分时,游戏不会停止,而是继续要求玩家选择(石头、剪刀或布),即使我已经设置了停止或退出游戏的条件。以下是参考代码:

# 石头、剪刀、布
import random

choices = ("石头", "剪刀", "布")
player_score = 0
computer_score = 0
playing = False
score_limit = 5

play = input("要玩吗?(y/n):")

if play in ["y", "yes"]:
    playing = True
elif play in ["n", "no"]:
    playing = False
else:
    raise Exception("无效输入")

while playing:
    player = input("输入你的选择:")
    computer = random.choice(choices)

    if player == "石头":
        if computer == "布":
            print("电脑选择:{}".format(computer))
            computer_score += 1
            print("你的分数:{}".format(player_score))
            print("电脑的分数:{}".format(computer_score))
            continue
        if computer == "剪刀":
            print("电脑选择:{}".format(computer))
            player_score += 1
            print("你的分数:{}".format(player_score))
            print("电脑的分数:{}".format(computer_score))
            continue

    elif player == "布":
        if computer == "石头":
            print("电脑选择:{}".format(computer))
            player_score += 1
            print("你的分数:{}".format(player_score))
            print("电脑的分数:{}".format(computer_score))
            continue
        if computer == "剪刀":
            print("电脑选择:{}".format(computer))
            computer_score += 1
            print("你的分数:{}".format(player_score))
            print("电脑的分数:{}".format(computer_score))
            continue

    elif player == "剪刀":
        if computer == "石头":
            print("电脑选择:{}".format(computer))
            computer_score += 1
            print("你的分数:{}".format(player_score))
            print("电脑的分数:{}".format(computer_score))
            continue
        if computer == "布":
            print("电脑选择:{}".format(computer))
            player_score += 1
            print("你的分数:{}".format(player_score))
            print("电脑的分数:{}".format(computer_score))
            continue

    if player == computer:
        print("电脑选择:{}".format(computer))
        print("你的分数:{}".format(player_score))
        print("电脑的分数:{}".format(computer_score))
        continue

    elif player not in choices:
        raise Exception("无效输入。")

    if player_score == score_limit or computer_score == score_limit:
        print("游戏结束")
        break

希望这可以帮助你解决问题!

英文:

Calling all coders, wish you all the best on your respective coding journeys!

I'm currently making a rock, paper, scissors game (traditional, I know) in Python. Everything so far is going so expected, except for one thing. While I was coding, I figured that I should set up a scoring limit for both the player and the CPU, which is 5 (as you will see below) to prevent a semi-infinite loop from occuring. And I did. However, when either the computer's score or the player's score reaches 5, the game doesn't stop, instead it continues to ask the player for his choice (rock, paper, or scissors), even if I had already set up conditions to stop, or break out of, the game after that occurs. Here is the code for reference

# Rock, Paper, Scissors
import random
choices = ("Rock", "Paper", "Scissors")
player_score = 0
computer_score = 0
playing = False
score_limit = 5
play = input("Do you want to play? (y/n): ")
if play in ["y", "yes"]:
playing = True
elif play in ["n", "no"]:
playing = False
else:
raise Exception("Invalid input")
while playing:
player = input("Enter your choice: ")
computer = random.choice(choices)
if player == "Rock":
if computer == "Paper":
print("Computer's choice: {}".format(computer))
computer_score += 1
print("Your score: {}".format(player_score))
print("Computer's score: {}".format(computer_score))
continue
if computer == "Scissors":
print("Computer's choice: {}".format(computer))
player_score += 1
print("Your score: {}".format(player_score))
print("Computer's score: {}".format(computer_score))
continue
elif player == "Paper":
if computer == "Rock":
print("Computer's choice: {}".format(computer))
player_score += 1
print("Your score: {}".format(player_score))
print("Computer's score: {}".format(computer_score))
continue
if computer == "Scissors":
print("Computer's choice: {}".format(computer))
computer_score += 1
print("Your score: {}".format(player_score))
print("Computer's score: {}".format(computer_score))
continue
elif player == "Scissors":
if computer == "Rock":
print("Computer's choice: {}".format(computer))
computer_score += 1
print("Your score: {}".format(player_score))
print("Computer's score: {}".format(computer_score))
continue
if computer == "Paper":
print("Computer's choice: {}".format(computer))
player_score += 1
print("Your score: {}".format(player_score))
print("Computer's score: {}".format(computer_score))
continue
if player == computer:
print("Computer's choice: {}".format(computer))
print("Your score: {}".format(player_score))
print("Computer's score: {}".format(computer_score))
continue
elif player not in choices:
raise Exception("Invalid input.")
if player_score == score_limit or computer_score == score_limit:
print("Game ended")
break

答案1

得分: 3

代码永远不会执行,因为您在每种可能性之后都使用了 continue。您可以通过删除它们并将连续的 if 语句替换为 elif 来修复这个问题。

示例:

while playing:
    player = input("输入你的选择: ")
    computer = random.choice(choices)

    if player == "Rock":
        if computer == "Paper":
            print("电脑选择: {}".format(computer))
            computer_score += 1
            print("你的分数: {}".format(player_score))
            print("电脑的分数: {}".format(computer_score))
        elif computer == "Scissors":
            print("电脑选择: {}".format(computer))
            player_score += 1
            print("你的分数: {}".format(player_score))
            print("电脑的分数: {}".format(computer_score))

    elif player == "Paper":
        if computer == "Rock":
            print("电脑选择: {}".format(computer))
            player_score += 1
            print("你的分数: {}".format(player_score))
            print("电脑的分数: {}".format(computer_score))
        elif computer == "Scissors":
            print("电脑选择: {}".format(computer))
            computer_score += 1
            print("你的分数: {}".format(player_score))
            print("电脑的分数: {}".format(computer_score))
英文:

The code is never reached because you use continue after every possibility. You can fix this by removing them and replacing your consecutive if statements with elif.

example:

while playing:
player = input("Enter your choice: ")
computer = random.choice(choices)
if player == "Rock":
if computer == "Paper":
print("Computer's choice: {}".format(computer))
computer_score += 1
print("Your score: {}".format(player_score))
print("Computer's score: {}".format(computer_score))
elif computer == "Scissors":
print("Computer's choice: {}".format(computer))
player_score += 1
print("Your score: {}".format(player_score))
print("Computer's score: {}".format(computer_score))
elif player == "Paper":
if computer == "Rock":
print("Computer's choice: {}".format(computer))
player_score += 1
print("Your score: {}".format(player_score))
print("Computer's score: {}".format(computer_score))
elif computer == "Scissors":
print("Computer's choice: {}".format(computer))
computer_score += 1
print("Your score: {}".format(player_score))
print("Computer's score: {}".format(computer_score))

答案2

得分: 0

import random

def judge(player, computer):
    wins = [("石头", "剪刀"),
            ("剪刀", "纸"),
            ("纸", "石头")]
    if player == computer:
        return "平局"
    if (player, computer) in wins:
        return "你赢了"
    return "计算机赢了"

choices = ("石头", "纸", "剪刀")
player_score = 0
computer_score = 0
playing = False
score_limit = 5

play = input("你想玩吗? (y/n): ")

if play in ["y", "yes"]:
    playing = True
elif play in ["n", "no"]:
    playing = False
else:
    raise Exception("无效输入")

while playing:
    player = input("请输入你的选择: ")
    if player not in choices:
        raise Exception("无效输入。")
    
    computer = random.choice(choices)
    print(f"计算机选择: {computer}")
    outcome = judge(player, computer)
    
    if outcome == "你赢了":
        player_score += 1
    elif outcome == "计算机赢了":
        computer_score += 1
    
    print(f"你的分数: {player_score}")
    print(f"计算机的分数: {computer_score}\n")
    
    if score_limit in [player_score, computer_score]:
        print("游戏结束")
        break
英文:

You already have a direct answer to your question. However, you might find the following refactored version of your code to be helpful. In particular, I've reorganized the while loop's logic so that you're not giving instructions redundantly.

import random
def judge(player, computer):
wins = [("Rock","Scissors"),
("Scissors","Paper"),
("Paper","Rock")]
if player == computer:
return "tie"
if (player,computer) in wins:
return "win"
return "lose"
choices = ("Rock", "Paper", "Scissors")
player_score = 0
computer_score = 0
playing = False
score_limit = 5
play = input("Do you want to play? (y/n): ")
if play in ["y", "yes"]:
playing = True
elif play in ["n", "no"]:
playing = False
else:
raise Exception("Invalid input")
while playing:
player = input("Enter your choice: ")
if player not in choices:
raise Exception("Invalid input.")
computer = random.choice(choices)
print(f"Computer's choice: {computer}")
outcome = judge(player, computer)
if outcome == "win":
player_score += 1
elif outcome == "lose":
computer_score += 1
print(f"Your score: {player_score}")
print(f"Computer's score: {computer_score}\n")
if score_limit in [player_score, computer_score]:
print("Game ended")
break

huangapple
  • 本文由 发表于 2023年7月18日 03:29:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定