UNO Card Game - Help Requested

python

#1

https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/2/2?curriculum_id=4f89dab3d788890003000096#

NOTE: I am using Battleship! Exercise 16. Game Over to type my code in - my code does not relate to the actual exercise.

My code is as follows:

from random import randint
from random import choice
player_deck = []
com_deck = []
# Classes to represent Player and Computer
class Player(object):
    def __init__(self, number, color):
        self.number = number
        self.color = color
        def card_number():
            number = randint(1, 9)
            return number
        def card_color():
            color = choice('@#$%')
            return color
        def create_deck(x,y):
            x = str(x)
            fin_card = x + y
            player_deck.append(fin_card)
class Com(object):
    def __init__(self, number, color):
        self.number = number
        self.color = color
        def card_number():
            number = randint(1, 9)
            return number
        def card_color():
            color = choice('@#$%')
            return color
        def create_deck(x,y):
            x = str(x)
            fin_card = x + y
            com_deck.append(fin_card)
# Prep player/com decks
for x in range(7):
    Player.create_deck(Player.card_number, Player.card_color)
    Com.create_deck(Com.card_number, Com.card.color)


I am attempting to replicate the UNO Card Game through Python, however, a rather confusing error came through the console (Never mind the error Codecademy gives):

Traceback (most recent call last):
File "python", line 36, in <module>
AttributeError: type object 'Player' has no attribute 'create_deck'

Why does this error occur?


#2

I'm not good at Python, so I can't help with this one, but I moved the topic to the Corner Bar.

I suggest that you try it in labs instead of the exercise, because (I'm not 100% sure on this) you might get errors related to the exercise being incorrect.

Anyway, good luck with your game.


#3

Yeah, slight problem with Codecademy Labs: It never loads. That is the only reason I am using the exercise instead of Labs.


#4

If it doesn't work, you could try repl.it


#5

OK, good news and bad news:

Good News: repl.it works; the console loaded right away.

Bad News: The console still returns the exact same error message and I don't have a way to fix it.


#6

I played around with it in Labs, still no fix yet, but I ran Com.create_deck() I got the same error, but about Com not having the attribute instead, so whatever the problem is, it's the same for both.


#7

i don't use objects on a regular bases, but i would say your card_number, card_color and create_deck are inside the __init__ function (in your Player class), you might want to re-indent it, example:

def __init__(self, number, color):
    self.number = number
    self.color = color
    def card_number():
       number = randint(1, 9)
       return number

to:

def __init__(self, number, color):
    self.number = number
    self.color = color
def card_number():
   number = randint(1, 9)
   return number

Now, this will throw a new error, but at least solves the current problem