1

So I've defined the initial opening of my game as mainmenu and within that is prints a few things with a few options to choose from such as the dificulty of the levels or they can see help for the game. When they go to help I want them to have the option to go back to the main menu, hence why I made def mainmenu(). However in my helpmenu if statement even though I have stated that if they type menu or Menu that it should call back on mainmenu it doesn't do anything. We have a module that we must use and this is why I have to use things such as p.next() which actually means it's listening for a user input so just ignore that :)

here's the code

       def mainmen():
        p.write("Welcome to 'The Great Escape'!\n")
        p.write("\nPlease type what dificulty you would like to play the game,\nthe options     are Easy, Medium or Hard\n")
        p.write("\nHowever, if you need help please type Help for instructions\n")
        p.write(">>>")

        dificulty = p.next()  

        if dificulty == "easy" or dificulty == "Easy":  
            p.clear() 
            p.write("The Great Escape")
            easy() 

        elif dificulty == "medium" or dificulty == "Medium":
            p.clear()
            p.write("The Great Escape")
            medium()

        elif dificulty == "hard" or dificulty == "Hard":
            p.clear()
            p.write("The Great Escape")
            hard()


        elif dificulty == "help" or dificulty == "Help":
            p.clear()
            p.write("Welcome to 'The Great Escape' instructions\n")
            p.write("\nTo complete the level you must move your Turtle around the\nline without touching the line itself\n")
            p.write("\nControls\n")
            p.write("Forward    - 'W'\n")
            p.write("Left       - 'A'\n")
            p.write("Backwards  - 'S'\n")
            p.write("Right      - 'D'\n")
            p.write("\nPlease type 'Menu' to go back to the main menu,\nor 'Exit' to quit the game\n")
            p.write(">>>")

**`here is my help menu if statements, I want it so that if they type Menu or menu they get taken back to the main menu.`** 

helpmenu = p.next()
if helpmenu == "Menu" or helpmenu == "menu":
    p.clear()
    mainmenu()  **<<This should call on the mainmenu but it doesn't??**

elif helpmenu == "Exit" or helpmenu == "exit":
    p.clear()
    p.write("Hope you play soon!")
4

2 回答 2

0

一般来说,在调用类方法之前,必须先实例化该类,然后才能使用点符号调用不同的方法。

回到你的情况,变量 p 是什么?如果您的代码不起作用,Lennart 的答案就是这样做的方法,这是您底部的 if 语句的问题。您正在执行语句

helpmenu = p.next()

然后您在 if 语句中检查 helpmenu 的值。在不知道 p 是什么以及它的 next() 方法是什么的情况下,我无法给出一个真正有用的答案,但是让我们说

p.next()

返回“菜单”或“退出”。在这种情况下,您应该编写代码的最后一部分,如下所示:

# Note that helpmenu is either "menu" or "exit", so either the if block will be
# executed or the elif block will be executed. 
if helpmenu == "Menu" or helpmenu == "menu":
    p.clear()
    m = mainmenu()
    m.mainmen()

elif helpmenu == "Exit" or helpmenu == "exit":
    p.clear()
    p.write("Hope you play soon!")

说到这里,我建议你在 helpmenu = p.next() 之后插入一个 print 语句,这样你就可以在该行之后检查 helpmenu 的值。

我在您的代码中注意到的其他内容:

  • 您可以使用难度.lower() == "easy"(和 helpmenu 类似),这样您就不必编写所有可能的选择。
  • 考虑在 if 块中添加 else 语句。如果没有匹配的 if/elif 块,您的代码将不会做任何事情,并且用户不会知道他们做错了什么。
  • 在这里定义一个类是没有用的。您可以自己编写 mainmen() 函数,它会起作用。您无需为此用途创建对象。

我希望这会有所帮助,如果您需要任何进一步的帮助,请告诉我,我将尽我所能提供帮助。

于 2013-08-04T18:57:01.123 回答
0

您必须将类实例化为一个对象:

menu = mainmenu()
menu.mainmen()
于 2013-08-04T18:09:26.073 回答