繁体   English   中英

Python中调用函数的怪异问题

[英]Weird problem with calling functions in Python

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

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()

它还不完整,但问题是,当我输入“是”时,它应该带程序去定义core_game(),但是它给我一个错误,即“未定义core_game”。

这部分是您的问题:

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()

我还应用了一些样式更正,并在此更正了缩进。 您还剩下一个逻辑错误,但我将其留给您练习。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM