Trying to score a hand of blackjack in this python code but my loop is consistently...

50.1K

Verified Solution

Question

Programming

Trying to score a hand of blackjack in this python code but myloop is consistently outputting (13,1) which makes me think thatsomething 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 1and 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))

Answer & Explanation Solved by verified expert
4.1 Ratings (538 Votes)
In for loop of score function Instead of i you have to usecardsiIn your code the value of i will either be 0 or 1 Nothing else So the output is the same each and every timeyou have    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students