#learnpython
@elduvelle hi! Take a look to this, please: https://github.com/JoseMachete/learning_python/blob/main/highest_card.py
The first part of the game works, it draws and prints two random cards. The problem comes when the if /elif statements have to print the results. I'm trying to pass lists made of functions and that second block it just prints nothing. What am I missing here?
Thank you in beforehand!
I think the main confusion comes from understanding what a function is and what it does.
In Python, a function, just like everything else, is an Object. This is why you can put the functions in a list. It's also why printing a function gives you something weird like <function diam_ace ax070>. It's doing what you told it to do, but you weren't expecting it to do that: it printed the "representation" of the function OBJECT. But that is not the same as RUNNING the function.
To run a function, you have to "call" it, and to do that, you just need to state it's name (or a reference to it) with brackets after.
In addition to that, your functions return a value, and you need somewhere to "put" it.
To call your spad_queen function, you'd do:
card = spad_queen()
The "card" variable would then end up with the string 'Queen of Spades', but only after the function had printed out:
------
|Q |
| /\ |
| / \ |
| \/\/ |
| _||_ |
| |
------
But this is a bit difficult where you are "using" your functions because of the way you are keeping them in lists.
You define the "aces" list like this:
aces = [diam_ace, hear_ace, club_ace, spad_ace]
When printed, this will give you the ID of each of those functions. It will NOT run the functions.
You could run the first item in the list by indexing it, and putting brackets after:
aces[0]()
would print:
------
|A |
| /\ |
| / \ |
| \ / |
| \/ |
| |
------
'Ace of Diamonds'
I hope this explains a bit more about what is happening here.
Later on where you choose the cards, you get something you can use, but you don't quite manage to get there:
player_card=random.choice(options)()
computer_card=random.choice(options)()
You have the brackets after the random.choice call, which will run the function chosen! And "player_card" will get the string returned by that function - great!
When you print it, you will see those strings in the card_picks dictionary, but then when you try to work out who won, you are trying to compare a string like 'Ace of Diamonds' to an entire list of function IDs. That's not going to work, because the function IDs, as explained above, bear no relation to what the function DOES or what it RETURNS.
What would work better, is to call the functions when you define card_picks, rather than where you choose the cards. That way "player_card" and "computer_card" would at least contain function IDs:
player_card=random.choice(options)
computer_card=random.choice(options)
card_picks={" Player gets the... ": player_card(), " Computer gets the... ": computer_card()}
(I just moved the brackets).
Now though, you have function IDs you can compare to your lists. But in your if/elif statements, you are trying to see if the chosen function exactly matches the entire lists you mention:
if player_card == aces
asks if "player_card" looks like [diam_ace, hear_ace, club_ace, spad_ace]
which it obviously doesn't, ever.
"==" means "matches exactly", not "is one of these".
The question you want to ask is, "is player_card in aces", or in Python code:
if player_card in aces and computer_card in aces:
That should get you going, and hopefully understanding a bit better what is going on. You still have a couple of issues:
Your functions print stuff out every time they are run, and that is probably not what you want; using functions this way is pretty wack - they are really DATA not functions, and I understand that you are learning, so might not have all the concepts yet, but that is not the best way to do this. A better approach would be to put your cards in a data structure like a dictionary:
cards = {
'Ace of Diamonds': [
' ------ ',
' |A | ',
' | /\ | ',
' | / \ | ',
' | \ / | ',
' | \/ | ',
' | | ',
' ------ '],
....
}
Then you can use the names and the contents to control when you print them. It makes it all a lot easier to handle, and think about.
Hope that helps.