I would still recommend getting away from ... somebody at work called it "primitive obsession". Card aren't strings, and there's a lot of string manipulation being done to pretend that they are.
I would create a "Card" object which included a suit and rank property. I would define a "CrazyEightsCard" class that inherits from "Card" and adds both an "IsWild" and "VirtualSuit" property. Depending on your design philosophy, the "CrazyEightsCard" might get a "CanBePlayedOn" method or that might remain part of the crazy 8s logic. The virtual suit would need to be reset at some point (there are arguments for "during shuffle" and "when played"; the former feels more natural and may prevent certain classes of bugs, while the latter is simpler to implement as the virtual suit really only matters for the "next" card).
I would also create a "Player" class with properties like "Name" and "Hand". Getting the player's choice of card to play in OOP would generally mean sub-classes ("Human", "SuitFirstComputer", "NumberFirstComputer", etc.), but a callback(?) that determines which "get a card" method to use would be reasonable, too.
First off, I'm going to reiterate what I'm not going to design classes for this program, as I still am not confident that for learning purposes it's the right way to begin a programming demonstration. I chose to represent cards as strings because for the most part, this is a text-based program - and in that regard, the info about such cards to be printed out and handled is via strings. Pushing for a "card class" might be an organizational option, but I feel that classes too often obstruct newcomers from the fact that the data you have to handle and control still is primitive numbers and strings.
Until you can prove that designing classes is the best way to
teach programming conceptually, I won't use them in this demonstration.
Classes are an incredibly powerful tool that has the great benefit of being conceptually pretty simple. But, the earlier that tool is encountered, the more likely people will be to use that tool.
Classes simplify design by grouping like functionality while simultaneously allowing for "obviously correct" behavior in a lot of places where primitive obsession doesn't - for example, in this project: "8 of Spades" needs to be broken into the rank ("8") and suit ("Spades") a whole bunch of times. Each time that needs to happen, the code needs to know that "Spades" is capitalized, that there's an "of" in the middle, that the king is "King" rather than "K", etc.; if anyone ever needed to change the program to use "spades" or "K" instead of "King", there are a dozen places that need to be touched. If there were a "card" class, there would need to be exactly one.
Classes are how programmers evolve beyond toy programs. Ignoring them hobbles the budding programmer, just like ignoring a screwdriver hobbles the novice carpenter - after all, a hammer will suffice.
So: I see value in showing the "string" version, then talking about "what about if we want to use 'K' instead of 'King'" and how classes help simplify that transition.
Things like player_turn's if player == "You": print(ui_text(player) ) are concerning because they violate separation of concerns - there's UI logic in the middle of a method that should be concerned with orchestrating other methods (that is, player_turn should delegate "display info" to something that's more UI-focused; adding a GUI to this would require significant rework).
I'm not sure what you mean here. That simply says, "If it is the end-user's turn, Print out the interface for the player's turn." That is, I intended the game to be single player, with one human going against cpu-controlled opponents. I don't need to print out that stuff for the cpu players.
The problem is exactly that the code says "If it is the end-user's turn, Print out the interface for the player's turn." - it puts UI logic ("how do I inform the user that it's their turn?") in the middle of an orchestration method. UI and "business logic" should remain separate so that either can change without forcing a change on the other. In this particular case, granted, neither is likely; merging UI and business logic is a shortcut that works well enough in a toy program but that will absolutely bite the novice programmer in the butt as soon as they leave "toy" world.
Additionally, my predicament about organizing the text interface part of the codebase is mainly the input() function: I only use it during the end user's turn, but not the cpu turns. But given its nature, I cannot simply print the info all at once, as I have to pause and wait for the user to give input in between much of the interface text.
Actually, having a single "input" method works here: there's one single place that would need to change if the UI were to change from pure text to a GUI (or even an ncurses-like UI).
Nitpick, but a mulligan is generally a restart to the game, not simply drawing more cards as a normal part of play (eg., in Magic: The Gathering, players may re-draw their starting hand before normal play begins; IIRC, they draw one fewer cards each time they choose to do so).
Would something like "deck_draw" and "deck_draw_text" work in place of "mulligans"? I must admit that the terminology is kind of weird.
What's actually happening? The current player is drawing cards until they can play or the draw pile is exhausted. So, "draw_until_playable_or_exhausted", maybe? Naming things is hard, and I don't particularly like that one, but it's clear...
There are a lot of "magic strings" (eg., if "8" in discard_pile[0]: on line 308 - "8" is a magic string). Magic strings are concerning: if the value needs to change for whatever reason, it needs to be changed everywhere; also, it's unclear to a future reader why "8" is important.
Comments should explain why, not what - right above that if "8" in discard_pile[0]: on line 308 is a comment that explains what's happening; that should explain why shuffling an 8 back into the deck if it's the initial discard pile is desired. The code itself should be clear enough in what's going on. "What" comments are likely to become obsolete as the exact "what"-ness changes, leading to incorrect documentation; "why" comments are much less likely to become obsolete through code changes IME.
That part of the code simply means, "If the card string in the discard pile contains an 8, it has to be sent into the bottom of the deck, and another card from the top of the deck needs to be drawn onto the discard pile in its place. Afterwards, shuffle the deck." It follows the exact rules of crazy eights as far as I know.
That's just it: the comment explains
what the code is doing. But, that should be obvious from the code. Comments, when they're necessary at all, should explain
why.
As for the "8 is a magic string" complaint, it might make sense to have something that called is_wild that deals with that.
That sounds useful, given how the boolean expression seems to be the same for figuring out whether a card is an 8 or not.
---
As for all the comment complaints, I'm still really confused about how to fix them. I need actual example on what a "good" comment should look like to people here to figure that out. Because I primarily use comments to organize the important steps of the code, and to point out where I really am in a program to prevent myself from getting lost in it.
A good comment explains
why the code exists,
why a given choice was made. ... or,
why a method might get called, in the case of docstring-type comments.
This blog post has some more details and examples, too.
Also, you have a bunch of header style comments, you know things like # Set up the discard pile and its starting card., which to me indicate that you might want to extract a function with that as it's docstring.
Just that: make a method "setup_discard_pile" with a docstring of
# Set up the discard pile and its starting card..
Comments are lousy tools for code organization or explaining
what the code is doing, in virtually every scenario: there is nothing to enforce that any type of comment change when the code changes, so they have a tendency to quickly become out of date. Comments that explain
why the code is doing a particular thing (or
how to call a method) are less likely to become stale simply because the reason for the code is less likely to change (and, the method's signature is, similarly, less likely to change) than the actual
what - the user will still be asked which card they want to play (or whether they want to draw, even though they might not need to!), but the way they're asked might change. Similarly, comments that say "here's what the next few lines do" are highly suggestive of putting the next few lines into a method - instead of a comment that says
# Display the point values of each card:, write a "display_card_values" method and call it; that way, if the call site changes - or the context around the call site does - the "comment" is guaranteed to come along for the ride.
Like with classes and not mixing UI in with flow control, this becomes a lot more apparent when leaving the "toy" world.
And, this is a "toy" program - and that's not a bad thing! They can be a great way to learn concepts and to play around with a new language! But, if you're planning on teaching how to program, stopping with a "toy" is a disservice to your students. Show them the evolution of a simple, straightforward program from "toy" through to quality code that demonstrates the things they'll need if they ever want to contribute to a non-trivial OSS project or work in programming.
The current version of the program is a great second or third step! I would highly recommend keeping it more-or-less as-is as a demonstration of moving from the "toy" world into the ... "adult"? world of programming (along with at least one previous version; this iteration shows a lot of movement in the right direction). But, if I saw this code in a pull request at work, I'd have questions for the author's manager.
... and, that's actually where a lot of "learn to code" tutorials fall apart: they stop with a functioning toy program without any thought given to architecting a project that doesn't fit into 500 lines of code. The novice is then left without the tools they need to better themselves or even the names they need to seek the tools.
Teaching carpentry by explaining how to use a hammer and a saw will, technically, allow the novice to build whatever they want. But: without seeing when a screwdriver might be better than a hammer, or when a chisel or planer might be used - or even that they're things that exist - they'll be stuck doing everything the hard way. Worse, they'll have no idea what to do when a board splits or that there are building codes that they need to follow or how to read industry-standard blueprints. Without exposure to the tools of the trade, they'll never to rise to their potential.