gpt4 book ai didi

python - 我的程序没有在仅在给出两个答案时停止的循环中显示 2 个答案

转载 作者:太空宇宙 更新时间:2023-11-04 08:37:13 25 4
gpt4 key购买 nike

我正在尝试构建 BlackJack 游戏。

程序启动,并使用随机模块生成 2 个不同的数字,告诉您您拥有哪张卡。

  • 数字 1 是值,例如8
  • 2 号是花色,例如4 = 黑桃

程序有时会给我 2 张卡片,而另一半时间会给我一张卡片。

问题:为什么这个程序没有按预期运行?

types = [" of Spades", " of Diamonds", " of Clubs", " of Hearts"]
special = ["King", "Queen", "Jack", "Ace"]
tries = 0

import random
import time

print("Welcome to BlackJack.")
time.sleep(1)

print("Let's Begin.")
time.sleep(1)

while tries < 1:
cardnumber1 = random.randint(2, 13)
random.shuffle(types)
random.shuffle(special)
cardnumber2 = random.randint(2, 13)
random.shuffle(special)
random.shuffle(types)
cardnumber3 = random.randint(2, 9)
random.shuffle(special)
random.shuffle(types)
cardnumber4 = random.randint(2, 9)
random.shuffle(special)
random.shuffle(types)
cardtype1 = random.choice(types)
random.shuffle(special)
random.shuffle(types)
cardtype2 = random.choice(types)
random.shuffle(special)
random.shuffle(types)
cardtype3 = random.choice(types)
random.shuffle(special)
random.shuffle(types)
cardtype4 = random.choice(types)
random.shuffle(special)
random.shuffle(types)
cardspecial = random.choice(special)
random.shuffle(special)
random.shuffle(types)
cardspecial2 = random.choice(special)
random.shuffle(special)
random.shuffle(types)

if cardnumber1 > 10:
print(str(cardnumber3) + cardtype1)
tries = tries + 1
if cardnumber2 > 10:
print(str(cardnumber4) + cardtype2)
tries = tries + 1
if cardnumber1 < 9:
print(cardspecial + cardtype3)
tries = tries + 1
if cardnumber2 < 9:
print(cardspecial2 + cardtype4)
tries = tries + 1

最佳答案

您可以完整地声明您的列表:

types = [" of Spades", " of Diamonds", " of Clubs", " of Hearts"]
cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"]

并创建一整套它们:

deck =  [ (card, typ) for card in cards for typ in types] # create tuples
print (deck)

要抽一张牌:

card = random.choice(deck) # will return a tuple for 1 card, 1st value is face, 2nd is typ

deck.remove(card) # remove the card from the deck so you dont draw it again

face,typ = card # deconstruct tuple

print(face)
print(typ)

输出:

 [('2', ' of Spades'), ('2', ' of Diamonds'), ('2', ' of Clubs'), ('2', ' of Hearts'),
('3', ' of Spades'), ('3', ' of Diamonds'), ('3', ' of Clubs'), ('3', ' of Hearts'),
('4', ' of Spades'), ('4', ' of Diamonds'), ('4', ' of Clubs'), ('4', ' of Hearts'),
('5', ' of Spades'), ('5', ' of Diamonds'), ('5', ' of Clubs'), ('5', ' of Hearts'),
('6', ' of Spades'), ('6', ' of Diamonds'), ('6', ' of Clubs'), ('6', ' of Hearts'),
('7', ' of Spades'), ('7', ' of Diamonds'), ('7', ' of Clubs'), ('7', ' of Hearts'),
('8', ' of Spades'), ('8', ' of Diamonds'), ('8', ' of Clubs'), ('8', ' of Hearts'),
('9', ' of Spades'), ('9', ' of Diamonds'), ('9', ' of Clubs'), ('9', ' of Hearts'),
('10', ' of Spades'), ('10', ' of Diamonds'), ('10', ' of Clubs'), ('10', ' of Hearts'),
('Jack', ' of Spades'), ('Jack', ' of Diamonds'), ('Jack', ' of Clubs'), ('Jack', ' of Hearts'),
('Queen', ' of Spades'), ('Queen', ' of Diamonds'), ('Queen', ' of Clubs'), ('Queen', ' of Hearts'),
('King', ' of Spades'), ('King', ' of Diamonds'), ('King', ' of Clubs'), ('King', ' of Hearts'),
('Ace', ' of Spades'), ('Ace', ' of Diamonds'), ('Ace', ' of Clubs'), ('Ace', ' of Hearts')]


10
of Diamonds

deck = [ (card, typ) for card in cards for typ in types] 

称为列表理解。这是一种从东西构造列表的方法——即范围、其他列表、迭代器、...

基本语法:

我构建元组 (card, typ)对于 cards 中的每个元素(您的列表)和 types你的其他列表)。

[ (card, typ) for card in cards for typ in types] 
# equivalent to
deck = [] # empty list
for card in cards:
for typ in types:
deck.append( (card,typ) ) # create tuples and add to list

你可以使用这副牌并 shuffle() 一次,然后从中取出 deck.pop() 牌(更快,只需要洗牌一次)或者每次随机抽取一张牌 random.choice(deck) 然后将其移除从甲板上。

参见 Lists对于列表中的方法。

参见 buildins - 你找到range()在该列表中,可以获得更多信息。

关于python - 我的程序没有在仅在给出两个答案时停止的循环中显示 2 个答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48086938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com