我正在为我的班级写一个二十一点游戏,我似乎无法弄清楚代码有什么问题,也许我只是盯着它看太长但是任何帮助都会受到赞赏
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
实际错误是:
追踪(最近一次通话): 文件“C:\ Users \ Daniel \ Desktop \ Software Design \ Tulminating project revised2.py”,第175行,在main()
文件“C:\ Users \ Daniel \ Desktop \ Software Design \ Tulminating project revised2.py”,第140行,主要 playerPool + =获胜
TypeError:+ =:'int'和'NoneType'不支持的操作数类型
答案 0 :(得分:0)
如果None
既不是option1
也不是'1'
,也不是'2'
,您的函数将返回'3'
。在这种情况下,将winning
(payout
的结果)添加到playerPool
将会失败。
也许您在函数的第一行添加print(option1)
,看看它是什么样的。
你可以这样重构你的功能:
def payout(betAmount, option1):
return int({'1': 1.5, '2': 1.24, '3': 1.2}[option1] * betAmount)
这样,当option1
不是有效选项时,您至少会遇到一个关键错误。
答案 1 :(得分:0)
在option1
中设置payoutRules
时,您可以指定整数值。
当您在option1
中检查payout
时,将其与字符串进行比较。它无法匹配任何内容,因此payout
始终返回None
。
您可以将payout
功能更改为:
def payout(betAmount):
return betAmount * (1.5, 1.25, 1.2)[option1]
这样做你使用option1
,这是一个整数来索引(1.5, 1.25, 1.2)
元组,以使乘数乘以betAmount
。如果option1
不是整数,那么你就会收到错误。