如何修复代码中的打印错误

时间:2018-07-31 10:22:01

标签: python

我的代码存在一些我不知道如何解决的问题。

  1. 当我打印出他们选择的游戏和级别的消息时,最后并没有打印出级别(初学者,中级或高级)。它只打印出游戏。我已经将'num'打印出来了,但是我也尝试了'Level'和'number'到位。代码是

    打印(“播放”,游戏列表[游戏类型],“在”,数字)

  2. 在询问用户他们想玩什么游戏时,如果他们输入的数字超出0-3或一个字母,则在最后打印消息时,其代码会进一步细分。这只是在询问什么游戏时,而不是在他们想要游戏的级别时。

任何帮助都将不胜感激。

#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
    print ("Hello",name,"the four games avaliable are:")
    while gamecount < 4:
        print (gamecount," ",gamelist[gamecount])
        gamecount = gamecount + 1
    gametype = int(input("What number game do you want? (Please choose between 0 and 3) "))
    print ( "You have chosen",gamelist[gametype],)
    print ("")

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

            if Level <= 25:
                print ("Begginer ")
                break

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

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

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

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

    print ("")
    print ("#                                                      #")
    print ("########################################################")
    print ("#################### ACTION MESSAGE ####################")
    print ("########################################################")
    print ("#                                                      #")
    print ("Play",gamelist[gametype], "at" ,num)
    print ("#                                                      #")
    print ("########################################################")
    print ("#################### ACTION MESSAGE ####################")
    print ("########################################################")
    print ("#                                                      #")

#This is to let the program work
name = input("What is your name? ")
print ("")

game ()
num = number()
printmessage()

2 个答案:

答案 0 :(得分:0)

在函数def号末尾添加以下代码行:

return Level

答案 1 :(得分:0)

我对更改发表了评论:

#Ask user what game they would like to play
def game (name) :
    global gametype,gamelist
    gamelist = ["Mario Cart","Minecraft","Angry Birds","Grabd Theft Auto"]
    gamecount = 0
    print ("Hello "+name+" the four games avaliable are:")
    while gamecount < 4:
        print (gamecount," ",gamelist[gamecount])
        gamecount = gamecount + 1
    gametype = int(input("What number game do you want? (Please choose between 0 and 3) "))
    print ( "You have chosen "+gamelist[gametype])
    print ("")

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

            if Level <= 25:
                print ("Begginer ")
                return("Begginer ")#changed break with return so it will break and return the selected value

            elif Level >=26 and Level <=75:
                print ("Intermediate")
                return("Intermediate ")#changed break with return so it will break and return the selected value

            elif Level >=76 and Level <=100:
                print ("Advanced")
                return("Advanced ")#changed break with return so it will break and return the selected value
            else:
                print("Out Of range(1-100): Please enter a valid number:")

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

#Create a subroutine to print out the action message
def printmessage (num):

    print ("")
    print ("#                                                      #")
    print ("########################################################")
    print ("#################### ACTION MESSAGE ####################")
    print ("########################################################")
    print ("#                                                      #")
    print ("Play "+gamelist[gametype]+" at "+num)
    print ("#                                                      #")
    print ("########################################################")
    print ("#################### ACTION MESSAGE ####################")
    print ("########################################################")
    print ("#                                                      #")

#This is to let the program work
name = raw_input("What is your name? ") # I changed input with raw_input because you are sending a string not integer
print ("")

game (name)
num = number()
printmessage(num)

这是输出:

What is your name? Test

Hello Test the four games avaliable are:
(0, ' ', 'Mario Cart')
(1, ' ', 'Minecraft')
(2, ' ', 'Angry Birds')
(3, ' ', 'Grabd Theft Auto')
What number game do you want? (Please choose between 0 and 3) 0
You have chosen Mario Cart

What is the level you would like to play at? 1000
Out Of range(1-100): Please enter a valid number:
What is the level you would like to play at? 1
Begginer 

#                                                      #
########################################################
#################### ACTION MESSAGE ####################
########################################################
#                                                      #
Play Mario Cart at Begginer 
#                                                      #
########################################################
#################### ACTION MESSAGE ####################
########################################################
#