关于python:修复try和except中的无效语法错误

Fixing invalid syntax errors in try and except

当我使用此代码时,我的目标是尝试确保任何输入都不会破坏代码,比如字母或数字,而不是0-3之间。但是当我使用这个代码时,整个列表不会出现。我该如何解决这个问题?

输出应该如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
Hello Megan the four games avaliable are:
0   Mario Cart
1   Minecraft
2   Angry Birds
3   Grabd Theft Auto
What number game do you want? h
Please choose a valid number

What number game do you want? 23
Please choose a number between 0 and 3

What number game do you want? 1
You have chosen Minecraft

相反,输出是

1
2
3
4
5
6
Hello the four games avaliable are:
0   Mario Cart
What number game do you want? 2
1   Minecraft
What number game do you want? h
Please enter a valid value

我使用的代码是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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 the four games avaliable are:")
    while gamecount < 4:
        print (gamecount,"",gamelist[gamecount])
        gamecount = gamecount + 1


        try:
            gametype = int(input("What number game do you want?"))

            while gametype <0 or gametype >3:
                print (" Please enter a value between 0 and 3")

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

                return gametype

game ()

我还尝试了另一种方法,在"try"之前使用"while-true",但程序说这是无效语法。

第二次尝试

我已经使用了这段新代码,但它不会让我运行代码,因为它说当我输入while true时语法无效,true以红色突出显示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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 the four games avaliable are:")

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

    while True:
        try:
            gametype = int(input("What number game do you want?"))

            if 0 <= gametype <= 3 :
                return game
            print ("Please enter a value between 0 and 3")

        except ValueError:
                print ("Please enter whole number from 0 to 3" )
                return game

game ()


我想你想要这样的东西:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
gamelist = ["Mario Cart","Minecraft","Angry Birds","Grand Theft Auto"]

def game(name):
   """ Ask user what game they would like to play"""
    print ("Hello, {}, the four available games are:".format(name))
    for gamenum, gamename in enumerate(gamelist):
        print(gamenum,":", gamename)

    while True:
        try:
            gamenum = int(input("What number game do you want?"))
            if 0 <= gamenum <= 3:
                return gamenum

            print("Please enter a value between 0 and 3")
        except ValueError:
            print ("Please enter a whole number from 0 to 3")

name = input("What's your name?")
gamenum = game(name)
print("You chose", gamelist[gamenum])

演示输出

1
2
3
4
5
6
7
8
9
10
11
12
What's your name? Megan
Hello, Megan, the four available games are:
0 : Mario Cart
1 : Minecraft
2 : Angry Birds
3 : Grand Theft Auto
What number game do you want? 4
Please enter a value between 0 and 3
What number game do you want? Minecraft
Please enter a whole number from 0 to 3
What number game do you want? 2
You chose Angry Birds

我对您的代码所做的主要更改是将try.. except的东西放在while True块中,所以我们一直要求输入,直到得到有效的东西为止。我还使用enumerate打印每个游戏及其编号。这比你的while gamecount < 4:环更整洁。

如果您必须使用while循环打印游戏列表,那么您可以这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
gamelist = ["Mario Cart","Minecraft","Angry Birds","Grand Theft Auto"]

def game(name):
   """ Ask user what game they would like to play"""
    print ("Hello, {}, the four available games are:".format(name))
    gamenum = 0
    while gamenum < len(gamelist):
        print(gamenum,":", gamelist[gamenum])
        gamenum += 1

    while True:
        try:
            gamenum = int(input("What number game do you want?"))
            if 0 <= gamenum <= 3:
                return gamenum

            print("Please enter a value between 0 and 3")
        except ValueError:
            print ("Please enter a whole number from 0 to 3")

name = input("What's your name?")
gamenum = game(name)
print("You chose", gamelist[gamenum])

我把gamelist列为一个全局列表,这样我们就可以在game功能之外访问它。我们不需要在函数中使用global语句,因为我们不更改gamelist。一般来说,应该避免使用global语句。