簡體   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