简体   繁体   English

为什么python变量在某些情况下不显示

[英]why does python variable not show up in certain situations

i was trying to make blackjack in python but it get does not work as intended when the computer wins and i can not see why.我试图在 python 中制作二十一点,但是当计算机获胜时它无法按预期工作,我不明白为什么。 here is my code:这是我的代码:

import random

hand = []
chand = []
bust = 'no'
sit = 'no'
value = 0
ais = 0
play = 'yes'
x = 0
cards = {
        'a': 11,
        '2': 2,
        '3': 3, 
        '4': 4,
        '5': 5, 
        '6': 6, 
        '7': 7,
        '8': 8,
        '9': 9,
        '10': 10,
        'j': 10,
        'q': 10,
        'k': 10, 
        }



print('''welcome to black jack
type draw or sit to get started''')
while x < 2:
        cardc = random.choice(list(cards.keys()))
        hand.append(cardc)
        x = x + 1
print('your cards', ', '.join(hand))
while bust != 'yes' and sit != 'yes':
        player = input('what would you like to do: ')
        if player == 'draw':
                cardc = random.choice(list(cards.keys()))
                hand.append(cardc)
                print('your cards', ', '.join(hand))
                value = 0
                for i in hand:
                        value = value + cards[i]
#                print(value)
                if value > 21:
                        bust = 'yes'
        elif 'sit' in player:
                print('you have sat')
                sit = 'yes'
if 'yes' in bust:
        print ('you lose')



if sit == 'yes':
        while ais < value and ais < 21:
                cardc = random.choice(list(cards.keys()))
                chand.append(cardc)
                ais = 0
                for i in chand:
                        ais = ais + cards[i]
                        print(ais)

        if ais > 21:
                print('my score was', ais)
                print('you win')
        else:
                print('my score was', ais)
                print('you lose')

when the computer wins it is meant to show the computers cards but it just shows a blank but when the player wins it shows the computers cards.当计算机获胜时,它意味着显示计算机卡,但它只显示空白,但是当玩家获胜时,它显示计算机卡。 i am quite new to python and might be missing something obvious but i don't know why this is going wrong我对 python 很陌生,可能会遗漏一些明显的东西,但我不知道为什么会出错

while ais < value and ais < 21:

Is the issue you're facing.是你面临的问题。 If ais == value this never runs, so the dealer never draws a card.如果ais == value这永远不会运行,所以经销商永远不会抽牌。

You need to use;你需要使用;

while ais <= value and ais < 21:

Furthermore, this ties in with an issue you have here;此外,这与您在这里遇到的问题有关;

if ais > 21:
    print('my score was', ais)
    print('you win')
else:
    print('my score was', ais)
    print('you lose')

You're only checking if the dealer has a hand of over 21. You do not check if there is a draw (This results in a loss).您只是检查庄家的手牌是否超过 21。您不会检查是否有平局(这会导致亏损)。

You also do not set a value if the player does not draw a card, this means even if the user has a higher score then the dealer, the dealer will always win if the player does not draw.如果玩家不抽牌,你也不要设置一个值,这意味着即使用户的分数比庄家高,如果玩家不抽牌,庄家也总是赢。

welcome to black jack
type draw or sit to get started
your cards a, 8
what would you like to do: sit
you have sat
5
my score was 5
you lose

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM