Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Casino - Homework
#1
Hi guys,

I have this assignment that I've been working on but my head has been Wall. Was wondering if I could get some help solving it.

Thx in advance.

I basically have to create/define a function that:

1. checks if sum < 21, then print a str
2. checks if sum > 21 and if it is, check if there are 1 or more 11's. If there are then reduce the total sum by 10. Then check the list again for 11's and if there are any reduce the sum by 10 again.
3. if sum is still > 21 and there are no more reductions possible then str
4. if sum == 21 and there's only one 11 and one 10 in the list then str

With the exception of 3 hands that I can't get to continue to reduce, everything else is peachy.

Ok, here's what I have so far:

def get_hand_value(*args):

    if sum(args) <= 21 and args.count(11) == 0:
        return "The value of your hand is {0}".format(sum(args))
    elif sum(args) > 21 and args.count(11) == 1 or args.count(11) > 1:
        return (sum(args) - 10)
    # elif
    elif sum(args) > 21 and args.count(11) == 0:
        return "Busted!"
    else:
        if sum(args) == 21 and args.count(11) == 1 and args.count(10) == 1:
            return "Winner Winner Chicken Dinner!"
#-----------------------------------------------------------------------------

# While testing the execution of your program, I will use the hands below:
hand_1 = get_hand_value(4,4,4,4)
hand_2 = get_hand_value(11,10)
hand_3 = get_hand_value(11,5,6,4,11,4)
hand_4 = get_hand_value(11,5,6,10)
hand_5 = get_hand_value(10,5,7)
hand_6 = get_hand_value(5,6,5,10)
hand_7 = get_hand_value(10,11)
hand_8 = get_hand_value(11,11,11,11,10,10)
hand_9 = get_hand_value(5,6,5,5)

print(hand_1)
print(hand_2)
print(hand_3)
print(hand_4)
print(hand_5)
print(hand_6)
print(hand_7)
print(hand_8)
print(hand_9)
And this is the output:

The value of your hand is 16
Winner Winner Chicken Dinner!
31
22
Busted!
Busted!
Winner Winner Chicken Dinner!

Once again...Thx
Cheers,
54
The value of your hand is 21
Reply
#2
What you're missing is a loop to test for more than one eleven. Here's a suggestion:
def get_hand_value(*args):

	hand_total = sum (args)
	total_elevens = args.count (11)
	while total_elevens > 0 and hand_total > 21 :
		total_elevens -= 1
		hand_total -= 10
	if hand_total  < 21 :
		return "The value of your hand is {0}".format(hand_total)
	elif hand_total > 21 :
		return "Busted!"
	else:
		return "Winner Winner Chicken Dinner!"
#-----------------------------------------------------------------------------
 
# While testing the execution of your program, I will use the hands below:
hand_1 = get_hand_value(4,4,4,4)
hand_2 = get_hand_value(11,10)
hand_3 = get_hand_value(11,5,6,4,11,4)
hand_4 = get_hand_value(11,5,6,10)
hand_5 = get_hand_value(10,5,7)
hand_6 = get_hand_value(5,6,5,10)
hand_7 = get_hand_value(10,11)
hand_8 = get_hand_value(11,11,11,11,10,10)
hand_9 = get_hand_value(5,6,5,5)
 
print(hand_1)
print(hand_2)
print(hand_3)
print(hand_4)
print(hand_5)
print(hand_6)
print(hand_7)
print(hand_8)
print(hand_9)
Output:
The value of your hand is 16 Winner Winner Chicken Dinner! Winner Winner Chicken Dinner! Busted! Busted! Busted! Winner Winner Chicken Dinner! Busted! Winner Winner Chicken Dinner!
Men likes this post
Reply
#3
BashBedlam,

Thank you very much. This works perfectly.

As far as missing a loop is concerned...I think I missed more than that Tongue.

Nevertheless, I understand the structure now. Because initially I created a variable but I kept getting the error variable referenced before assignment.

Once again, thx a bunch.

Cheers,


(Dec-21-2021, 04:13 PM)BashBedlam Wrote: What you're missing is a loop to test for more than one eleven. Here's a suggestion:
def get_hand_value(*args):

	hand_total = sum (args)
	total_elevens = args.count (11)
	while total_elevens > 0 and hand_total > 21 :
		total_elevens -= 1
		hand_total -= 10
	if hand_total  < 21 :
		return "The value of your hand is {0}".format(hand_total)
	elif hand_total > 21 :
		return "Busted!"
	else:
		return "Winner Winner Chicken Dinner!"
#-----------------------------------------------------------------------------
 
# While testing the execution of your program, I will use the hands below:
hand_1 = get_hand_value(4,4,4,4)
hand_2 = get_hand_value(11,10)
hand_3 = get_hand_value(11,5,6,4,11,4)
hand_4 = get_hand_value(11,5,6,10)
hand_5 = get_hand_value(10,5,7)
hand_6 = get_hand_value(5,6,5,10)
hand_7 = get_hand_value(10,11)
hand_8 = get_hand_value(11,11,11,11,10,10)
hand_9 = get_hand_value(5,6,5,5)
 
print(hand_1)
print(hand_2)
print(hand_3)
print(hand_4)
print(hand_5)
print(hand_6)
print(hand_7)
print(hand_8)
print(hand_9)
Output:
The value of your hand is 16 Winner Winner Chicken Dinner! Winner Winner Chicken Dinner! Busted! Busted! Busted! Winner Winner Chicken Dinner! Busted! Winner Winner Chicken Dinner!
Reply
#4
Your error was that you didn't create the proper abstraction. You could solve this problem by changing the value of aces from 11 to 1 and checking the sum.
def handValue(*cards):
    # Initially force all aces to be 11 before we start.
    cards = [11 if card == 1 else card for card in cards]
    # While sum > 21, convert aces from 11 to 1
    while sum(cards) > 21 and len(cards.count(11)) > 0:
        cards[cards.index(11)] = 1
This is a very literal interpretation of your algorithm. It does exactly what is asked. Starting programmers write a lot of code like this because they focus a lot on finding the right commands and using correct syntax and spend very little time thinking about the problem they are trying to solve.

The problem becomes simpler if you stop thinking about an hand of cards and think about the value of the hand and the number of aces.
def handValue(*cards):
    # Initially force all aces to be 11 before we start.
    cards = [11 if card == 1 else card for card in cards]
    value = sum(cards)
    aces = cards.count(11)
    # While value> 21, convert aces from 11 to 1
    while value > 21 and aces > 0:
        value -= 10
        aces -= 1
In this example the abstraction is minor, but I think it still offers a significant improvement. Instead of searching for 11's in the list, converting them to 1's, and summing the hand value, the abstraction has two integer subtractions. This would be a big performance boost if you were computing millions of hands.

Next time you have a programming assignment don't think about how to solve the problem using Python, think about how to solve the problem efficiently. Try to write all the steps in a manner that would make it easy for someone else to follow. When you have a clearly defined solution you can then start programming.
Men likes this post
Reply
#5
Totally agree with your advice.

Like you so rightly said "Next time you have a programming assignment don't think about how to solve the problem using Python, think about how to solve the problem efficiently. Try to write all the steps in a manner that would make it easy for someone else to follow. When you have a clearly defined solution you can then start programming."

Flowcharting then executing...is what I'm working on.

Not trying to make any excuses but I have a teacher who's a deep diver.

Teaches how to swim...then takes you deep sea diving and tells you to swim back up Big Grin.

Cheers,


(Dec-21-2021, 05:33 PM)deanhystad Wrote: Your error was that you didn't create the proper abstraction. You could solve this problem by changing the value of aces from 11 to 1 and checking the sum.
def handValue(*cards):
    # Initially force all aces to be 11 before we start.
    cards = [11 if card == 1 else card for card in cards]
    # While sum > 21, convert aces from 11 to 1
    while sum(cards) > 21 and len(cards.count(11)) > 0:
        cards[cards.index(11)] = 1
This is a very literal interpretation of your algorithm. It does exactly what is asked. Starting programmers write a lot of code like this because they focus a lot on finding the right commands and using correct syntax and spend very little time thinking about the problem they are trying to solve.

The problem becomes simpler if you stop thinking about an hand of cards and think about the value of the hand and the number of aces.
def handValue(*cards):
    # Initially force all aces to be 11 before we start.
    cards = [11 if card == 1 else card for card in cards]
    value = sum(cards)
    aces = cards.count(11)
    # While value> 21, convert aces from 11 to 1
    while value > 21 and aces > 0:
        value -= 10
        aces -= 1
In this example the abstraction is minor, but I think it still offers a significant improvement. Instead of searching for 11's in the list, converting them to 1's, and summing the hand value, the abstraction has two integer subtractions. This would be a big performance boost if you were computing millions of hands.

Next time you have a programming assignment don't think about how to solve the problem using Python, think about how to solve the problem efficiently. Try to write all the steps in a manner that would make it easy for someone else to follow. When you have a clearly defined solution you can then start programming.
Reply
#6
Wow, I wasn't expecting so much help here
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020