Pierian-Data / Complete-Python-3-Bootcamp

Course Files for Complete Python 3 Bootcamp Course on Udemy
26.73k stars 84.08k forks source link

am i doing something wrong here #304

Open imahana1109 opened 3 years ago

imahana1109 commented 3 years ago

class Deck:

def __init__(self):

    self.all_cards=[]

    for suit in suits:
        for rank in ranks:

            created_card=Card(suit,rank)

            self.all_cards.append(created_card)

def shuffle(self):

    random.shuffle(self.all_cards)

def deal_one(self):
    return self.all_cards.pop()

    #code above returns TypeError:pop from empty list when i run code below

setup

player_one=Player("One") player_two=Player("Two")

new_deck=Deck() new_deck.shuffle()

for x in range(26): player_one.add_cards(new_deck.deal_one()) player_two.add_cards(new_deck.deal_one()) Note:spaces used to be underscores, this code is from milestone 2 :) please tell me if i did something wrong

imahana1109 commented 3 years ago

p.s. note that error is this:

IndexError Traceback (most recent call last)

in 7 8 for x in range(26): ----> 9 player_one.add_cards(new_deck.deal_one()) 10 player_two.add_cards(new_deck.deal_one()) in deal_one(self) 17 18 def deal_one(self): ---> 19 return self.all_cards.pop() 20 IndexError: pop from empty list
hbmartin commented 3 years ago

@imahana1109 this error is happening because calling some_list.pop() requires some_list to have at least one item. So the problem here isn't actually at the line where the error occurred, but in setting up the deck in __init__(). At least one of suits or cards are not correctly setup, start by checking those values.

imahana1109 commented 3 years ago

@imahana1109 this error is happening because calling some_list.pop() requires some_list to have at least one item. So the problem here isn't actually at the line where the error occurred, but in setting up the deck in __init__(). At least one of suits or cards are not correctly setup, start by checking those values.

class Card:

def __init__(self,suit,rank):
    self.suit=suit
    self.rank=rank
    self.values=values[rank]

def __str__(self):
    return self.rank+ " of "+self.suit

above is card class

import random suits=("Hearts","Diamonds","Spades","Clubs") ranks=("Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King") values = {"Two": 2, "Three":3, "Four":4, "Five":5, "Six":6, "Seven":7, "Eight":8, "Nine":9, "Ten":10, "Jack":11, "Queen":12, "King":13, "Ace":14}

setting up deck

hbmartin commented 3 years ago

ranks=("Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King") this line is the problem 🙂

RishiSuthar commented 3 years ago

ranks=("Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King")

please correct this line, you will be good to go...

imahana1109 commented 3 years ago

So are you guys saying I forgot Ace in ranks? if you say yes, and if it works, i'll close this issue

longwuyuan commented 3 years ago

print the cards for both players while doing the loop 26 and you will see helpful info

imahana1109 commented 3 years ago

print the cards for both players while doing the loop 26 and you will see helpful info

........ can you type the code in reply idk what you're talking about, and it doesn't work