我需要使'printmessage'函数起作用,以便打印动作消息,以便可以重复使用它,而不必多次使用打印。任何有帮助的事情都会很棒。我知道这很麻烦,但是如果可能的话,代码可以保持尽可能相似。现在,当我使用我的新代码时,会出现一条追溯错误消息
错误消息是:
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
Traceback (most recent call last):
File "/Users/megan/Desktop/Digital/Python/mock 1.py", line 87, in <module>
main()
File "/Users/megan/Desktop/Digital/Python/mock 1.py", line 85, in main
actualmessage()
File "/Users/megan/Desktop/Digital/Python/mock 1.py", line 72, 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 (action_message_header)
print (action_message_header)
print (actual_message_header)
print (actual_message_header)
print (actual_message_header)
#This is to let the program work
def main ():
global num
num = number()
printmessage ()
actualmessage()
main()
答案 0 :(得分:0)
您正在打印一个不返回任何值的函数。您可以通过将printmessage
添加到下面的示例中来更改您的return action_message_header
函数以返回要打印的值,然后继续操作
def printmessage ():
action_message_header = """
("# #")
("########################################################")
("#################### ACTION MESSAGE ####################")
("########################################################")
("# #")
"""
return action_message_header
或用以下内容替换您的printmessage
函数:
def printmessage ():
action_message_header = """
("# #")
("########################################################")
("#################### ACTION MESSAGE ####################")
("########################################################")
("# #")
"""
print(action_message_header)
并调用printmessage()
而不是打印函数。
当代码中包含#
时,应使用三引号将它们全部读取为一个变量。