修复函数中的错误消息

时间:2018-08-26 04:09:58

标签: python python-3.x function

所以我用一个函数尝试几次打印消息,但是我不断收到相同的错误消息,说我需要定义该函数,但是我确实有,我真的不知道如何解决它,因为我尝试过通过action_message_header()定义它,但是它不起作用,我尝试搜索它,但是什么也没找到。

结果代码应为:

Hello the four games avaliable are:
0   Mario Cart
1   Minecraft
2   Angry Birds
3   Grabd Theft Auto
What number game do you want? 1
You have chosen Minecraft

What is the level you would like to play at? 1
Begginer 

#                                                      #
########################################################
#################### ACTION MESSAGE ####################
########################################################
#                                                      #
Play Minecraft at Beginner 
play Minecraft at Beginner
#                                                      #
########################################################
#################### ACTION MESSAGE ####################
########################################################
#                                                      #

当前输出为:

Hello the four games avaliable are:
0   Mario Cart
1   Minecraft
2   Angry Birds
3   Grabd Theft Auto
What number game do you want? 2
You have chosen Angry Birds

What is the level you would like to play at? 2
Begginer 
Traceback (most recent call last):
  File "/Users/megan/Desktop/Digital/Python/mock 1.py", line 86, in <module>
    main()
  File "/Users/megan/Desktop/Digital/Python/mock 1.py", line 84, in main
    actualmessage()
  File "/Users/megan/Desktop/Digital/Python/mock 1.py", line 71, in actualmessage
    print (action_message_header)
NameError: name 'action_message_header' is not defined

我使用的代码是:

#Ask user what game they would like to play
def game () :
    global gametype,gamelist
    gamelist = ["Mario Cart","Minecraft","Angry Birds","Grabd Theft Auto"]
    gamecount = 0
    LENGTH = len(gamelist)
    print ("Hello the four games avaliable are:")

    while gamecount < LENGTH:
        print (gamecount," ",gamelist[gamecount])
        gamecount = gamecount + 1

    while True:
        try:
            gamenum = int(input("What number game do you want? "))
            if 0 <= gamenum <= LENGTH:
                gametype = gamenum
                return gamenum
            print ("Please enter a number between 0 and",gamecount - 1,)
            print ("")

        except ValueError:
                print ("Please enter a valid value " )
                print ("")

gamenum = game ()
print ("You have chosen",gamelist[gamenum],)
print ("")

#Ask game level
def number():
    while True:
        try:
            Level = int(input("What is the level you would like to play at? "))

            if Level >= 1 and Level <= 25:
                print ("Begginer ")
                return ("Beginner")

            elif Level >=26 and Level <=75:
                print ("Intermediate")
                return ("Intermediate")

            elif Level >=76 and Level <=100:
                print ("Advanced")
                return ("Advanced")
            else:
                print("Out Of range(1-100): Please enter a valid number")
                print ("")

        except ValueError:
            print("Please enter a valid number")
            print ("")


#Create a subroutine to print out the action message
def printmessage ():
    action_message_header = """
    ("#                                                      #")
    ("########################################################")
    ("#################### ACTION MESSAGE ####################")
    ("########################################################")
    ("#                                                      #")
    """

def actualmessage ():
    global stationtype, num
    actual_message_header = ("Play "+gamelist[gamenum]+" at"+num)


    print (action_message_header)
    print (actual_message_header)
    print (actual_message_header)
    print (action_message_header)

#This is to let the program work
def main ():
    global num
    num = number()

    printmessage ()
    actualmessage()

main()

3 个答案:

答案 0 :(得分:0)

该函数不返回或打印任何内容。对于该输出,使函数按如下所示打印变量内容:

#Create a subroutine to print out the action message
def printmessage ():
    action_message_header = """
#                                                      #
########################################################
#################### ACTION MESSAGE ####################
########################################################
#                                                      #
    """
    print(action_message_header)    # print header

def actualmessage ():
    global stationtype, num
    actual_message_header = "Play "+gamelist[gamenum]+" at"+num
    print(actual_message_header)    # print actual_message_header 


#This is to let the program work
def main ():
    global num
    num = number()

    printmessage ()
    actualmessage()
    actualmessage()
    printmessage ()

main()

运行:

Hello the four games avaliable are:
0   Mario Cart
1   Minecraft
2   Angry Birds
3   Grabd Theft Auto
What number game do you want? 1
You have chosen Minecraft

What is the level you would like to play at? 2
Begginer 

#                                                      #
########################################################
#################### ACTION MESSAGE ####################
########################################################
#                                                      #

Play Minecraft atBeginner
Play Minecraft atBeginner

#                                                      #
########################################################
#################### ACTION MESSAGE ####################
########################################################
#                                                      #

答案 1 :(得分:0)

尝试这种方式

#Ask user what game they would like to play
def game () :
    global gametype,gamelist
    gamelist = ["Mario Cart","Minecraft","Angry Birds","Grabd Theft Auto"]
    gamecount = 0
    LENGTH = len(gamelist)
    print ("Hello the four games avaliable are:")

    while gamecount < LENGTH:
        print (gamecount," ",gamelist[gamecount])
        gamecount = gamecount + 1

    while True:
        try:
            gamenum = int(input("What number game do you want? "))
            if 0 <= gamenum <= LENGTH:
                gametype = gamenum
                return gamenum
            print ("Please enter a number between 0 and",gamecount - 1,)
            print ("")
            return gamenum

        except ValueError:
                print ("Please enter a valid value " )
                print ("")

gamenum = game ()
print ("You have chosen",gamelist[gamenum],)
print ("")

#Ask game level
def number():
    while True:
        try:
            Level = int(input("What is the level you would like to play at? "))

            if Level >= 1 and Level <= 25:
                print ("Begginer ")
                return ("Beginner")

            elif Level >=26 and Level <=75:
                print ("Intermediate")
                return ("Intermediate")

            elif Level >=76 and Level <=100:
                print ("Advanced")
                return ("Advanced")
            else:
                print("Out Of range(1-100): Please enter a valid number")
                print ("")

        except ValueError:
            print("Please enter a valid number")
            print ("")


#Create a subroutine to print out the action message
def printmessage ():
    print("""
    ("#                                                      #")
    ("########################################################")
    ("#################### ACTION MESSAGE ####################")
    ("########################################################")
    ("#                                                      #")
    """)

def actualmessage ():
    global stationtype, num
    actual_message_header = ("Play "+gamelist[gamenum]+" at"+num)


    print (actual_message_header)
    printmessage()

#This is to let the program work
def main ():
    global num
    num = number()

    printmessage ()
    actualmessage()

main()

输出:

Hello the four games avaliable are:
0   Mario Cart
1   Minecraft
2   Angry Birds
3   Grabd Theft Auto
What number game do you want? 1
You have chosen Minecraft

What is the level you would like to play at? 1
Begginer 

    ("#                                                      #")
    ("########################################################")
    ("#################### ACTION MESSAGE ####################")
    ("########################################################")
    ("#                                                      #")

Play Minecraft atBeginner

    ("#                                                      #")
    ("########################################################")
    ("#################### ACTION MESSAGE ####################")
    ("########################################################")
    ("# 

答案 2 :(得分:0)

首先请参见此处,您需要将变量action_message_header声明为global action_message_header,然后按如下所示更改其值:

def game () :
global gametype,gamelist
gamelist = ["Mario Cart","Minecraft","Angry Birds","Grabd Theft Auto"]
gamecount = 0
LENGTH = len(gamelist)
print ("Hello the four games avaliable are:")

while gamecount < LENGTH:
    print (gamecount," ",gamelist[gamecount])
    gamecount = gamecount + 1

while True:
    try:
        gamenum = int(input("What number game do you want? "))
        if 0 <= gamenum <= LENGTH:
            gametype = gamenum
            return gamenum
        print ("Please enter a number between 0 and",gamecount - 1,)
        print ("")

    except ValueError:
            print ("Please enter a valid value " )
            print ("")

gamenum = game ()
print ("You have chosen",gamelist[gamenum],)
print ("")

#Ask game level
def number():
  while True:
    try:
        Level = int(input("What is the level you would like to play at? "))

        if Level >= 1 and Level <= 25:
            print ("Begginer ")
            return ("Beginner")

        elif Level >=26 and Level <=75:
            print ("Intermediate")
            return ("Intermediate")

        elif Level >=76 and Level <=100:
            print ("Advanced")
            return ("Advanced")
        else:
            print("Out Of range(1-100): Please enter a valid number")
            print ("")

    except ValueError:
        print("Please enter a valid number")
        print ("")


#Create a subroutine to print out the action message
def printmessage ():
    global action_message_header
    action_message_header = "#                                                      
    #\n" + "########################################################\n" + 
    "#################### ACTION MESSAGE ####################\n" + 
    "########################################################\n" + "#                                                      
    #\n"

def actualmessage ():
    global stationtype, num
    actual_message_header = ("Play "+gamelist[gamenum]+" at"+num)


    print (action_message_header)
    print (actual_message_header)
    print (actual_message_header)
    print (action_message_header)

#This is to let the program work
def main ():
   global num
   num = number()

   printmessage ()
   actualmessage()

main()

它给出如下输出:

Hello the four games avaliable are:
0   Mario Cart
1   Minecraft
2   Angry Birds
3   Grabd Theft Auto
What number game do you want? 2
You have chosen Angry Birds

What is the level you would like to play at? 45
Intermediate
#                                                      #
########################################################
#################### ACTION MESSAGE ####################
########################################################
#                                                      #

Play Angry Birds atIntermediate
Play Angry Birds atIntermediate
#                                                      #
########################################################
#################### ACTION MESSAGE ####################
########################################################
#                                                      #