【发布时间】:2013-02-27 06:37:08
【问题描述】:
在编写二十一点脚本时,我遇到了一些关于如何使用“if”、“elif”和“else”语句的困惑。我在这里查看了有关该主题的大多数帖子,用谷歌搜索了它,但仍然感到困惑。 . .我确实了解到,如果使用“elif”而不是重复“if”语句,则当“elif”语句(或其中一个)评估为 True 时,代码将短路。这实际上让我更加困惑(尽管我理解使用“elif”和短路时会发生什么的概念)。第一个 5 'if' 语句说明了这一点。如果我使用 'elif' 而不是 'if',如果玩家和庄家都击中 21,代码可能永远不会达到最后一个条件。。 . 不过,在此之后,似乎我可以使用“elif”语句,或者保持原样。 . .所以,我的问题是,我在 main() 的其余部分是否正确使用了它们?如果没有,你会怎么做?非常感谢。
# current working version - - - 02/26/2013
# Notes: Nees to fix Ace problem. Ace can be 11 or 1.
import random
import os
def main():
print "Welcome To Python Blackjack. [H] Is For A Hit, [S] Is To Stand, [Q] To Quit.\n"
c = "" # Hit, Stand or Quit Variable.
player = deal_cards() # deal player
dealer = deal_cards() # deal dealer
print "< ---- Player Hand ---->"
print "Player Hand: ", player
print "Total Player Hand: ", total_hand(player)
print
print "< ---- Dealer Hand ---->"
print "Dealer Hand: ", dealer
print "Total Dealer Hand: ", total_hand(dealer)
print
if (total_hand(player) == 21):
print "BLACKJACK! YOU WIN!"
message()
if (total_hand(player) > 21):
print "BUSTED! You Lose"
message()
if (total_hand(dealer) == 21):
print "BLACKJACK! Sorry You Lose! Dealer Wins" # must use if statements because elif would fail to reach the tie line.
message()
if (total_hand(dealer) > 21):
print "Dealer Busted! You Win!"
message()
if (total_hand(player) == 21) and (total_hand(dealer) == 21): # must use if statements because elif would fail to reach this line.
print "Player And Dealer Tie! Game Goes To Dealer"
message()
while (c != "q"):
c = raw_input("[H]it [S]tand [Q]uit: ").lower()
if (c == "h"):
hit(player)
print ""
print "Your Cards Are Now: ",player
print "Total For Player Is: ",total_hand(player)
if (total_hand(player) == 21):
print "BLACKJACK! You Win!"
message()
if (total_hand(player) > 21):
print "BUSTED! Sorry, You Lose."
message()
if (total_hand(dealer) == 21):
print "BLACKJACK! Sorry You Lose! Dealer Wins."
message()
if (total_hand(dealer) > 21):
print "Dealer Busted! You Win!\n"
message()
if (total_hand(dealer) <= 17):
hit(dealer)
print "\nThe Dealer Takes A Card", dealer
print "For A Total Of: ", total_hand(dealer)
if (total_hand(dealer) == 21):
print "BLACKJACK! Sorry You Lose! Dealer Wins.\n"
message()
if (total_hand(dealer) > 21):
print "Dealer Busted! You Win!\n"
message()
elif (c == "s"):
if (total_hand(dealer) <= 17):
hit(dealer)
print "The Dealer Takes A Card", dealer
print "For A Total Of: ", total_hand(dealer)
if (total_hand(dealer) == 21):
print "BLACKJACK! Dealer Wins.\n"
message()
if (total_hand(dealer) > 21):
print "Dealer Busted! You Win!\n"
message()
if (total_hand(dealer) >= total_hand(player)):
print "Sorry, You Lose. Dealer Wins With A Tie\n"
message()
if (total_hand(player) > total_hand(dealer)):
print "You Win With The Best Hand!\n"
message()
if (total_hand(player) > total_hand(dealer)):
print "You Win With The Best Hand!\n"
message()
if (total_hand(dealer) > total_hand(player)):
print "Sorry, You Lose. Dealer Wins\n"
message()
else:
if (c == "q"):
message()
else:
print "Invalid Choice. . .To Quit, Press [Q]"
def deal_cards():
random1 = random.randint(1,11)
random2 = random.randint(1,11)
hand = [random1, random2]
return hand
def hit(hand):
newCard = random.randint(1,11)
hand.append(newCard)
return hand
def total_hand(hand):
total = sum(hand)
return total
def message():
again = raw_input("Do You Want To Play Again? [Y] For Yes - Press Any Key To Quit: ").lower()
if "y" in again:
main()
else:
print "Thanks For Playing"
os._exit(1)
# main
if __name__ == '__main__':
main()
【问题讨论】:
-
Python 的
elif等价于else: if。也就是说,如果达到了你之前的条件,它甚至不会检查下一个条件。如果您希望某些条件优先于其他条件,则需要先列出它们! -
谢谢。 .我明白你在说什么,我想我什至在我的问题中提到了这一点。我遇到的问题是,您会像我一样使用多个 if 语句,还是将其更改为 if。 .elif,确保首先列出更高优先级的代码?再次感谢您的帮助!
-
我认为一些答案已经说明了这一点,但是如果您希望同时触发多个
if语句,则应该使用多个语句。如果只发生一次,请使用if/elif/else。我认为您没有在代码中看到通常的行为,因为您已经通过调用message来编写它,该调用将永远不会返回。这通常是糟糕的编码风格(循环会更好)。 -
我明白你在说什么。这正是我一直在寻找的答案。 .再次感谢。
标签: python if-statement