【发布时间】:2017-03-17 22:35:27
【问题描述】:
我有一个 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 块也会始终发布 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:
即使48 和50 不在列表中,它仍然希望留在try-except 块中。如果在这里找不到问题,请单击此链接Repl.it Code。这将带您查看完整代码。
【问题讨论】:
-
你真的应该尽量保持
try except尽可能少地包装代码。 -
“一个;即使您输入的数据不正确,try-except 块也将始终发布 except”——此时 应该 输入
except案例。 -
@juanpa.arrivillaga 你认为他们可以单独通过try catch块找到问题吗?在代码方面,我非常谨慎。我想确保人们有足够的信息来解决问题。我可能会删除,但我还不确定。
-
另外,如果你只想转储一堆代码,你至少应该尝试坚持 Python 风格的约定,例如
snake_case中的变量名而不是UpperCase(在 Python 中用于类名)。不要用多余的括号括起你的条件,不要用分号结束行......没有人想阅读和破译这个。 -
所有这些
return Main(whatever)行到底是怎么回事?看起来你是用goto写的,然后尝试将其转换为尾递归。
标签: python exception try-except