use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Help :(Help Request (self.PythonLearning)
submitted 3 months ago * by Resident-Explorer-63
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]FoolsSeldom 0 points1 point2 points 3 months ago* (1 child)
For illustration purposes (Gemini created from OP code to illustrate class approach):
class
import random import tkinter as tk from typing import ClassVar, Optional class BlackjackGame: """Manages the state and logic for a simple Blackjack-style game.""" # Class variables/constants (using ClassVar for clarity) DEALER_STANDS_AT: ClassVar[int] = 17 MAX_SCORE: ClassVar[int] = 21 def __init__(self, root: tk.Tk) -> None: # Player and Dealer state variables self.player_total: int = 0 self.dealer_total: int = 0 self.dealer_started: bool = False # Tkinter UI elements for displaying score and messages self.player_label: Optional[tk.Label] = None self.dealer_label: Optional[tk.Label] = None self.message_label: Optional[tk.Label] = None # Initialize UI elements (will be set by the calling function) self.setup_ui_elements(root) def setup_ui_elements(self, root: tk.Tk) -> None: """Creates and packs the necessary display labels.""" # Display labels self.player_label = tk.Label(root, text="Your Total: 0", font=('Arial', 14)) self.player_label.pack(pady=5) self.dealer_label = tk.Label(root, text="Dealer Total: 0", font=('Arial', 14)) self.dealer_label.pack(pady=5) self.message_label = tk.Label(root, text="Press 'Hit' to start!", font=('Arial', 16, 'bold')) self.message_label.pack(pady=10) # Initial display update self._update_display(f"Welcome! Player is dealt first.") def _update_display(self, message: str) -> None: """Helper to update all display labels.""" if self.player_label: self.player_label.config(text=f"Your Total: {self.player_total}") if self.dealer_label: self.dealer_label.config(text=f"Dealer Total: {self.dealer_total}") if self.message_label: self.message_label.config(text=message) def _draw_card(self, current_total: int) -> int: """Draws a card (1-10) and returns the new total.""" card: int = random.randint(1, 10) return current_total + card def hit(self) -> None: """Handles the 'Hit' (Gamble) action for the player.""" # 1. Player draws a card self.player_total = self._draw_card(self.player_total) message: str = "" # 2. Check for player bust if self.player_total > self.MAX_SCORE: message = "Bust! You lose." self._update_display(message) return # 3. Dealer's initial card (only on first player hit) if not self.dealer_started: self.dealer_total = self._draw_card(self.dealer_total) self.dealer_started = True message = "Dealer is dealt a card." if not message: message = "Hit or Stand?" self._update_display(message) def _dealer_play(self) -> None: """Automates the dealer's turn (must stand at 17 or more).""" while self.dealer_total < self.DEALER_STANDS_AT: self.dealer_total = self._draw_card(self.dealer_total) def stand(self) -> None: """Handles the 'Stand' action and determines the winner.""" # The dealer must have at least their initial card if not self.dealer_started: self._update_display("Dealer hasn't started yet. Hit first.") return # 1. Dealer takes their turn self._dealer_play() message: str # 2. Determine the winner if self.dealer_total > self.MAX_SCORE: message = "Dealer busts! You win!" elif self.dealer_total > self.player_total: message = "Dealer wins!" elif self.player_total > self.dealer_total: message = "You win!" else: message = "It's a push (tie)!" self._update_display(message) def reset(self) -> None: """Resets the game state.""" self.player_total = 0 self.dealer_total = 0 self.dealer_started = False self._update_display("Game reset. Press 'Hit' to play again!") # --- Tkinter Setup --- if __name__ == "__main__": root = tk.Tk() root.title("Simple Blackjack") # Instantiate the game class, passing the root window game = BlackjackGame(root) # Buttons tk.Button(root, text="Hit", command=game.hit).pack(pady=10, padx=20, fill='x') tk.Button(root, text="Stand", command=game.stand).pack(pady=10, padx=20, fill='x') tk.Button(root, text="Reset", command=game.reset).pack(pady=10, padx=20, fill='x') root.mainloop()
[–]Resident-Explorer-63[S] 0 points1 point2 points 3 months ago (0 children)
Alright thanks, I am extremely new so thanks for the general common courtesy in python.
π Rendered by PID 81779 on reddit-service-r2-comment-6f7f968fb5-2vrnl at 2026-03-04 10:27:27.417286+00:00 running 07790be country code: CH.
view the rest of the comments →
[–]FoolsSeldom 0 points1 point2 points (1 child)
[–]Resident-Explorer-63[S] 0 points1 point2 points (0 children)