【发布时间】:2013-12-17 13:53:26
【问题描述】:
我正在为我的班级编写一个二十一点游戏,但我似乎根本无法弄清楚代码有什么问题,也许我只是盯着它太久了,但我们将不胜感激
def payout(betAmount):
global option1
if option1 == "1":
betAmount*1.5
return int(betAmount)
elif option1 == "2":
betAmount*=1.25
return int(betAmount)
elif option1 == "3":
betAmount*=1.2
return int(betAmount)
接下来是:
winning=payout(bet)
playerPool+=winning
抱歉,完整代码是:
import random, pickle
option1=None
def payoutRule():
global option1
pick=None
while pick != "1" or "2" or "3":
pick=input("""
What is the house payout you want to play with?
1- 3:2 (1.5X)
2- 5:4 (1.25X)
3- 6:5 (1.2X)\n
""")
if pick == "1":
option1=1
break
elif pick == "2":
option1=2
break
elif pick == "3":
option1=3
break
else:
print("That is not a valid option!\n")
def payout(betAmount):
global option1
if option1 == "1":
betAmount*1.5
return int(betAmount)
elif option1 == "2":
betAmount*=1.25
return int(betAmount)
elif option1 == "3":
betAmount*=1.2
return int(betAmount)
def hitDeck():
CARDS=(2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11)
drawCard=random.choice(CARDS)
return drawCard
def aceCounter(hand):
aces=hand.count(11)
total=sum(hand)
if total > 21 and aces > 0:
while aces > 0 and total > 21:
total-=10
aces-=1
return total
def main():
print("\t\tWelcome to the Blackjack Table!")
payoutRule()
playerPool=int(250)
while True:
playerCard=[]
dealerCard=[]
playerBust=False
dealerBust=False
endGame=None
bet=None
playerCard.append(hitDeck())
playerCard.append(hitDeck())
print("You have", playerPool, "dollars to play with.")
while True:
bet=input("""
How much are you betting?
\t1- $5
\t2- $10
\t3- $25
""")
if bet == "1":
print("You put down $5 on the table.")
bet=int(5)
break
elif bet == "2":
print("You put down $10 on the table.")
bet=int(10)
break
elif bet == "3":
print("You put down $25 on the table.")
bet=int(25)
break
else:
print("That is not a valid choice!")
while endGame != "1":
totalPlayer=aceCounter(playerCard)
print("You have", len(playerCard), "cards with a total value of", totalPlayer)
if totalPlayer > 21:
print("You are busted!")
playerBust=True
break
elif totalPlayer == 21:
print("You've got a blackjack!!!")
break
else:
hit=input("""
Would you like to:
\t1- Hit
\t2- Stand
""")
if "1" in hit:
playerCard.append(hitDeck())
elif "2" in hit:
break
else:
print("That is not a valid choice!")
while True:
dealerCard.append(hitDeck())
dealerCard.append(hitDeck())
while True:
totalDealer=aceCounter(dealerCard)
if totalDealer <= 17:
dealerCard.append(hitDeck())
else:
break
print("The dealer has", len(dealerCard), "cards with a total value of", totalDealer)
if totalDealer > 21:
print("The dealer busted!")
dealerBust=True
if playerBust == False:
winning=payout(bet)
print("You won!")
print("You just won", winning, "dollars!")
playerPool+=winning
elif playerBust == True:
print("You both busted but the house will still collect.")
print("You just lost", bet, "dollars...")
playerPool-=bet
elif totalPlayer > totalDealer:
if playerBust == False:
winning=payout(bet)
print("You won!")
print("You just won", winning, "dollars!")
playerPool+=winning
if playerBust == True:
print("The dealer won!")
print("You just lost", bet, "dollars...")
playerPool-=bet
elif totalPlayer == totalDealer:
print("It's a draw, but the house still wins.")
print("You just lost", bet, "dollars...")
playerPool-=bet
elif totalPlayer < totalDealer:
print("The dealer won!")
print("You just lost", bet, "dollars...")
playerPool-=bet
break
if playerPool == 0:
print("You don't have anymore money to play with!")
break
elif playerPool < 0:
print("You owe the house", -playerPool, "dollars, time to work in the kitchen...")
break
print("You have", playerPool, "dollars in your pocket now.")
playAgain=input("Press the Enter key to play again or 'q' to quit.\n")
if "q" in playAgain.lower():
break
实际的错误是这样的:
Traceback(最近一次调用最后一次): 文件“C:\Users\Daniel\Desktop\Software Design\Culminating project revision2.py”,第 175 行,在 main()
文件“C:\Users\Daniel\Desktop\Software Design\Culminating project revision2.py”,第 140 行,在 main playerPool+=获胜
TypeError:+= 不支持的操作数类型:“int”和“NoneType”
【问题讨论】:
-
完整显示错误信息。事实上,将所有代码完整地展示给我们可能是个好主意。
-
除了忘记
=为betAmount *= 1.5行之外,您的代码应该可以正常工作,除非bet是None。或option1不是您测试的字符串值。 -
请告诉我们
bet和option1。 -
option1是一个糟糕的变量名,你应该将它作为第二个参数传递给这个函数,而不是使用global -
请记住,当您寻求帮助时,请向人们提供他们需要的所有信息,以便轻松解决问题。事实上,您没有说明错误发生的位置(错误消息告诉您这一点),我们知道您是如何创建
bet变量的,所以我们不知道它是什么类型的对象。