使用 while 循环使用另一个列表中的元素更新列表并找到新列表的总和
Using a while loop to update list with elements from another list and find the sum of new list
我很难使用 while 循环将卡片添加到列表并不断更新总和。
目前我收到一个操作数类型错误,提示我无法将列表添加到列表中。理想情况下
每当玩家选择一张带有 'y' 的新牌时,列表就会更新,分数也会更新。
import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
def blackjack():
should_continue = True
player_card1 = int(random.choice(cards))
comp_card = random.choice(cards)
if input("Do you want to play? Type 'y' or 'n'\n") == 'y':
while should_continue:
add_card = int(random.choice(cards))
player_cards = [player_card1]
player_cards.append(add_card)
comp_card = random.choice(cards)
player_score = sum(player_cards)
print(f"Your cards: {player_cards}, current score:{player_score}")
print(f"Computer's first card: {comp_card}")
if input(f"Type 'y' to get another card, type 'n' to pass:") == 'y':
player_card1 = player_cards
comp_card = comp_card
else:
should_continue = False
#calculate()
else:
should_continue = False
blackjack()
您正在创建嵌套列表。卡片是这样添加的
[10, 10]
[[10, 10], 10]
所以,你不能[10, 10] + 10
我已经修复它并删除了一些死代码。考虑使用 PyCharm 之类的东西进行调试、语法检查和其他好处
import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
def blackjack():
should_continue = True
player_cards = [random.choice(cards)]
if input("Do you want to play? Type 'y' or 'n'\n") == 'y':
while should_continue:
player_cards.append(random.choice(cards))
comp_card = random.choice(cards)
player_score = sum(player_cards)
print(f"Your cards: {player_cards}, current score:{player_score}")
print(f"Computer's first card: {comp_card}")
if not input(f"Type 'y' to get another card, type 'n' to pass:") == 'y':
should_continue = False
blackjack()
我很难使用 while 循环将卡片添加到列表并不断更新总和。 目前我收到一个操作数类型错误,提示我无法将列表添加到列表中。理想情况下 每当玩家选择一张带有 'y' 的新牌时,列表就会更新,分数也会更新。
import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
def blackjack():
should_continue = True
player_card1 = int(random.choice(cards))
comp_card = random.choice(cards)
if input("Do you want to play? Type 'y' or 'n'\n") == 'y':
while should_continue:
add_card = int(random.choice(cards))
player_cards = [player_card1]
player_cards.append(add_card)
comp_card = random.choice(cards)
player_score = sum(player_cards)
print(f"Your cards: {player_cards}, current score:{player_score}")
print(f"Computer's first card: {comp_card}")
if input(f"Type 'y' to get another card, type 'n' to pass:") == 'y':
player_card1 = player_cards
comp_card = comp_card
else:
should_continue = False
#calculate()
else:
should_continue = False
blackjack()
您正在创建嵌套列表。卡片是这样添加的
[10, 10]
[[10, 10], 10]
所以,你不能[10, 10] + 10
我已经修复它并删除了一些死代码。考虑使用 PyCharm 之类的东西进行调试、语法检查和其他好处
import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
def blackjack():
should_continue = True
player_cards = [random.choice(cards)]
if input("Do you want to play? Type 'y' or 'n'\n") == 'y':
while should_continue:
player_cards.append(random.choice(cards))
comp_card = random.choice(cards)
player_score = sum(player_cards)
print(f"Your cards: {player_cards}, current score:{player_score}")
print(f"Computer's first card: {comp_card}")
if not input(f"Type 'y' to get another card, type 'n' to pass:") == 'y':
should_continue = False
blackjack()