Reputation: 13
I'm currently working on my Programming Fundamentals w/ Python final project which is a collection of small games. I'm trying to make the game "High Card", where 2 random numbers between 1 & 13(the face values of a 52 card deck) are generated, then compared for the higher number and a winner is displayed.
We are required to have seperate files for the Class and Program code. This is what my Class looks like.
import random
class card:
def __init__(self, value):
self.card_value = value
def deal(self):
numb = random.randrange(1, 13)
def set_value(self, value):
self.card_value = value
def get_value(self):
return self.card_value
def find_face_value(self, numb):
#face = 'test'
print(numb)
if numb == 1:
print("numb == 1")
return 'Ace'
elif numb == 2:
return 'two'
## elif numb == 3:
## face = 'three'
## elif numb == 4:
## face = 'four'
## elif numb == 5:
## face = 'five'
## elif numb == 6:
## face = 'six'
## elif numb == 7:
## face = 'seven'
## elif numb == 8:
## face = 'eight'
## elif numb == 9:
## face = 'nine'
## elif numb == 10:
## face = 'ten'
## elif numb == 11:
## face = 'jack'
## elif numb == 12:
## face = 'queen'
## elif numb == 13:
## face = 'king'
#return face
def __str__(self):
return 'Numeric Value: ' + str(self.card_value)
I've commented out those (if numb == ...) statements b/c I am testing to see if I can get one correct return. And Here is my Program code for the game.
def high_card():
#player1_card = cardClass.card(random.randrange(1, 13))
player1_card = cardClass.card(1)
player2_card = cardClass.card(2)
card1 = player1_card.find_face_value(player1_card)
#player2_card = cardClass.card(random.randrange(1, 13))
card2 = player2_card.find_face_value(player2_card)
print('Player 1 got', card1, player1_card, '\nPlayer 2 got', card2, player2_card)
And this is the output I recieve when I run the high_card() function.
>>> high_card()
Numeric Value: 1
Numeric Value: 2
Player 1 got None Numeric Value: 1
Player 2 got None Numeric Value: 2
My problem is in the last 2 lines of the output. It should say "Player 1 got Ace Numeric Value: 1" and "Player 2 got two Numeric Value: 2". I need the find_face_value function in my class to return a string of the "face" of a card. Instead, Python is printing "None", implying that I did not return anything inside the find_face_value function. I'm stumped on what to do to get this working and would greatly appreciate some help.
Upvotes: 0
Views: 43
Reputation: 1695
card1 = player1_card.find_face_value(player1_card)
In the statement above, numb
is neither 1 nor 2. numb
is a card class (you assigned numb=player1_card). So those if statements inside find_face_value
are both false, thus return values are skipped. Python returns None
by default. You have to check self.card_value
inside if statements, instead of checking numb
.
def find_face_value(self):
if self.card_value == 1:
print("card value is 1")
return 'Ace'
elif self.card_value == 2:
return 'two'
card1 = player1_card.find_face_value()
print(card1)
the statement above should print 'Ace'.
Upvotes: 1