'How to convert 2-9 from strings to numeric
I'm making a Blackjack game but to score I have to make it so that it converts 2-9 from strings to numeric but I can't seem to make it work. I'm trying to score the cards based on their given value but I don't know how to convert a string to a number and using my code it just scores everything as 10 and I wanna make code it in a way that it'll score 2-9 as their respective number but I also need it to score the Ace card as 1 or 11.
import random
from colorama import Fore, Back, Style
suits = (Fore.BLACK + '\u2660'+Fore.BLACK,
Fore.BLACK + '\u2663'+Fore.BLACK,
Fore.RED + '\u2665'+Fore.BLACK,
Fore.RED + '\u2666'+Fore.BLACK)
faces = ('A','K','Q','J','T','9','8','7','6','5','4','3','2')
deck = []
for face in faces:
for suit in suits:
deck.append("["+face + suit+"]")
print(Fore.GREEN)
print('''
___ _ _ _ _
| _ ) |__ _ __| |__ _ | |__ _ __| |__
| _ \ / _ / _| / / | || / _ / _| / /
|___/_\__,_\__|_\_\ \__/\__,_\__|_\_\\
''')
print(Fore.RESET)
card = deck[0]
for index in range (1, len(deck)+1 ):
print(deck[index-1], end=" ")
if index % 4 == 0:
print()
random.shuffle(deck)
card1 = deck.pop()
print('card1 =', card1)
card2 = deck.pop()
print('card2 =', card2)
def score(card1, card2):
valueOne = card1
valueTwo = card2
if valueOne.isnumeric():
valueOne = int(valueOne)
else: valueOne = 10
if valueTwo.isnumeric():
valueTwo = int(valueTwo)
else:
valueTwo = 10
return valueOne + valueTwo
print("{} {} score {}".format(card1,card2, score(card1,card2)))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
