Skip to content
Learnearn.uk » A Level Computer Science Home » Classes & Objects Programming Exercises in Python

Classes & Objects Programming Exercises in Python

Challenge 1 - Dog

Challenge 1 – Dog Class

Class Dog

Attributes:

  • Name
  • size
  • Breed Default: ‘Unknown’
  • Date of birth in DD/MM/YYYY format  Default: ‘Unknown’

Methods:

  • Bark
    • This should get the dog to bark (print out the word ‘woof!’)
  • get_name
    • This should return the dog’s name
  • set_name
    • This should allow the user to set an alphabetical name between 2 and 30 characters.
    • Convert the name to title case before setting
  • dog_years
    • This should calculate a dog’s age in dog years (use 1 year = 7 dog years)

Challenge 2 - Deck of Cards

Challenge 2 – Deck of Cards

Create 2 Classes:

Class Card

Attributes:

  • name
  • value

Class Deck

Attributes:

  • pack: this should be initialised to a list of full deck of cards objects
  • discarded: This should be initialised to an empty list

Methods

  • Shuffle
  • Pick a card (should return a card instance from the top of the deck)

Create a simple higher or lower game using your deck of cards