Author Topic: Need help with Python  (Read 1398 times)

0 Members and 1 Guest are viewing this topic.

Offline Son of Gnome

  • Thread Starter
  • Posts: 129
Need help with Python
« on: Fri, 09 December 2011, 16:47:42 »
Hey guys I working on some python homework which I am completely lost on.

I am trying to make a card game, most of it works fine except the my moveCard function it doesnt work and I dont know why.

Here is my code:
# Note: cards are represented as 2-character strings, e.g.,
#       "2C", "9S", "QH", "TD", "AC"

from random import randint, shuffle

class DeckOfCards:
  def __init__(self):
      """Initializes a new deck of cards."""
      ranks = "23456789TJQKA"
      suits = "CDHS"
      self.cards = [r+s for r in ranks for s in suits]

  def shuffle(self):
      """Shuffles the cards into a random order."""
      shuffle(self.cards)

  def dealCard(self):
      """Removes the top/last card in the deck & returns it."""
      if self.numCards() > 0:
        return self.cards.pop()
      else:
        return ""

  def addCard(self, card):
      """Adds the specified card to the bottom/front of the deck."""
      self.cards.insert(0, card)
     
  def numCards(self):
      """Returns the number of cards remaining in the deck."""
      return len(self.cards)

  def __str__(self):
      """Returns the string representation of the deck."""
      return ' '.join(self.cards)

#########
   
class RowOfCards:
  def __init__(self):
      """Initializes an empty row (list) of cards."""
      self.cards = []

  def addAtEnd(self, card):
      """Adds the specified card to the end of the row (list)."""
      self.cards.append(card)

  def moveCard(self, card, numSpots):
      """Moves the specified card numSpots to the left."""
      self.cards.index(card)
      (i for i in list if card == self)
      print i
      self.cards.remove(i)
      self.cards.insert(i - numSpots, card)
     
   
  def numCards(self):
      """Returns the number of cards in the row (list)."""
      return len.self.cards
   
  def __str__(self):
      """Returns the string representation of the deck (list)."""
      return ' '.join(self.cards)

Offline mbc

  • Posts: 469
  • Location: Germany
  • -delete-
Need help with Python
« Reply #1 on: Fri, 09 December 2011, 18:01:09 »
card == self ??
ps there is a code tag that would make this a lot more readable
Code: [Select]
def moveCard(self, card, numSpots):
"""Moves the specified card numSpots to the left."""
abc = self.cards.index(card)
if abc - numSpots >= 0
 self.cards.insert(abc-numSpots, card)
 self.cards.pop(abc+1)
« Last Edit: Fri, 09 December 2011, 18:28:17 by mbc »

Offline Son of Gnome

  • Thread Starter
  • Posts: 129
Need help with Python
« Reply #2 on: Fri, 09 December 2011, 22:56:20 »
Got that assignment done thank God lol. Stuck on this one now :(
from cards import DeckOfCards, RowOfCards
   
def skip3():
  """Basic framework for playing Skip3 Solitaire."""
  print "Welcome to Skip3 Solitaire."
 
  deck = DeckOfCards()
  deck.shuffle()
  row = RowOfCards()

  response = ""
  while response != "q":
    print row, "\n"
   
    response = raw_input("Action? ")
    responseWords = response.split()
    firstLetter = responseWords[0][0].lower()
   
    if firstLetter == "d":
      row.addAtEnd(deck.dealCard())
      else print "ERROR"
    elif firstLetter == "m":
      row.moveCard(responseWords[1].upper(), 1)
    elif firstLetter == "s":
      row.moveCard(responseWords[1].upper(), 3)
    elif firstLetter == "q":
      print "Thanks for playing."
      print DeckOfCards()
    else:
      print "Unknown command"

I am trying to make it so if the rules arent fulfilled the game presents an error message of some kind. I know I need to have the program read something I am just unsure of what and how to go about doing it.

Offline hazeluff

  • * Vendor
  • Posts: 2384
  • Location: Vancouver, BC
  • 光復香港
    • Hazeluff
Need help with Python
« Reply #3 on: Fri, 09 December 2011, 23:28:58 »
If you do my VHDL. I'll do your Python. ; p.
Fight For Freedom. Stand with Hong Kongers