Random card game

This is the place for queries that don't fit in any of the other categories.

Random card game

Postby ltcy » Tue Apr 02, 2013 7:17 am

I'm currently working on a random card game project and I got stuck. The program should starts with giving 5 random cards to the user, and then ask the user if he/she want to change the position of the card or not. Then the user should type in the no. of position change, then the program should change the card randomly. for example : AJ891, user typed: 1 , --> A2891.

Here's my code:
Code: Select all
def ask_pos():
    pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
    A_J_Q_K = ["A","J","Q","K"]
    s = random.randint(0,3)
    card = A_J_Q_K[s]

    for i in range(4):
        digit = random.randint(2,10)
        new = random.randint(2,10)
        card = card + str(digit)
        if card[i] == "10" :
            digit = "0"
            for i in range(len(card)):
                if (card[i] == pos_change):
                    card = card + new
           
    return card


the code doesn't work out, can someone please help?
ltcy
 
Posts: 12
Joined: Sat Feb 16, 2013 1:32 am

Re: Random card game

Postby stranac » Tue Apr 02, 2013 10:14 am

ltcy wrote:the code doesn't work out

That's not very descriptive. You'll need to give us more details.
How does it not work? Do you get an error? Is the result different than what you expected?

Also, show the code you're trying to use this function in, preferably something we can run.

But, here are a few hints that could help you:
  • create a function that generates a random card(random.choice() might help)
  • use this function to generate the hand(a list would work better than a string)
  • use this function again to change the selected card, using assignment to a list element, instead of your weird looping:
    Code: Select all
    >>> hand
    ['10', '7', '2', '10', '5']
    >>> hand[2] = random_card()
    >>> hand
    ['10', '7', 'K', '10', '5']
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: Random card game

Postby ltcy » Wed Apr 03, 2013 8:15 am

For changing the position, i re-wrote my code
Code: Select all
def ask_pos(card):
    hand = ["A","0","2","3","4","5","6","7","8","9","J","Q","K"]
    new = random.sample(hand,1)
    pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
   
    while not (pos_change.isdigit()):
        print "Your input must be an integer number"
        pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
        if (pos_change > 4) :
            print "Sorry the value has to be between 0 and 4, please re-type"
            pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
            for i in range(len(card)):
                if card[i] == card[pos_change]:
                    card = card + new
        print "User >>>> ",card
    return card

but the program doesn't show anything and i think it crashed, can someone please help?
stranac wrote:
ltcy wrote:the code doesn't work out

That's not very descriptive. You'll need to give us more details.
How does it not work? Do you get an error? Is the result different than what you expected?

Also, show the code you're trying to use this function in, preferably something we can run.

But, here are a few hints that could help you:
  • create a function that generates a random card(random.choice() might help)
  • use this function to generate the hand(a list would work better than a string)
  • use this function again to change the selected card, using assignment to a list element, instead of your weird looping:
    Code: Select all
    >>> hand
    ['10', '7', '2', '10', '5']
    >>> hand[2] = random_card()
    >>> hand
    ['10', '7', 'K', '10', '5']
ltcy
 
Posts: 12
Joined: Sat Feb 16, 2013 1:32 am

Re: Random card game

Postby stranac » Wed Apr 03, 2013 11:44 am

If that's your whole code, you're not calling the function anywhere, so it's no executed.
You need to actually call your functions for them to run.
Code: Select all
def my_function():
    # some
    # code
    # here

my_function() # calling the function
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests