# Conditional statements in Hot 18 card game [closed]

How can I write this shorter (while keeping the same functionality and outputs to console)?

def review():
v1 = value_of_current_cards(hand_one)
v2 = value_of_current_cards(hand_two)
v3 = value_of_current_cards(hand_three)
vd = value_of_current_cards(hand_dealer)
if number_of_hands > 0:
print ""
print "Recap of that round"
if v1 < 18:
print ""
print "First hand: You stayed on %s" % (v1)
if vd > 18:
print "*You won on First Hand*" "  (Dealer busted with %s)" % (vd)
elif vd < 18:
print "            Dealer stayed on %s" % (vd)
if vd > v1:
print "-You lost on First Hand-" " (Dealer was closer to 18)"
elif vd < v1:
print "*You won on First Hand*" " (You were closer to 18 than dealer)"
elif vd == v1:
print "*You tied on First Hand*" " (You and dealer both had %s)" % (vd)
elif v1 == 18:
print ""
print "First hand: You HIT HOT 18!"
if v1 > vd:
print "*You won on First Hand*" "  (Dealer only had %s)" % (vd)
elif v1 == vd:
print "*You tied on First Hand*" "  (You and dealer both had %s)" % (vd)
elif v1 < vd:
print "*You won on First Hand*" "  (Dealer busted with %s)" % (vd)
elif v1 > 18:
print ""
print "-You lost on First Hand- " "(You had %s, so you went over 18)" % (v1)
if number_of_hands > 1:
if v2 < 18:
print ""
print "Second hand: You stayed on %s" % (v2)
if vd > 18:
print "*You won on Second Hand*" "   (Dealer busted with %s)" % (vd)
elif vd < 18:
print "             Dealer stayed on %s" % (vd)
if vd > v2:
print "-You lost on Second Hand-" " (Dealer was closer to 18)"
elif vd < v2:
print "*You won on Second Hand*" " (You were closer to 18 than dealer)"
elif vd == v2:
print "*You tied on Second Hand*" " (You and dealer both had %s)" % (vd)
elif v2 == 18:
print ""
print "Second hand: You HIT HOT 18!"
if v2 > vd:
print "*You won on Second Hand*" "  (Dealer only had %s)" % (vd)
elif v2 == vd:
print "*You tied on Second Hand*" "  (You and dealer both had %s)" % (vd)
elif v2 < vd:
print "*You won on Second Hand*" "  (Dealer busted with %s)" % (vd)
elif v2 > 18:
print ""
print "-You lost on Second Hand-" " (You had %s, so you went over 18)" % (v2)
if number_of_hands > 2:
if v3 < 18:
print ""
print "Third hand: You stayed on %s" % (v3)
if vd > 18:
print "*You won on First Hand*" "    (Dealer busted with %s)" % (vd)
elif vd < 18:
print "            Dealer stayed on %s" % (vd)
if vd > v3:
print "-You lost on Third Hand-" " (Dealer was closer to 18)"
elif vd < v3:
print "*You won on Third Hand*" " (You were closer to 18 than dealer)"
elif vd == v3:
print "*You tied on Third Hand*" " (You and dealer both had %s)" % (vd)
elif v3 == 18:
print ""
print "Third hand: You HIT HOT 18!"
if v3 > vd:
print "*You won on Third Hand*" "  (Dealer only had %s)" % (vd)
elif v3 == vd:
print "*You tied on Third Hand*" "  (You and dealer both had %s)" % (vd)
elif v3 < vd:
print "*You won on Third Hand*" "  (Dealer busted with %s)" % (vd)
elif v3 > 18:
print ""
print "-You lost on Third Hand-" " (You had %s, so you went over 18)" % (v3)

• You forgot to handle the case v1 < 18 and vd == 18 – Janne Karila Dec 11 '13 at 14:31
• This question lacks any indication of what the code is intended to achieve. To help reviewers give you better answers, please add sufficient context to your question, including a title that summarises the purpose of the code. We want to know why much more than how. The more you tell us about what your code is for, the easier it will be for reviewers to help you. The title needs an edit to simply state the task. – Toby Speight Mar 11 '19 at 16:25

This rearrangement of the if statements avoids repetition of print statements:

print
print "Recap of that round"
print
if v1 < 18:
print "First hand: You stayed on %s" % (v1)
if vd < 18:
print "            Dealer stayed on %s" % (vd)
elif v1 == 18:
print "First hand: You HIT HOT 18!"

if v1 > 18:
print "-You lost on First Hand- " "(You had %s, so you went over 18)" % (v1)
elif v1 < vd <= 18:
print "-You lost on First Hand-" " (Dealer was closer to 18)"
elif v1 == vd:
print "*You tied on First Hand*" "  (You and dealer both had %s)" % (vd)
else:
print "*You won on First Hand*",
if vd > 18:
print "  (Dealer busted with %s)" % (vd)
elif v1 == 18:
print "  (Dealer only had %s)" % (vd)
else:
print " (You were closer to 18 than dealer)"


Then, use string formatting to insert "First hand" from a variable.

You could use a loop like this to call a function that prints the review of a single round:

rounds = (("First hand", hand_one),
("Second hand", hand_two),
("Third hand", hand_three))
for name, hand in rounds[:number_of_hands]:
review_round(name, hand, hand_dealer)


You can/shuold extract repeating code, e.g. into into functions (see my example)

example:

def evaluate_hand(value_of_hand, hand_no, hand_of_dealer):
"""
Evalutates  ....

@param value_of_hand the value for the current hand.
@param hand_no the number of the hand. Have to be a value of the interval [1,4]
@param hand_of_dealer contains the value of the dealers hand
"""
hand_to_str_dispatcher = {1: "First hand", 2: "Second hand", 3: "Thrid hand", 4: "Fourth hand"}
print "Recap of that round"
if value_of_hand < 18:
print ""
print "{0}: You stayed on {1}".format(hand_to_str_dispatcher[hand_no],value_of_hand)
if hand_of_dealer > 18:
print "*You won on First Hand*" "  (Dealer busted with {0})".format(hand_of_dealer)
elif hand_of_dealer < 18:
print "            Dealer stayed on {0}".format(hand_of_dealer)
if hand_of_dealer > value_of_hand:
print "-You lost on {0}-  (Dealer was closer to 18)".format(hand_to_str_dispatcher[hand_no])
elif hand_of_dealer < value_of_hand:
print "*You won on {0}* (You were closer to 18 than dealer)".format(hand_to_str_dispatcher[hand_no])

elif hand_of_dealer == value_of_hand:
print "*You tied on {0}* (You and dealer both had {1})".format(hand_to_str_dispatcher[hand_no], hand_of_dealer)
# .....
# adjust the subsequent code accordingly

def recap_round():
hand_of_dealer = 10
v1 = 19
v2 = 2
v3 = 20
v4 = 22
hands = [v1, v2, v3, v4]
print "Recap of that round"
for i in range(0, len(hands)):
evaluate_hand(hands[i], i + 1, hand_of_dealer)


Run the recap_round function in order to see how the example works. If you try to understand the used concepts and methods you probably get a better idea how to structure you're code in the future.