Python:为什么我的Except Block运行时甚至不是真的?

时间:2017-03-17 22:35:27

标签: python exception try-except

我有一个try-except块:

YourSecondPick = input("Second Card:");

try:
    if(int(YourSecondPick) not in YourHand):
        print("\nYou don't have that card.");
        return Main(40);
    else:
        YourTotalPick = int(YourFirstPick) + int(YourSecondPick);
        print("\nYou put down a total of {0}".format(YourTotalPick));
        YourHand.remove(int(YourFirstPick));
        YourHand.remove(int(YourSecondPick));
        YourHand.append(Deck.pop(random.choice(range(len(Deck)))));
        YourHand.append(Deck.pop(random.choice(range(len(Deck)))));
        return Main(45);
except ValueError as e:
    print("\nThe input you entered is not a number");
    print(e);
    return Main(40);

如果您手中输入两张牌,例如15 30,则应该添加这些牌的总和并将它们放入新的列表中。然后它将从您的卡中删除卡,并再给你两张来自牌组的牌。一旦发生这种情况,它应该返回到下一个块:

if(line == 45):
    while(len(Deck) / NumberOfPlayers >= 1):
            if(OneCard and Computer1 == True):
                Computer1Pick = (random.choice(Computer1Hand));
                print("Computer1 put down a {0}".format(Computer1Pick));
                MaxList.append(Computer1Pick);
                Computer1Hand.remove(int(Computer1Pick));
                Computer1Hand.append(Deck.pop(random.choice(range(len(Deck)))));

            elif(TwoCards and Computer1 == True):
                Computer1FirstPick = (random.choice(Computer1Hand));
                Computer1SecondPick = (random.choice(Computer1Hand));
                Computer1TotalPick = int(Computer1FirstPick) + int(Computer1SecondPick);
                print("Computer1 put down a total of {0}".format(Computer1TotalPick));
                MaxList.append(Computer1TotalPick);
                Computer1Hand.remove(int(Computer1FirstPick));
                Computer1Hand.remove(int(Computer1SecondPick));
                Computer1Hand.append(Deck.pop(random.choice(range(len(Deck)))));
                Computer1Hand.append(Deck.pop(random.choice(range(len(Deck)))));

            else:
                Computer1Pick = None;

            if(OneCard and Computer2 == True):
                Computer2Pick = (random.choice(Computer2Hand));
                print("Computer2 put down a {0}".format(Computer2Pick));
                MaxList.append(Computer2Pick);
                Computer2Hand.remove(int(Computer2Pick));
                Computer2Hand.append(Deck.pop(random.choice(range(len(Deck)))));

            else:
                Computer2Pick = None;

            if(OneCard and Computer3 == True):
                Computer3Pick = (random.choice(Computer3Hand));
                print("Computer3 put down a {0}".format(Computer3Pick));
                MaxList.append(Computer3Pick);
                Computer3Hand.remove(int(Computer3Pick));
                Computer3Hand.append(Deck.pop(random.choice(range(len(Deck)))));

            else:
                Computer3Pick = None;
            return Main(50);

此代码中出现两个问题。一;即使您正确输入数据,try-except块也会始终发布除外。二;即使我放了return Main(45);,下一个代码块也不会发生。我不知道为什么会这样,但我认为这可能与我的代码设置方式有关。如果你能告诉我为什么会这样,以及我应该做些什么来改进这一点,我将非常感激。我不知道究竟究竟是什么造成了什么,但我预感到它是Main(#)部分。以下是我运行时会发生的事情。

Your cards: [48, 50, 39, 83, 73]

Choose a number from your hand or enter a command.

First Card: 48
Second Card: 50

You put down a total of 98
Computer1 put down a total of 164

The input you entered is not a number

max() arg is an empty sequence

Your cards: [39, 83, 73, 18, 17]

Choose a number from your hand or enter a command.

First Card:

即使4850是列表的一部分,它仍然希望保留在try-except块中。如果您在此处无法解决问题,请点击此链接Repl.it Code。这将带您到完整的代码。

1 个答案:

答案 0 :(得分:0)

我忘记将总数添加到列表中。

MaxList.append(int(YourTotalPick));