if statement - Python - For loop isn't kicking in (blackjack game) -
i'm starting learn python , first project text based blackjack game.
phand being players hand , ptotal being total of players cards.
the loop seems exiting after first iteration. when print ptotal , phand out after loop, shows first card's value every time.
code:
import random f = open('documents/python/cards.txt', 'r') deck = f.read().splitlines() f.close ptotal = 0 ctotal = 0 random.shuffle(deck) phand = [deck[0], deck[1]] chand = [deck[2]] x in phand: if deck[x][0] == '1' or deck[x][0] == 'j' or deck[x][0] == 'q' or deck[x][0] == 'k': ptotal += 10 elif deck[x][0] == 'a': ptotal += 11 else: ptotal += int(deck[x][0])
any appreciated!
i think want
#using x list item x in phand: if x[0] == '1' or x[0] == 'j' or x[0] == 'q' or x[0] == 'k': ptotal += 10 elif x[0] == 'a': ptotal += 11 else: ptotal += int(x[0])
the for-in loop iterates through items in phand
using x
temporary variable each value. in case, in first iteration, have x = deck[0]
. in second iteration have x = deck[1]
.
in code posted, trying use x
index, fine, long use right values loop.
#using x index x in range(0, len(phand)): if deck[x][0] == '1' or deck[x][0] == 'j' or deck[x][0] == 'q' or deck[x][0] == 'k': ptotal += 10 elif deck[x][0] == 'a': ptotal += 11 else: ptotal += int(deck[x][0])
Comments
Post a Comment