While writing a Blackjack script, I've come across some confusion on how I should use 'if', 'elif', and 'else' statements. I looked at most of the posts on the subject here, googled it, but am still confused. . .I did learn that if 'elif' is used instead of repeating 'if' statements, the code will short circuit when the (or one of the) 'elif' statements evaluates to True. This has actually confused me more (although I understand the concept of what happens when using 'elif' and shortcircuiting). The first 5 'if' statement illustrates this. Had I used 'elif' instead of 'if', the code may never reach the last condition if both player and dealer hit 21. . .After this though, it seems I could have used 'elif' statements or just leave it the way it is. . .So, my question is, did I use them correctly in the rest of main()? If not, how would you do it? Thank you very much.
# 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()