类型错误:不支持的操作数int和NoneType?python
我正在为我的班级写一个21点游戏,我似乎根本不知道代码有什么问题,也许我只是盯着它看太久了,但如果有任何帮助,我将不胜感激类型错误:不支持的操作数int和NoneType?python,python,typeerror,nonetype,Python,Typeerror,Nonetype,我正在为我的班级写一个21点游戏,我似乎根本不知道代码有什么问题,也许我只是盯着它看太久了,但如果有任何帮助,我将不胜感激 def payout(betAmount): global option1 if option1 == "1": betAmount*1.5 return int(betAmount) elif option1 == "2": betAmount*=1.25 return int(bet
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
导入随机、pickle
选项1=无
def payoutRule():
全球选择1
选择=无
边摘边摘!=“1”或“2”或“3”:
拾取=输入(“”)
你想玩的房子支出是多少?
1-3:2(1.5倍)
2-5:4(1.25倍)
3-6:5(1.2X)\n
""")
如果选择=“1”:
选项1=1
打破
elif pick==“2”:
选项1=2
打破
elif pick==“3”:
选项1=3
打破
其他:
打印(“该选项无效!\n”)
def支出(金额):
全球选择1
如果选项1==“1”:
betAmount*1.5
返回整数(betAmount)
elif选项1==“2”:
betAmount*=1.25
返回整数(betAmount)
elif选项1==“3”:
betAmount*=1.2
返回整数(betAmount)
def hitDeck():
卡片=(2,3,4,5,6,7,8,9,10,10,10,10,11)
抽卡=随机。选择(卡)
回程提款卡
def ACE计数器(手动):
得分=手数(11)
总计=总计(手)
如果总数>21且aces>0:
当ACE>0且总数>21时:
总数-=10
ACE-=1
返回总数
def main():
打印(“\t\t请转到21点表!”)
支付单元()
playerPool=int(250)
尽管如此:
playerCard=[]
经销商卡=[]
playerBust=False
DealBust=False
终局=无
打赌=无
playerCard.append(hitDeck())
playerCard.append(hitDeck())
打印(“你有”,playerPool,“可以玩的美元。”)
尽管如此:
bet=输入(“”)
你赌多少?
\t1-5美元
\t2-10美元
\t3-25美元
""")
如果下注=“1”:
打印(“你把5美元放在桌子上。”)
bet=int(5)
打破
elif bet==“2”:
打印(“你把10美元放在桌子上。”)
bet=int(10)
打破
elif bet==“3”:
打印(“你把25美元放在桌子上。”)
bet=int(25)
打破
其他:
打印(“这不是一个有效的选择!”)
当游戏结束时!="1":
totalPlayer=aceCounter(玩家卡)
打印(“您有”,len(玩家卡),“总价值为的卡”,totalPlayer)
如果totalPlayer>21:
打印(“你完蛋了!”)
playerBust=True
打破
elif totalPlayer==21:
打印(“你有21点!!!”)
打破
其他:
点击=输入(“”)
你想:
\t1-命中
\t2-支架
""")
如果hit中的“1”:
playerCard.append(hitDeck())
命中中的elif“2”:
打破
其他:
打印(“这不是一个有效的选择!”)
尽管如此:
dealerCard.append(hitDeck())
dealerCard.append(hitDeck())
尽管如此:
totalDealer=aceCounter(dealerCard)
如果是21:
打印(“经销商破产了!”)
DealBust=True
如果playerBust==False:
赢=支付(下注)
打印(“你赢了!”)
打印(“你刚刚赢了”,赢了,“美元!”)
playerPool+=获胜
elif playerBust==真:
打印(“你们俩都破产了,但房子还是会收的。”)
打印(“你刚刚输了”,打赌,“美元…”)
玩家工具-=下注
elif totalPlayer>totalDealer:
如果playerBust==False:
赢=支付(下注)
打印(“你赢了!”)
打印(“你刚刚赢了”,赢了,“美元!”)
playerPool+=获胜
如果playerBust==True:
打印(“经销商赢了!”)
打印(“你刚刚输了”,打赌,“美元…”)
玩家工具-=下注
elif totalPlayer==totalDealer:
打印(“这是一场平局,但众议院仍然获胜。”)
打印(“你刚刚输了”,打赌,“美元…”)
玩家工具-=下注
elif totalPlayer
实际错误如下:
回溯(最近一次呼叫最后一次):
文件“C:\Users\Daniel\Desktop\Software Design\concentering project revised2.py”,第175行,在main()中
文件“C:\Users\Daniel\Desktop\Software Design\concentering project revied2.py”,第140行,主目录
playerPool+=获胜
类型错误:不支持+=:'int'和'NoneType'的操作数类型如果
选项1
既不是'1'
也不是'2'
也不是'3'
,则函数将返回None
。在这种情况下,将获胜
(这是支付
的结果)添加到玩家工具
将失败
也许您可以添加一个打印(选项1)
作为函数的第一行,以查看它的外观
您可以通过以下方式重构函数:
def payout(betAmount, option1):
return int({'1': 1.5, '2': 1.24, '3': 1.2}[option1] * betAmount)
这样,当选项1<
def payout(betAmount):
return betAmount * (1.5, 1.25, 1.2)[option1]