【问题标题】:Function and loop debugging [duplicate]函数和循环调试[重复]
【发布时间】:2026-01-03 13:15:02
【问题描述】:

这是我写的代码,它运行得很好,这个代码的功能也是如此停止这是假设发生的事情。另一方面,如果他们说“是”,他们会得到另一个完全相同的提示“你想再试一次吗? (是/否)'。你可以说 y 100 次,什么都不会发生,我的目标是让代码返回并从一开始就调用函数。如果用户说“y”以尝试退出循环等,我已经尝试过休息,但它不起作用,我现在不知道......

此外,如您所见,我有正确的数字,可以比较用户猜测的数字是否在生成的列表中,这部分我没有问题。现在有了正确的位置,我不知道该怎么做,目标是检查两个列表中的数字和位置是否都是名称。

import random

play = True
turnsleft = 1

#this is a function that is in charge of generating a random password
def generatePassword():
    generatePassword = [] #create an empty list
    for i in range(1,6):
        generatePassword.append(random.randint(1,9))
    return generatePassword

'''this is a function that prompts the userfor their guess
the input will be comprimised of 5 different variables to store
the different numbers which will then all be added to the list of user number'''
def getUserGuess():
    getUserGuess = [] #create an empty list
    v1,v2,v3,v4,v5 = input("Please take a guess of the password by entering 5 numbers(comma between each): ").split(",")
    v1,v2,v3,v4,v5 = int(v1), int(v2), int(v3), int(v4), int(v5)
    for i in(v1,v2,v3,v4,v5):
        getUserGuess.append(i)
    return getUserGuess

#this function will compare the cpu generated password to the user inputed numbers list
def reportResult(generatePassword,getUserGuess):
    correctdigits = 0
    correctlocations = 0
    global turnsleft #use the play variable initiated outside the funtion
    global play #use the play variable initiated outside the funtion

    while play is True:
        if getUserGuess == generatePassword:
            print("Congradulations! You have guessed the right password.")
        elif turnsleft == 0:
            print("You will never guess my password! It was " +str(generatePassword()))
            playagain = input("Would you like to play again? (y/n) ")
            if playagain == 'n':
                play = False
        else:
            turnsleft-= 1
            for e in getUserGuess():
                if e in generatePassword():
                    correctdigits+= 1
            for e in getUserGuess():
                if e in generatePassword():
                    correctlocations+= 1
            print(str(turnsleft) +" guesses left.")
            print(str(correctdigits) +" of 5 correct digits.")
            print(str(correctlocations) +" of 5 correct locations.")
    return reportResult

while play is True:
    reportResult(generatePassword,getUserGuess)

【问题讨论】:

  • 再看看你的代码。您正在检查哪个变量以查看是否应该显示“想再玩一次”?信息?也许如果您在输入“y”时修改/重置该变量,它将停止显示...
  • 另一个问题是您似乎调用函数来生成密码并让用户多次猜测。每次调用这些函数时,它都会生成一个新密码并请求用户进行新的猜测。用户猜测应该每回合调用一次,存储到一个变量中,然后应该将该变量与每场游戏仅生成一次的密码进行检查。
  • 看起来你传递给你的函数 sa 这样的reportResult(generatePassword,getUserGuess),实际上是函数对象的名称,而不是像 generatePassword() 这样的函数的执行
  • 而且......任何用户都很难获得正确的代码,因为您只是从您指定的 int 范围生成随机抽样:/

标签: python python-3.x


【解决方案1】:

我相信你只需要在 'turnsleft' 为 0 时将 'turnsleft' 设置为大于 0 的值。

例如:

elif turnsleft == 0:
        print("You will never guess my password! It was " +str(generatePassword()))
        turnsleft = 2 #<-- Reset turns here!
        playagain = input("Would you like to play again? (y/n) ")
        if playagain == 'n':
            play = False

这将允许您“开始新游戏”,并将回合设置为某个值。但它也带来了新的问题。也许您应该编写一个 resetGame() 来编辑真正从头开始所需的所有变量。

【讨论】: