这是错误:
追踪(最近一次通话): 文件" C:/ Users / Wattel / Desktop / 21 21 21.py",第177行,in 主要() 文件" C:/ Users / Wattel / Desktop / 21 21 21.py",第167行,主要 PPOINTS,DPOINTS,PHAND,DHAND = dLoop(PHAND,DHAND,DECK) TypeError:' NoneType'对象不可迭代
我知道它与此代码有关,但无法弄清楚问题究竟是什么:
def create
@form = Form.new(form_params)
respond_to do |format|
if @form.save
format.html { redirect_to @form, notice: 'Form was successfully created.' }
format.json { render :show, status: :created, location: @form }
else
format.html { redirect_to new_form_path(:event_id => request.referrer.params[:event_id]) }
format.json { render json: @form.errors, status: :unprocessable_entity }
end
end
end
以下是我的其余代码:
scoreHAND(DHAND) < 18 or scoreHAND(DHAND) < 21 and scoreHAND(DHAND) < scoreHAND(PHAND) or scoreHAND(DHAND) < 21
答案 0 :(得分:1)
我看到这个代码的唯一问题是在checkScore()函数中,每个if语句返回相同的东西,但ifs之外没有返回。如果你错过了一个角落案件或其他什么,那么这将引发错误。我建议移动线
return pwin, dwin
在if,elif块之外的。如果这不能解决您的问题,请尝试修改dloop功能;只有在函数返回None时才会出现错误。
答案 1 :(得分:0)
这是最终的代码结果!有没有人发现任何问题?
##21 Card Game
##Wattel
##2016 Spring
from random import *
import random
from random import shuffle, randint
CARDPOINTS = {'AD': 11,'AH': 11,'AC': 11,'AS': 11,
'KD': 10,'KH': 10,'KC': 10,'KS':10,
'QD': 10,'QH': 10,'QC': 10,'QS': 10,
'JD': 10,'JH': 10,'JC': 10,'JS': 10,
'10D': 10,'10H': 10,'10C': 10,'10S': 10,
'9D': 9,'9H': 9,'9C': 9,'9S': 9,
'8D': 8,'8H': 8,'8C': 8,'8S': 8,
'7D': 7,'7H': 7,'7C': 7,'7S': 7,
'6D': 6,'6H': 6,'6C': 6,'6S': 6,
'5D': 5,'5H': 5,'5C': 5,'5S': 5,
'4D': 4,'4H': 4,'4C': 4,'4S': 4,
'3D': 3,'3H': 3,'3C': 3,'3S': 3,
'2D': 2,'2H': 2,'2C': 2,'2S': 2,}
def shuffleDeck():
List = ['AD','AH','AC','AS',
'KD','KH','KC','KS',
'QD','QH','QC','QS',
'JD','JH','JC','JS',
'10D','10H','10C','10S',
'9D','9H','9C','9S',
'8D','8H','8C','8S',
'7D','7H','7C','7S',
'6D','6H','6C','6S',
'5D','5H','5C','5S',
'4D','4H','4C','4S',
'3D','3H','3C','3S',
'2D','2H','2C','2S',]
CardPile = len(List)
random.shuffle(List)
return List
def want2play():
Hit2Play = input(" Hit 'y' to begin your game of 21): ")
print ("")
return Hit2Play
def deckCheck(DECK):
print (DECK)
print("There are:",len(DECK),"cards in the deck.")
if len(DECK) < 20:
DECK = shuffleDeck()
return DECK
else:
return DECK
def openingDeal(DECK):
PHAND = []
DHAND = []
DHANDT = []
PHAND, DECK = dealOneCard(PHAND, DECK)
DHAND, DECK = dealOneCard(DHAND, DECK)
DHANDT.append(DHAND[0])
PHAND, DECK = dealOneCard(PHAND, DECK)
DHAND, DECK = dealOneCard(DHAND, DECK)
DHANDT.append("back of card")
PPOINTS = scoreHAND(PHAND)
printScores_0(PPOINTS, PHAND, DHANDT)
return PHAND, DHAND, DHANDT, DECK
def pLoop(PHAND, DHAND, DHANDT, DECK):
while scoreHAND(PHAND) < 21:
hitorhold = input('Do you want to hit or hold?: ')
if hitorhold == 'hit':
dealOneCard(PHAND, DECK)
printScores_0(scoreHAND(PHAND), PHAND, DHANDT)
elif hitorhold == 'hold':
print('Player holds')
printScores_0(scoreHAND(PHAND), PHAND, DHANDT)
break
return PHAND, DHAND, DECK
def dLoop(PHAND, DHAND, DECK):
printScores_1(scoreHAND(PHAND), scoreHAND(DHAND), PHAND, DHAND)
if scoreHAND(DHAND) < 21 or scoreHAND(DHAND) < 21 or scoreHAND(DHAND) < scoreHAND(PHAND) or scoreHAND(DHAND) < 21 or scoreHAND(PHAND) < 22:
dealOneCard(DHAND, DECK)
printScores_1(scoreHAND(PHAND), scoreHAND(DHAND), PHAND, DHAND)
return scoreHAND(PHAND), scoreHAND(DHAND), PHAND, DHAND
else:
return scoreHAND(PHAND), scoreHAND(DHAND), PHAND, DHAND
def checkScore(pWin, dWin, PPOINTS, DPOINTS):
if DPOINTS > PPOINTS and DPOINTS < 22:
dWin += 1
print (' ')
print ("Dealer Win")
return pWin, dWin
elif PPOINTS == DPOINTS and PPOINTS < 21:
dWin += 1
print (' ')
print ("Dealer Win")
return pWin, dWin
elif PPOINTS > 21 and DPOINTS < 21:
dWin += 1
print(' ')
print("Dealer Win")
return pWin, dWin
elif PPOINTS < 22 and DPOINTS > 21:
pWin += 1
print (' ')
print ("Player Win")
return pWin, dWin
elif PPOINTS > 21 and DPOINTS > 21:
print (' ')
print ("Tie.")
return pWin, dWin
else:
PPOINTS == 21 and DPOINTS == 21
print(' ')
print("Tie.")
return pWin, dWin
def dealOneCard(HAND,DECK):
theCard = DECK.pop(0)
HAND.append(theCard)
return HAND, DECK
def scoreHAND(HAND):
points = addScore(HAND)
aceCount=0
if points > 21:
aceCount += HAND.count('AS')
aceCount += HAND.count('AH')
aceCount += HAND.count('AC')
aceCount += HAND.count('AD')
while points > 21 and aceCount > 0:
points -= 10
aceCount -= 1
return points
else:
return points
def addScore(HAND):
tempScore = 0
for i in HAND:
tempScore += CARDPOINTS[i]
return tempScore
def printScores_0(POINTS, HAND, HAND1):
print("Player's cards: ", HAND, "Player's hand score: ", POINTS)
print("Dealer's cards: ", HAND1)
def printScores_1(PPOINTS, DPOINTS, PHAND, DHAND):
print("Player's cards: ", PHAND, "Player's hand score: ", PPOINTS)
print("Dealer's cards: ", DHAND, "Dealer's hand score: ", DPOINTS)
def main():
DECK = shuffleDeck()
pWin = 0
dWin = 0
while True:
Hit2Play = want2play()
if Hit2Play == 'y':
DECK = deckCheck(DECK)
PHAND, DHAND, DHANDT, DECK = openingDeal(DECK)
PHAND, DHAND, DECK = pLoop(PHAND, DHAND, DHANDT, DECK)
PPOINTS, DPOINTS, PHAND, DHAND = dLoop(PHAND, DHAND, DECK)
pWin, dWin = checkScore(pWin, dWin, PPOINTS, DPOINTS)
print("")
print("Player's Wins:", pWin, "Dealer's Wins:",dWin)
print("")
else:
print("Player's Wins:", pWin, "Dealer's Wins:",dWin)
break
main()
答案 2 :(得分:0)
我在你的新代码中假设你犯了一个拼写错误,
else:
PPOINTS == 21 and DPOINTS == 21
print(' ')
print("Tie.")
return pWin, dWin
应该是
elif PPOINTS == 21 and DPOINTS == 21:
print(' ')
print("Tie.")
return pWin, dWin
如果是这样的话,我写了一个像这样的快速测试框架:
results = [[''] * 25 for _ in range(25)]
for dp in range(25):
for pp in range(25):
res = checkScore(0, 0, pp, dp)
if res is None:
results[dp][pp] = 'N'
else:
p, d = res
results[dp][pp] = ["=+","-="][d][p]
results.reverse() # flip bottom-to-top
print("\n".join("".join(row) for row in results))
给出(标签添加):编辑:我得到了反击得分:
++++++++++++++++++++++===
++++++++++++++++++++++===
++++++++++++++++++++++===
D ---------------------=NNN
e 20 ---------------------N---
a --------------------NN--- + player wins
l -------------------NNN--- = player ties
e ------------------NNNN--- - player loses
r -----------------NNNNN---
15 ----------------NNNNNN---
P ---------------NNNNNNN--- N game crashes because
o --------------NNNNNNNN--- checkScore does not
i -------------NNNNNNNNN--- handle this combination
n ------------NNNNNNNNNN--- of inputs
t 10 -----------NNNNNNNNNNN---
s ----------NNNNNNNNNNNN---
---------NNNNNNNNNNNNN---
--------NNNNNNNNNNNNNN---
-------NNNNNNNNNNNNNNN---
5 ------NNNNNNNNNNNNNNNN---
-----NNNNNNNNNNNNNNNNN---
----NNNNNNNNNNNNNNNNNN---
---NNNNNNNNNNNNNNNNNNN---
--NNNNNNNNNNNNNNNNNNNN---
0 -NNNNNNNNNNNNNNNNNNNNN---
0 5 1 1 2
0 5 0
Player Points
我在那里看到了很多N.
编辑2:解决方案:
上面的图表可以让您轻松查看所涉及的几何区域;我们只需要重写checkScore
来正确处理它们:
def check_score(player_wins, dealer_wins, player_pts, dealer_pts):
if dealer_pts > 21:
if player_pts > 21:
# no-one wins (top right square)
print("\nTie")
return player_wins, dealer_wins
else:
# player wins (top left rectangle)
print("\nPlayer wins")
return player_wins + 1, dealer_wins
else:
if player_pts > 21:
# player loses (bottom right rectangle)
print("\nDealer wins")
return player_wins, dealer_wins + 1
elif dealer_pts >= player_pts:
# player loses (top triangle)
print("\nDealer wins")
return player_wins, dealer_wins + 1
else:
# player wins (bottom triangle)
print("\nPlayer wins")
return player_wins + 1, dealer_wins
并重新测试给了我们
++++++++++++++++++++++===
++++++++++++++++++++++===
++++++++++++++++++++++===
-------------------------
---------------------+---
--------------------++--- Problem solved!
-------------------+++---
------------------++++---
-----------------+++++---
----------------++++++---
---------------+++++++---
--------------++++++++---
-------------+++++++++---
------------++++++++++---
-----------+++++++++++---
----------++++++++++++---
---------+++++++++++++---
--------++++++++++++++---
-------+++++++++++++++---
------++++++++++++++++---
-----+++++++++++++++++---
----++++++++++++++++++---
---+++++++++++++++++++---
--++++++++++++++++++++---
-+++++++++++++++++++++---
答案 3 :(得分:0)
为了比较,我写了一个OO版本。希望你能从中学到很多东西; - )
from random import shuffle
LOSE, TIE, WIN = -1, 0, 1
def get_yn(prompt, error_message=None, yes_values={'y','yes',''}, no_values={'n','no'}):
"""
Prompt for input until a yes_value or no_value is entered.
Return True for a yes_value or False for a no_value
"""
while True:
response = input(prompt).strip().lower()
if response in yes_values:
return True
elif response in no_values:
return False
elif error_message is not None:
print(error_message)
class Deck:
SUITS = ('D', 'H', 'C', 'S')
FACES = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')
POINTS = ( 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11 )
def __init__(self):
self.reset()
def freshen(self):
if len(self.cards) < 20:
self.reset()
def reset(self):
self.cards = list(self.CARD_VALUES)
shuffle(self.cards)
def deal_to(self, hand):
card = self.cards.pop()
hand.cards.append(card)
# Back-patch because we cannot refer to Deck class variables until Deck is fully defined
Deck.CARD_VALUES = {f+s:p for f,p in zip(Deck.FACES, Deck.POINTS) for s in Deck.SUITS}
class Hand:
def __init__(self):
self.cards = []
@property
def points(self):
# get sum of cards values, counting aces as 11
card_value = Deck.CARD_VALUES.__getitem__
total = sum(card_value(card) for card in self.cards)
if total > 21:
# Aces can also be counted as 1;
# if total is too high, try to get it <= 21
max_aces = sum(card[0] == 'A' for card in self.cards)
use_aces = (total - 12) // 10
total -= 10 * min(use_aces, max_aces)
return total
def __str__(self):
return "{} ({} points)".format(" ".join(self.cards), self.points)
def dealer_first_hand(self):
# alternate __str__ for use while dealer's second card is hidden
return "{} ??".format(self.cards[0])
def show_hands(player, dealer, first_hand=False):
print("Player hand: {}".format(player))
print("Dealer hand: {}\n".format(dealer.dealer_first_hand() if first_hand else dealer))
class TwentyOne:
def __init__(self):
self.deck = Deck()
@staticmethod
def result(player_pts, dealer_pts):
if dealer_pts > 21:
if player_pts > 21:
# no-one wins (top right square)
print("\nTie")
return TIE
else:
# player wins (top left rectangle)
print("\nPlayer wins")
return WIN
else:
if player_pts > 21:
# player loses (bottom right rectangle)
print("\nDealer wins")
return LOSE
elif dealer_pts >= player_pts:
# player loses (top triangle)
print("\nDealer wins")
return LOSE
else:
# player wins (bottom triangle)
print("\nPlayer wins")
return WIN
def play(self):
self.deck.freshen()
player = Hand()
dealer = Hand()
# opening deal
self.deck.deal_to(player)
self.deck.deal_to(dealer)
self.deck.deal_to(player)
self.deck.deal_to(dealer)
# player loop
while player.points < 21:
show_hands(player, dealer, True)
if get_yn("Do you want to take another card? [Y/n] "):
self.deck.deal_to(player)
else:
print("Player holds\n")
break
# dealer loop
show_hands(player, dealer)
while dealer.points < 17:
self.deck.deal_to(dealer)
show_hands(player, dealer)
# tally the result
return self.result(player.points, dealer.points)
def main():
game = TwentyOne()
wins = 0
losses = 0
while True:
result = game.play()
wins += (result == WIN)
losses += (result == LOSE)
print("Player won {}, dealer won {}\n".format(wins, losses))
if not get_yn("Play again? [Y/n] "): break
if __name__ == "__main__":
main()