Issue
I'm very new to Python, and programming all-around. My current project is creating a "bot" that I can message in command prompt or a termninal and play casino games with.
I'm having problems coding blackjack, specifically with the lists and the values of the strings within that list.
Here's what my variables & lists look like right now:
card = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
value = [1,2,3,4,5,6,7,8,9,10] #not sure what you do with this line
Here's how I choose a select a random card:
card1 = card[randint(0,12)]
And here's how I find the sum of the two randomly selected cards:
cardTotal = int(card1) + int(card2)
If I don't use "int(x)" the two numbers don't add, it just puts the two strings together, which makes sense. How do I correct this?
Your previous cards were 7 and 3. The newest dealt card is 7 for a total of 737.
The house's previous total was 7.
The house is dealt another card 4, which comes to a total of 74.
Sorry, you lost.
PS C:\Users\r\Desktop\python>
You were dealt K and 7.
Traceback (most recent call last):
File "c:\Users\r\Desktop\python\CasinoBot.py", line 66, in <module>
cardTotal = int(card1) + int(card2) # Sum of both cards
ValueError: invalid literal for int() with base 10: 'K'
PS C:\Users\r\Desktop\python>
Solution
Not sure the actual value of the cards, so I set ace to 1, jack to 11, queen to 12, and king to 13. Change the values in the dictionary and add other text as you need. This just prints the sum of the two cards. Your problem was that if it chose a card that was not a number it wouldn't work, etc it doesn't make sense to add 'Jack' to 11 and get a number.
import random
card = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
cards = {
'Ace': '1',
'Jack': '11',
'Queen': '12',
'King': '13'
}
card1 = random.choice(card)
if card1 in cards:
card1 = cards[card1]
card2 = random.choice(card)
if card2 in cards:
card2 = cards[card2]
cardTotal = int(card1) + int(card2)
print(cardTotal)
Answered By - qwerteee
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.