In: Computer Science
Trying to score a hand of blackjack in this python code but my loop is consistently outputting (13,1) which makes me think that something is wrong with my loop.
Could someone help me with this code?:
import random
cards = [random.randint(1,13) for i in range(0,2)]
#initialize hand with two random cards
def get_card():
#this function will randomly return a card with the value between 1
and 13
return random.randint(1,13)
def score(cards):
stand_on_value = 0
soft_ace = 0
for i in range(len(cards)):
if i == 1:
stand_on_value += 11
soft_ace = 1
# i += 1
if i < 10:
stand_on_value += i
if i >= 10 and i != 1:
stand_on_value += 10
else:
if i == 1:
stand_on_value += 1
return (stand_on_value, soft_ace)
if sum(cards) < 17:
stand_on_soft = False
elif sum(cards) >= 17:
stand_on_soft = True
print(score(cards))
"In for loop of score function" Instead of "i" you have to use "cards[i]
In your code the value of i will either be 0 or 1 [ Nothing else ] So, the output is the same each and every time
you have used random for generating random " cards[i] " [ & not "i" ]
One more error which I think is, You haven't used "stand_on_soft" anywhere in the code. So, what's the meaning of assigning true or false to it???
import random
cards = [random.randint(1, 13) for i in range(0, 2)]
# initialize hand with two random cards
def get_card():
# this function will randomly return a card with the value between 1 and 13
return random.randint(1, 13)
def score(cards):
stand_on_value = 0
soft_ace = 0
for i in range(0,len(cards)):
if cards[i] == 1:
stand_on_value += 11
soft_ace = 1
if cards[i] < 10:
stand_on_value += i
if cards[i] >= 10 and i != 1:
stand_on_value += 10
else:
if i == 1:
stand_on_value += 1
return (stand_on_value, soft_ace)
if sum(cards) < 17:
stand_on_soft = False
elif sum(cards) >= 17:
stand_on_soft = True
print(score(cards))
#PLEASE LIKE IT RAISE YOUR THUMBS UP
#IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT
SECTION