总是打20分,二十一点

时间:2018-06-12 18:04:33

标签: python random

该程序应该有2张卡的用户抽奖,然后是计算机选择。我想要它,以便如果卡最终成为面部卡,它将值设置为10,Aces设置为1或11的用户输入。目前它将绘制2张卡,无论结果如何,计算机和用户都得到平分为20分。

import random
import easygui

card_total = 0
ace_value = 0

#List of cards
cards = {2,3,4,5,6,7,8,9,10,12,13,14,15}
card1, card2 = random.sample(cards,2)
#list of suits
suits = ['Clubs', 'Hearts', 'Spades', 'Diamonds']
suit1, suit2 = random.sample(suits, 2)
#converts number to face card name
if card1 == 12:
    card1 = 'Jack'
if card1 == 13:
    card1 = 'Queen'
if card1 == 14:
    card1 = 'King'
if card1 == 15:
    card1 = 'Ace'

if card2 == 12:
    card2 = 'Jack'
if card2 == 13:
    card2 = 'Queen'
if card2 == 14:
    card2 = 'King'
if card2 == 15:
    card2 = 'Ace'
#displays cards
first_card = ("your first card is the {} of {}") .format(card1,suit1)
second_card = ("your second card is the {} of {}") .format(card2,suit2)
print (first_card)
print (second_card)

#If draw is a face card set card value to ten
if card1 == 'Jack' or 'Queen' or 'King':
    card1 = 10
if card2 == 'Jack' or 'Queen' or 'King':
    card2 = 10
#If draw is an ace allow user to select value of ace
if card1 == 'Ace':
    ace_value = easygui.buttonbox('Do you want your ace value to be 1 or 11?','Blackjack',(1,11))
    card1 = ace_value

if card2 == 'Ace':
    ace_value = easygui.buttonbox('Do you want your ace value to be 1 or 11?','Blackjack',(1,11))
    card2 = ace_value

card_total = card1 + card2

#-------------------------------------------------------------------------
comp_list = [1,11]

comp_cards = {2,3,4,5,6,7,8,9,10,12,13,14,15}
comp_card1, comp_card2 = random.sample(comp_cards,2)

#converts number to face card name
if comp_card1 == 12:
    comp_card1 = 'Jack'
if comp_card1 == 13:
    comp_card1 = 'Queen'
if comp_card1 == 14:
    comp_card1 = 'King'
if comp_card1 == 15:
    comp_card1 = 'Ace'

if comp_card2 == 12:
    comp_card2 = 'Jack'
if comp_card2 == 13:
    comp_card2 = 'Queen'
if comp_card2 == 14:
    comp_card2 = 'King'
if comp_card2 == 15:
    comp_card2 = 'Ace'

if comp_card1 == 'Jack' or 'Queen' or 'King':
    comp_card1 = 10
if comp_card2 == 'Jack' or 'Queen' or 'King':
    comp_card2 = 10

#If draw is an ace computer randomly selects value
if comp_card1 == 'Ace':
    comp_ace_value = random.choice(comp_list)
    comp_card1 = comp_ace_value

if comp_card2 == 'Ace':
    comp_ace_value = random.choice(comp_list)
    comp_card2 = comp_ace_value

comp_select = comp_card1 + comp_card2
#-------------------------------------------------------------------------
print("##############################")
print("Your total is {}") .format(card_total)
print("Dealer total is {}") .format(comp_select)
#-------------------------------------------------------------------------
#Now begin Processing of game rules

if card_total > 21:
    print("You're bust! GAME OVER")
    exit()

if comp_select > 21:
    print("Dealer has gone over! You Win!")
    exit()

if card_total > comp_select and card_total <= 21:
    print("You have beaten the dealer! You Win!")

if card_total < comp_select and comp_select <= 21:
    print("You have been beaten!")

if card_total == comp_select:
    print("Tie game!")

1 个答案:

答案 0 :(得分:4)

你的问题在这一行:

if comp_card1 == 'Jack' or 'Queen' or 'King':

这不是or运算符的工作原理。您需要重复操作:

if comp_card1 == 'Jack' or comp_card1 == 'Queen' or comp_card1 == 'King':

或者更好的是,使用in运算符:

if comp_card1 in ('Jack', 'Queen', 'King'):