首页 > 解决方案 > 在 Python 中调用函数的奇怪问题

问题描述

我只是想为自己编写一个代码,但在我的代码中调用特定函数时遇到问题,这很奇怪,因为我已经有另外 2 个函数,就像这个函数一样,他们正确地完成了他们的工作

import random

name = ("aghayan","jafari","panahi","kashkool")
word = random.choice(names)
dash_guess = "-" * len(word)
guesses_left = 5
class hangman():
    def Entrance():
        print(" one of your python classmates was an undercover cop and set a ")
        print(" trap for our Boss Mohammad Esmaili!'THE CARTEL KING' so they arrest him .")
        print(" we just need that snitch name and your the only person of that")
        print(" class that we have access to , so your going to tell us the snitch")
        print(" name or i will hang you my self and you got only 5 chances to ")
        print(" tell me his or hers name or you'll die")
        print()
        def repeat():

            your_choice =input(" so will you help me or you want to die ? 'yes' or 'no' : ")

            if your_choice == "yes":
                print("Good it seems you have someone waiting for you and you want to ")
                print("see him/her again , you better be telling the truth or i,ll go ")
                print("and pay a visit to your love")
                core_game(guess)


            elif your_choice == "no":
                print("ok good choice , it will be my pleasure to kill you ")
                print("________      ")
                print("|      |      ")
                print("|      0      ")
                print("|     /|\     ")
                print("|     / \     ")
                print("|             ")
                print("Adios my friend , i hope you rest in peace in HELL")
                exit()

            else :
                print(" it seems the noose tightens around your neck and its getting")
                print(" hard to talk but i just need 'yes' or 'no' for answer")
                repeat()
        repeat()
    Entrance() 
    def core_game(guess):
         while guesses_left > 0 and not dash_guess == word:
             guess = input("so tell me that snitch name letter by letter : ")
         if guess != 1:
             print("NOPE , i need you to spell the name of that rat")

    core_game(guess)




game = hangman()

它不完整,但问题是当我输入“是”时,它应该将程序带到 def core_game() 但它给我的错误是“core_game 未定义”。

标签: python-3.xfunctionclassobject

解决方案


这部分是你的问题:

def core_game(guess):
     while guesses_left > 0 and not dash_guess == word:
         guess = input("so tell me that snitch name letter by letter : ")
     if guess != 1:
         print("NOPE , i need you to spell the name of that rat")

core_game(guess)

最后一行缺少缩进会使您脱离类定义。换句话说,您是core_game全局范围(未定义)而不是从类内部(已定义)调用。

Python 对缩进和格式化很挑剔;我建议您花一些时间来学习如何为 Python 正确格式化代码,这不仅可以帮助您减少错误,还可以使您和其他任何人都更容易阅读您的代码。

您的解决方案是完全删除core_game(guess)呼叫。您不需要它,因为您已经在调用Entrance(),并且它会core_game在正确的位置为您调用。

您还有另一个问题 - 您的core_game方法有一个guess参数,但这不是必需的,而且它使您很难正确调用它:

def core_game(guess):
    while guesses_left > 0 and not dash_guess == word:
        # On this line, you're overwriting the value of the guess parameter
        # before you've actually read it. Hence, you don't actually
        # need that parameter at all.
        guess = input("so tell me that snitch name letter by letter : ")
    if guess != 1:
        print("NOPE , i need you to spell the name of that rat")

而且,你称之为:

if your_choice == "yes":
    print("Good it seems you have someone waiting for you and you want to ")
    print("see him/her again , you better be telling the truth or i,ll go ")
    print("and pay a visit to your love")

    # At this point, guess is not defined, so you're not passing anything
   # to the core_game function.
    core_game(guess)

鉴于(a)您没有传递任何东西,并且(b)您从未实际使用过该参数,您可以将其删除。

在上述所有建议之后,您的代码如下所示:

import random

name = ("aghayan", "jafari", "panahi", "kashkool")
word = random.choice(names)
dash_guess = "-" * len(word)
guesses_left = 5

class Hangman():
    def entrance(self):
        print(" one of your python classmates was an undercover cop and set a ")
        print(" trap for our Boss Mohammad Esmaili!'THE CARTEL KING' so they arrest him .")
        print(" we just need that snitch name and your the only person of that")
        print(" class that we have access to , so your going to tell us the snitch")
        print(" name or i will hang you my self and you got only 5 chances to ")
        print(" tell me his or hers name or you'll die")
        print()

        def repeat():
            your_choice =input(" so will you help me or you want to die ? 'yes' or 'no' : ")

            if your_choice == "yes":
                print("Good it seems you have someone waiting for you and you want to ")
                print("see him/her again , you better be telling the truth or i,ll go ")
                print("and pay a visit to your love")
                core_game(guess)


            elif your_choice == "no":
                print("ok good choice , it will be my pleasure to kill you ")
                print("________      ")
                print("|      |      ")
                print("|      0      ")
                print("|     /|\     ")
                print("|     / \     ")
                print("|             ")
                print("Adios my friend , i hope you rest in peace in HELL")
                exit()

            else:
                print(" it seems the noose tightens around your neck and its getting")
                print(" hard to talk but i just need 'yes' or 'no' for answer")
                repeat()

        repeat()

    def core_game(self):
        while guesses_left > 0 and not dash_guess == word:
            guess = input("so tell me that snitch name letter by letter : ")
        if guess != 1:
            print("NOPE , i need you to spell the name of that rat")

game = Hangman()
game.entrance()

我还应用了一些文体更正并更正了此处的缩进。你也有一个逻辑错误,但我会把它作为练习留给你弄清楚。


推荐阅读