Python 如何正确使用random.choice函数
我目前正在尝试制作21点游戏,但我发现错误:Python 如何正确使用random.choice函数,python,python-3.x,random,Python,Python 3.x,Random,我目前正在尝试制作21点游戏,但我发现错误: 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,
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
pylab的
random
正在跟踪您的随机导入。颠倒顺序或导入选项
from pylab import *
import random
# or
from random import choice
“'builtin\u function\u或\u method'”:当您看到错误时,10次中有9次,这意味着您没有实际调用该函数或方法。这里的问题是您从pylab import*导入的
pylab
有一个random()
函数,该函数覆盖模块。这就是为什么您很少、永远、永远都不应该使用导入中的*
。我看不到任何pylab
功能,所以只需删除整个导入。如果将来需要该功能,请使用单独的导入,例如从matplotlib导入pyplot作为plt导入numpy作为np
或。这不会起任何作用:它不会将END
更改为True
,也不会打破循环。最惯用的解决方案可能是使用,而True:
如果playreach==“n”:break
。