python - 我如何让这个功能从卡组中选择?
问题描述
import random
deck = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king', 'ace', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king', 'ace', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king', 'ace', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king', 'ace']
def dealer_total():
dealer_total = 0
while dealer_total < 16:
dealer_card = random.choice(deck)
deck.remove(dealer_card)
if dealer_card == "one":
dealer_total = dealer_total + 1
if dealer_card == "two":
dealer_total = dealer_total + 2
if dealer_card == "three":
dealer_total = dealer_total + 3
if dealer_card == "four":
dealer_total = dealer_total + 4
if dealer_card == "five":
dealer_total = dealer_total + 5
if dealer_card == "six":
dealer_total = dealer_total + 6
if dealer_card == "seven":
dealer_total = dealer_total + 7
if dealer_card == "eight":
dealer_total = dealer_total + 8
if dealer_card == "nine":
dealer_total = dealer_total + 9
if dealer_card == "ten" or dealer_card == "jack" or dealer_card == "queen" or dealer_card == "king":
dealer_total = dealer_total + 10
if dealer_card == "ace":
dealer_total = dealer_total + 11
if dealer_total > 15:
print(dealer_total)
def user_total():
user_total = 0
user_hit = input("Do you want to hit or stay? ")
while user_hit.lower() == "hit":
user_card = random.choice(deck)
deck.remove(user_card)
if user_card == "one":
user_total = user_total + 1
if user_card == "two":
user_total = user_total + 2
if user_card == "three":
user_total = user_total + 3
if user_card == "four":
user_total = user_total + 4
if user_card == "five":
user_total = user_total + 5
if user_card == "six":
user_total = user_total + 6
if user_card == "seven":
user_total = user_total + 7
if user_card == "eight":
user_total = user_total + 8
if user_card == "nine":
user_total = user_total + 9
if user_card == "ten" or user_card == "jack" or user_card == "queen" or user_card == "king":
user_total = user_total + 10
if user_card == "ace":
user_total = user_total + 11
print("Your total is", str(user_total) + ".")
if user_total == 21:
print("You win!")
if user_total < 22 and user_hit == "stay":
print(user_total)
if user_total > 21:
print("You busted!")
dealer_total()
user_total()
这是我的代码。每次我运行它时,它都会说“不能从空序列中选择”关于定义 user_card 的部分,即使它从列表中随机选择用于定义经销商卡。我也知道有一种更简洁的方法来做所有的 if 语句,我只是不知道如何去做,因为我是一个初学者编码器。任何帮助,将不胜感激。
解决方案
这里的具体问题是您在循环之前while
要求用户输入,但随后不要在while
循环内要求新输入,以便条件可以更改以退出。
user_hit = input("Do you want to hit or stay? ")
while user_hit.lower() == "hit":
可能是(限制代码更改量)
while input("Do you want to hit or stay? ").lower() == "hit":
接下来是循环的其余部分,从那时起,每个循环周期都会请求输入。
我还建议您使用字典来捕获卡片值(为什么同时存在“一”和“王牌”?),例如
card_value = {'one':1, 'two':2, 'three':3 # ... and so on
这将使您以后有机会使用以下表达式:
dealer_total += card_value[dealer_card]
推荐阅读
- oracle-apex - 允许在 Oracle apex 中仅选中一个复选框
- javascript - 当使用 PHP 包含时,工具按钮不起作用
- python - 带有 Azure blob 服务的 Python
- smartsheet-api - 如何解决连接到 api.smartsheet.com:443 [api.smartsheet.com/ip] 失败:连接被拒绝:连接?
- python - 将列表的元素拆分为数组
- javascript - Type(Java)Script,如何创建时间戳
- ios - 是否可以从 xcode 10.x 检测到任一用户在 iPhone 上选择了暗模式
- javascript - 使用 settimeout 从下拉列表中的后端返回数据
- java - 无法使用 Dagger 2 提供活动上下文
- python - Python库安装libact:libact构建轮失败