5 Replies - 591 Views - Last Post: 09 March 2012 - 04:43 PM Rate Topic: -----

#1 apejam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

compare value of word in list

Posted 09 March 2012 - 11:49 AM

Hello all! I am stuck on some homework and was hoping you guys could shed some light on the matter.

I am trying to create a simple version of the card game WAR, thus far everything functions as it should.. except when I compare the two randomly generated cards. I've researched it a bit and I think I have to use the
list.index

method but I do not know how to implement it into what I have. Another solution I know of, but seems extremely ugly and doesn't seem like the best way to go about doing this is create a function that compares every instance a word is generated then create a value from it.
if card == ('ace'):
    blah = 1
elif etc.. etc..








This is what I have so far, I need help on the comparisons.
Thanks all!

import random

def name1():
    name = input('Player one please enter your name -> ')
    return name


def name2():
    name = input('Player two please enter your name -> ')
    return name


def cardName():
    card = ['ace','two','three','four','five','six','seven','eight','nine','ten','jack','queen','king']
    cardChoice =(random.randint(1,13))
    return card[cardChoice-1]


def suiteName():
    suite = ['Clubs','Diamonds','Hearts','Spades']
    suiteChoice=(random.randint(1,4))
    return suite[suiteChoice-1]


###Main###
player1 = name1()
player2 = name2()

print('')
print('Here we go ...')
print('')


play = ('y')
while play ==('y'):

    p1card = cardName()
    p1suite = suiteName()
    p2card = cardName()
    p2suite = suiteName()
    p1win = 0
    p2win = 0
    tie = 0
    
    

  

    print(player1 +',' +' Your card is:   ' +(p1card) +' of ' + (p1suite))
    print(player2 +',' +' Your card is:   ' +(p2card) +' of ' + (p2suite))
    print('')


###############This is where I am getting messed up, how do I get the value of the random word in the list to compare?   
    
    if p1card > p2card:
       print(player1 + ' has won this round.')
       print('')
       p1win = p1win + 1
    elif p2card > p1card:
        print(player2 + ' has won this round.')
        print('')
        p2win = p2win + 1
    elif p1card == p2card and p1suite > p2suite:
        print(player1 + ' has won this round.')
        print('')
        p1win = p1win + 1
    elif p2card == p1card and p2suite > p1suite:
        print(player2 + ' has won this round.')
        print('')
        p2win = p2win + 1
    else:
        print('it was a tie')
        print('')
        tie = tie + 1

    play = input('Do you want to play again - (y or n)? ')
    print('')
    



Is This A Good Question/Topic? 0
  • +

Replies To: compare value of word in list

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: compare value of word in list

Posted 09 March 2012 - 12:43 PM

If your card list was global, you could do something like this:

if cards.index(p1card) > cards.index(p2card):
    # player 1 wins
elif cards.index(p1card) < cards.index(p2card):
    # player 2 wins
else:
    # tie


Was This Post Helpful? 1
  • +
  • -

#3 apejam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

Re: compare value of word in list

Posted 09 March 2012 - 01:07 PM

View PostMotoma, on 09 March 2012 - 12:43 PM, said:

If your card list was global, you could do something like this:

if cards.index(p1card) > cards.index(p2card):
    # player 1 wins
elif cards.index(p1card) < cards.index(p2card):
    # player 2 wins
else:
    # tie




Once again Motoma you've helped me out! I'm supposed to be avoiding the use of global variables in this assignment but your example pointed me in the right direction, I just made a function of the list and returned it then set a variable to the function

Thanks a lot for your help, I'm sure I'll be asking again in the near future!
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5757
  • View blog
  • Posts: 12,571
  • Joined: 16-October 07

Re: compare value of word in list

Posted 09 March 2012 - 01:17 PM

Card games lend themselves to physical style solutions. Which is to say, object oriented solutions. A card is an object. A deck of cards is a collection of cards. To randomly choice cards, you shuffle the deck and then start dealing from it. A player's hand is also a collection of cards.

Rather than 13 and 4, use 52 and worry out which card it is from a single number. This makes it easy to then just play with lists of numbers. Or, more fun, lists of card objects.
Was This Post Helpful? 0
  • +
  • -

#5 apejam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

Re: compare value of word in list

Posted 09 March 2012 - 01:35 PM

View Postbaavgai, on 09 March 2012 - 01:17 PM, said:

Card games lend themselves to physical style solutions. Which is to say, object oriented solutions. A card is an object. A deck of cards is a collection of cards. To randomly choice cards, you shuffle the deck and then start dealing from it. A player's hand is also a collection of cards.

Rather than 13 and 4, use 52 and worry out which card it is from a single number. This makes it easy to then just play with lists of numbers. Or, more fun, lists of card objects.



it's not technically "true" WAR, it is supposed to be an assignment exercising the use of a list


Wasn't long before I ran into another issue, at the end of the code if the user chooses not to play again it should display the score.. but for some reason it's only taking the last play into account, why is this?






import random

def name1():
    name = input('Player one please enter your name -> ')
    return name


def name2():
    name = input('Player two please enter your name -> ')
    return name


def card():
    card = ['ace','two','three','four','five','six','seven','eight','nine','ten','jack','queen','king']
    return card


def cardName():
    cardChoice =(random.randint(1,13))
    return card[cardChoice-1]



def suite():
    suite = ['Clubs','Diamonds','Hearts','Spades']
    return suite

def suiteName():
    suite = ['Clubs','Diamonds','Hearts','Spades']
    suiteChoice=(random.randint(1,4))
    return suite[suiteChoice-1]




 
        


player1 = name1()
player2 = name2()
card = card()
suite = suite()


print('')
print('Here we go ...')
print('')


play = ('y')

while play ==('y'):

    p1card = cardName()
    p1suite = suiteName()
    p2card = cardName()
    p2suite = suiteName()
    

    

  

    print(player1 +',' +' Your card is:   ' +(p1card) +' of ' + (p1suite))
    print(player2 +',' +' Your card is:   ' +(p2card) +' of ' + (p2suite))
    print('')


   
    p1win = 0
    p2win = 0
    tie = 0

    
    if card.index(p1card) > card.index(p2card):
       print(player1 + ' has won this round.')
       print('')
       p1win = p1win + 1
    elif card.index(p1card) < card.index(p2card):
        print(player2 + ' has won this round.')
        print('')
        p2win = p2win + 1
    elif card.index(p1card) == card.index(p2card) and suite.index(p1suite) > suite.index(p2suite):
        print(player1 + ' has won this round.')
        print('')
        p1win = p1win + 1
    elif card.index(p2card) == card.index(p1card) and suite.index(p1suite) < suite.index(p2suite):
        print(player2 + ' has won this round.')
        print('')
        p2win = p2win + 1
    else:
        print('it was a tie')
        print('')
        tie = tie + 1

    play = input('Do you want to play again - (y or n)? ')
    print('')
    if play == ('n'):
        print(player1 + ' has won ' + str(p1win) + ' rounds')
        print(player2 + ' has won ' + str(p2win) + ' rounds')
        print('there were ' + str(tie) + ' ties')

Was This Post Helpful? 0
  • +
  • -

#6 apejam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

Re: compare value of word in list

Posted 09 March 2012 - 04:43 PM

I figured out the answer to my last question, careless stupid one.

I set the number of times each player wins inside the loop, so every time it looped it was set back to 0, taking it out of the loop fixed it.

Thanks all, this forum is the best resource for computer science help and programming in general that I have found! A gem for learning programmers.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1