我的石头剪刀布游戏中出现错误,在 pass 语句后循环无法正常工作

    # Created By Evan Ryan


# we import our random module and time module

from random import *

import time


# this will track our wins

comp_wins = 0

player_wins = 0


# this will let us know when the match is finished

endgame = 0


# here we use simple print and sleep function to display a welcome message


print("Welcome to Rock, Paper, Scissors, Lizard, Spock")

time.sleep(3)

print("This match will be best 3 out of 5")

time.sleep(3)

print("Good Luck")

time.sleep(1)


# this will be our function to allow the user to make a choice

# they have 5 choices

# if they make a typo or choose a non option they will be asked to choose again

# we will also convert their selection to a lowercase letter


def pick_option():

    user_pick = input("Choose Rock, Paper, Scissors, Lizard, Spock: ")

    if user_pick in ["Rock", "rock", "r", "R"]:

        user_pick = "r"

    elif user_pick in ["Paper", "paper", "p", "P"]:

        user_pick = "p"

    elif user_pick in ["Scissors", "scissors", "sc", "SC"]:

        user_pick = "sc"

    elif user_pick in ["Lizard", "lizard", "l", "L"]:

        user_pick = "l"

    elif user_pick in ["Spock", "spock", "sp", "SP"]:

        user_pick = "sp"

    else:

        print("Are you speaking english buddy?")

        pick_option()

    return user_pick


# this will act as our function for the computer to choose

# they will random select a number 1-5

# these number are assinged to one of the five options the user has


def comp_option():

    comp_pick = randint(1,5)

    if comp_pick == 1:

        comp_pick = "r"

    if comp_pick == 2:

        comp_pick ="p"

    if comp_pick == 3:

        comp_pick = "sc"

    if comp_pick == 4:

        comp_pick = "l"

    if comp_pick == 5:

        comp_pick = "sp"

    return comp_pick

所以我遇到的麻烦就是这样。游戏运行良好,一旦计算机或用户取得 3 场胜利,我们就会收到获胜或失败的消息。发生这种情况后,系统会要求您再次玩游戏。如果您点击“否”,则代码结束。如果您点击“是”,游戏将正确重新启动,但在您第一次选择时,它会将您带回获胜或失败屏幕。似乎胜利计数器没有重置或者我有不正确的缩进?


任何帮助表示赞赏。


PS:我知道可能还有一种更简单的方法来制作游戏,而不是使用所有这些 elif 语句。也许是上课什么的?虽然我是新手,所以这对我来说是最简单的。


红糖糍粑
浏览 134回答 3
3回答

温温酱

一些旁注建议:要检查变量是否是一系列字符串之一,您可以通过执行以下操作使其更加紧凑:if user_pick.lower() in ["rock", "r"]:无需将 int 转换为 int :) int(1) == 1endgame充当旗帜 - 游戏是否结束。True因此,使用布尔值 - / False- 比使用 int更有意义。现在您的主要问题是您的代码endgame根据player_wins和设置变量comp_wins。当您开始新游戏时,这些值会保留旧值,因此一轮游戏结束后立即再次结束。只需重置代码最后部分中的所有关键变量:endgame = False...if player_wins >= 3:     endgame = True...if comp_wins >= 3:    endgame = True...if endgame:    user_pick = input("Do you want to play again (y/n)?")    if user_pick.lower() in ["y", "yes"]:        endgame = False        player_wins = 0        comp_wins = 0    else:        break附注建议:通常重复的if//树结构elif可以else被减少。在其他语言中,您有这样的switch语句 - 这有点让代码看起来相同,只是与if/else. 在 Python 中,您通常可以将大多数if树转换为字典中的选择。所以你的comp_option可以改为:def comp_option():    comp_pick = randint(1,5)    options = {1: 'r', 2: 'p', 3: 'sc', 4: 'l', 5: 'sp'}    return options[comp_pick]

慕容708150

我认为您必须添加一些额外的语句,以便在用户选择重新启动游戏时重置游戏内的统计数据。就像是: if int(endgame) == int(1):        user_pick = input("Do you want to play again (y/n)?")        if user_pick in ["Y", "y", "yes", "Yes"]:            comp_wins = 0            player_wins = 0            endgame = 0            continue

神不在的星期二

你发现问题了!您必须重置残局计数。为此,只需将endgame变量放入循环中while True(在开头)while True:    endgame = 0这应该有效。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python