Reputation: 1
I am new to programming and my brain is just not working (I'm nearing the end of Spring Break).
Please bear in mind that we are not using functions in this, that's next chapter. So my professor just wants if elif else.
Here's the instructions:
Write a program that will ask the user to input the value of two cards. The valid values are A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, or K.
If the player is dealt two aces or two eights, print "Split"
If the value of the player's cards adds up to 21, print "Blackjack!"
If the value of the player's cards adds up to 17 - 20, print "Stay". Exception: if the total is 17 and one of the cards is an ace, print "Hit"
If the value of the player's cards adds up to less than or equal to 16, print "Hit"
So far this is all I have:
User input and variables
card1 = int(input('Card one:\n'))
card2 = int(input('Card two:\n'))
total = 0
if card1 == "J" or card1 == "Q" or card1 == "K":
total += 10
elif card1 == "A"
total += 11
What should I do next, what direction should I go? I have just hit a wall with logic.
Upvotes: 0
Views: 763
Reputation: 886
Here it goes:
card1 = input()
card2 = input()
l = ['A','2','3','4','5','6','7','8','9','J','Q','K']
if card1 in l and card2 in l:
total = (l.index(card1) + 1) + (l.index(card2) + 1)
if (card1 == 'A' and card2 == 'A') or (card1 == '8' and card2 == '8'):
print('Split!')
elif total == 21:
print('Blackjack!')
elif total >= 17 and total <= 20:
if total == 17 and (card1 == 'A' or card2 == 'A'):
print("Hit")
else:
print("Stay")
else:
print("Hit")
else:
print("Invalid Input")
The following are some details to keep in mind.
Since we have predefined set of inputs we will use it to valid the input string. The first if condition does the validation check of the user input.
Once we know that the input is valid next we can calculate total
of the both the input cards using the indices of list l
. Now that we have card1
, card2
and total
with us, we can then start coding our requirements.
The next if, elif and else
block cover the logic mentioned in the question:
If the player is dealt two aces or two eights, print "Split"
If the value of the player's cards adds up to 21, print "Blackjack!"
If the value of the player's cards adds up to 17 - 20, print "Stay". Exception: if the total is 17 and one of the cards is an ace, print "Hit"
If the value of the player's cards adds up to less than or equal to 16, print "Hit"
Upvotes: 1
Reputation: 285
There are several ways to accomplish this. Have you learned about dictionaries yet? That is one way to assign values to your non numeric cards. Otherwise, the way you do that works.
From there, you'll need a series or if checks, such as:
if (card1 == 'A' and card2 == 'A') or (card1 == 8 and card2 == 8):
print('Split!')
if total == 21:
print('Blackjack!')
Upvotes: 0