【问题标题】:Weird problem with calling functions in Python在 Python 中调用函数的奇怪问题
【发布时间】:2019-03-01 09:55:30
【问题描述】:

我只是想为自己编写一个代码,但在我的代码中调用特定函数时遇到问题,这很奇怪,因为我已经有另外 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.x function class object


【解决方案1】:

这部分是你的问题:

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)

最后一行缺少缩进会让你脱离类定义。换句话说,你在 global 范围内调用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()

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

【讨论】:

  • 我在回复中回复了您,请查看
  • 保留答案部分以免费回答您的问题,@aliaghayan - 您首先在评论中回复我是对的。在我在这里建议之后,您不应该收到 same 错误,但您确实有一个单独的问题。让我编辑我的答案,我也会解决这个问题。
  • 非常感谢您的帮助,我会检查并报告给您,非常感谢
  • 我发现两个问题首先是程序开头的“name”定义必须是“names”,另一个是在 def Entrance() 处,它需要添加一个参数添加到代码 game.Entrance() 的底部,但现在我又多了三个错误
  • 我想,“另外三个错误”是您的逻辑错误的一部分。试着弄清楚它们;如果不能,请提出一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-04
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
相关资源
最近更新 更多