如何正确使用random.choice函数

时间:2018-01-15 21:06:14

标签: python python-3.x random

我目前正在尝试制作二十一点游戏,但我收到错误:

AttributeError: 'builtin_function_or_method' object has no attribute 'choice'

我试图将代码拆分成更小的部分,但我无法弄清楚错误是什么。如果有人能帮助我,我将不胜感激!

这些是我的代码:

import random
from pylab import *

chips=0
bet=0
yourCards=0
kortstokk=[1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13]
p_firstCard=0
d_firstCard=0
p_secondCard=0
d_secondCard=0
dealerCards=0
END=False

def beginPlayer():
    global yourCards
    p_firstCard=random.choice(kortstokk)
    kortstokk.remove(p_firstCard)
    p_secondCard=random.choice(kortstokk)
    kortstokk.remove(p_secondCard)
    yourCards=p_firstCard+p_secondCard
    return yourCards
#end beginPlayer

def beginDealer():
    global dealerCards
    d_firstCard=random.choice(kortstokk)
    kortstokk.remove(d_firstCard)
    d_secondCard=random.choice(kortstokk)
    kortstokk.remove(d_secondCard)
    dealerCards=d_firstCard+d_secondCard
    return dealerCards
#end beginDealer

def playerHit():
    global yourCards
    p_hit=0
    p_hit=random.choice(kortstokk)
    kortstokk.remove(p_hit)
    yourCards=yourCards+p_hit
    return yourCards
#end p_hit

def playerWon():
    global bet
    global chips
    chips=0
    print("Congratulation, you won this hand and will recive the twice your bet.")
    chips=chips+(2*bet)
#end playerWon

def dealerWon():
    print("You lost this round. Better luck next round.")
    global bet
    bet=0
#end dealerWon

def bothWin():
    global chips
    global bet
    print("You had equal cards.")
    print("You will recive your original bet again.")    
    chips=chips+bet
    bet=0
#end bothWin

def showWin():
    if dealerCards>21:
        playerWon()
    if yourCards>21:
        dealerWon()
    if 21>=yourCards and yourCards>dealerCards:
        playerWon()
    if 21>=dealerCards and dealerCards>yourCards:
        dealerWon()
    if yourCards==dealerCards:
        bothWin()
#end showDealer

while not END:
    bet=input("Place your bet > ")
    bet=int(bet)
    beginPlayer()
    beginDealer()
    print("Your card amount is : ",yourCards)
    print("The dealer's first card is : ",d_firstCard)
    hit1=input("Would you like to hit or stand?(h/s) > ")
    if hit1=="h":
        playerHit()
        hit2=input("Would you like to hit or stand?(h/s) > ")
        if hit2=="h":
            playerHit()
            hit3=input("Would you like to hit or stand?(h/s) > ")
            if hit3=="h":
                playerHit()
                hit4=input("Would you like to hit or stand?(h/s) > ")
                if hit4=="h":
                    playerHit()
                    hit5=input("Would you like to hit or stand?(h/s) > ")
                    if hit5=="h":
                        playerHit()
                    if hit5=="s":
                        showWin()
                    else:
                        print("Ukjent kommando...")
                    #end if
                if hit4=="s":
                    showWin()
                else:
                    print("Ukjent kommando...")
                #end if
            if hit3=="s":
                showWin()
            else:
                print("Ukjent kommando...")
            #end if
        if hit2=="s":
            showWin()
        else:
            print("Ukjent kommando...")
        #end if
    if hit1=="s":
        showWin()
    else:
        print("Ukjent kommando...")
    playAgain=input("Do you want to play again? (y/n) > ")
    if playAgain=="n":
        END
    if playAgain=="y":
        not END
    #end if
#end while

1 个答案:

答案 0 :(得分:0)

pylab的random正在影响您的随机导入。反转订单或导入选择

from pylab import *
import random
# or
from random import choice