#pythonprogramming hi! Anyone using this tag can help me with a question about #python ? I have a github link to my non-working little game.
#learnpython
@josemachete79 probably! what's your question?

@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!

@josemachete79 @elduvelle

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.

@cybervegan Great explanation 🙌 @josemachete79
@elduvelle @cybervegan wowsers :O that was a great explanation indeed. Goodmorning to you all. #CoffeeTime now, will get my hands on the keyboard later on.
Big thanks to both of you!
@josemachete79 @elduvelle Got to travel soon, but I just thought of something that is very useful: test things out in the Python command-line ("repl") so you can see it in action in real time. Hope you have fun!
@cybervegan @elduvelle hello, my kind folks, reporting back partial success; now the check_win() works as intended... Sometimes. See, while there was only aces/kings/queens/jesters, the list comparison was working. Upon introducing tens, now the operator 'or' is not working as I would like, and now the logic relationship between values is somewhat messed up. Linking to github with the updated file:
https://github.com/JoseMachete/learning_python/blob/main/highest_card.py
Cannot fully get it working in all possible combinations, I mean.
learning_python/highest_card.py at main · JoseMachete/learning_python

small corner to upload some python exercises. Contribute to JoseMachete/learning_python development by creating an account on GitHub.

GitHub

@josemachete79 Logical operators don't work in python like they do in English. You have to repeat the condition.

x in y or z

Does NOT ask if x is in y or z, it asks if x is in y, or z is true. You probably want to say:

x in y or x in z

@cybervegan @elduvelle oh, i get it. Then I have to basically go every possible interaction of cards, if I'm not mistaken?
But this is derived from me using the cards as functions, I guess, right?