Python奇怪的结果 [英] Python weird results

查看:153
本文介绍了Python奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个代码

So I have this code

class Deck():

    deck=[]
    for i in range(1,15):
        deck.append(Card("red","heart",i))
        deck.append(Card("red","diamond",i))
        deck.append(Card("black","club",i))
        deck.append(Card("black","spade",i))
    #make our deck random
    random.shuffle(deck)
    #removing cards with number 11 because 1 and 11 cards are the same
    for i in deck:
        if i.number==11:
            deck.remove(i)


#hand class creates hands of 5 cards, taking items from the list created in deck class
class Hand():
    def get_hand():
        #check if there are enough cards to make a new hand
        if len(Deck.deck)<5  :
            print("No more hands to deal")
        else:
            #get the first 5 items from our deck
            iterator = islice(Deck.deck, 5)

            #show the hand created
            for i in iterator:
                print('{} {} {}'.format(i.number,i.type,i.colour))
                #delete the shown card from the deck
                Deck.deck.remove(i)





我开始发垃圾邮件后



After I start to spam

Hand.get_hand()



直到我的52张牌中少于10张牌,它不再打印另外5张牌,而是4张牌一。这有什么不对?



我尝试过的事情:



我试图调整


until there are less than 10 cards in my 52 cards deck, It won't print another 5 cards hand, but a 4 cards one. What is wrong here?

What I have tried:

I tried to tweak the

if len(Deck.deck)<5  :

行,但没有结果

推荐答案

你正在弄乱迭代器,在迭代时删除项目。

尝试

You are messing up with the iterator, removing items while iterating.
Try
import random
from itertools import islice

class Card():
  def __init__(self, color, suit, rank):
    self.color = color
    self.suit = suit
    self.rank = rank
  def display(self):
    print( '{2}-{0}-{1}'.format(self.color, self.suit,self.rank))

class Deck():
  def __init__(self):
    self.deck=[]
    for i in range(1,15):
      if i != 11:
        self.deck.append(Card("red","heart",i))
        self.deck.append(Card("red","diamond",i))
        self.deck.append(Card("black","club",i))
        self.deck.append(Card("black","spade",i))
    #make our deck random
    random.shuffle(self.deck)


#hand class creates hands of 5 cards, taking items from the list created in deck class
class Hand():
  def get_hand( theDeck ):
    #check if there are enough cards to make a new hand
    l = len(theDeck.deck)
    if l < 5:
      print("No more hands to deal")
    else:
      #get the first 5 items from our deck
      iterator = islice(theDeck.deck, 5)
      remain = slice(5,l)
      theDeck.deck = theDeck.deck[remain]
      #show the hand created
      for i in iterator:
        i.display()


theDeck = Deck()
for n in range (1,15):
  print("--------------------------------------")
  Hand.get_hand(theDeck)


你的代码没有表现出你期望的方式,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。 br />
调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不是'知道你的代码应该做什么,它没有发现错误,它只是通过向你展示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]



调试器只是向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于Python奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆