#2,923 in Computers & technology books
Use arrows to jump to the previous/next product
Reddit mentions of The Python Workbook: A Brief Introduction with Exercises and Solutions
Sentiment score: 1
Reddit mentions: 1
We found 1 Reddit mentions of The Python Workbook: A Brief Introduction with Exercises and Solutions. Here are the top ones.
Buying options
View on Amazon.comor
- 4K Ultra High definition capture - 4X the resolution of 1080P
- Improved H.265 compression codec - same filesize as 1080P @ 60fps
- Built-in GPS & Dual band Wi-Fi - change settings and review video on the fly!
- Built-in impact & motion detection - lock in event recordings
- BlackVue over the cloud compatible - view your videos anywhere in the world over the internet
- Operation Temperature: -20 °C − 70 °C (-4 °F − 158 °F)
Features:
Specs:
Height | 9.21258 Inches |
Length | 6.14172 Inches |
Number of items | 1 |
Weight | 9.60995000058 Pounds |
Width | 0.499999 Inches |
I like little mini-challenges like this which is why I wrote this out for you. Hopefully you can learn from it as you start your journey in learning Python. I wrote this to be pretty basic (i.e. no cool magic tricks, just straight logical code) so that you'll be more likely to understand what it's doing. I also wrote it down and dirty (no error checking). At any rate, I ran this through a number of different rounds, and finally tried 100 rounds where I got 6 wins and 94 losses, so it is possible to win.
​
You'll need Python3 to run this. There's plenty of room for improvement, such as capturing which rounds were wins and which were losses, doing some averages etc.
​
At any rate, good luck on your coding journey. Take a look at The Python Workbook https://www.amazon.com/Python-Workbook-Introduction-Exercises-Solutions/dp/3319142399 which I found a valuable resource of exercises to do once I had the basics under my belt.
Edit: Fixed bug with cards left count and tweaked with some color so it's easier to review output.
"""
A quick down and dirty (no error checking) game of solitaire with the following
rules:
same value as your count e.g. a seven is flipped on the seventh flip, it's
a match and you start your count over at 1 with a new pile.
you pick up the cards, reshuffle and start over.
flipped is the same as your count, it's a WIN, otherwise it's a LOSS.
"""
import random
class TextColor:
PURPLE = str('\033[95m\033[1m')
BLUE = str('\033[94m\033[1m')
GREEN = str('\033[92m\033[1m')
YELLOW = str('\033[93m\033[1m')
RED = str('\033[91m\033[1m')
ENDC = str('\033[0m')
BOLD = str('\033[1m')
UNDERLINE = str('\033[4m')
def buildDeck():
deck = []
Since this game of solitaire doesn't care what the suit is, we don't
need to define a seven of spades. We only care if on the seventh count
during the laydown whether it's a "card" with a value of seven. All we
need to do is make sure the deck has four of every "card" value.
for x in range(4): # Simulate four suits
for y in range(1, 14): # Simulate Ace through King
deck.append(y)
random.shuffle(deck)
return deck
Prep counters
ldc = 1 # Lay Down Count
piles = 1 # Piles created
wins = 0 # How many games won
losses = 0 # How many games lost
restarts = 0 # How many times no matches were found and a new deck is created
rounds = int(input("How many rounds to play? "))
while rounds > 0:
deck = buildDeck()
print(f"Your starting deck is:")
print(deck)
Start laying down cards
while len(deck) > 0:
for c in deck:
if ldc > 13: # No matches, build a new deck
deck = buildDeck()
print(f"{TextColor.YELLOW}New Deck: ", end="")
print(f"{deck}{TextColor.ENDC}")
ldc = 1
restarts += 1
elif ldc == c: # Match found, start a new pile and reset count to 1
deck.remove(c)
print(f"Count: {ldc} - Card: {c} - {len(deck)} cards left{TextColor.PURPLE} <=== MATCH!{TextColor.ENDC}")
if len(deck) > 0: # We've still got deck left
print(f"{TextColor.BLUE}Current Deck: ", end="")
print(f"{deck}{TextColor.ENDC}")
piles += 1
ldc = 1
else: # ldc == c and no cards left, WIN
wins += 1
print(f"{TextColor.GREEN}** WIN **{TextColor.ENDC}")
else: # Not a match, continue on
deck.remove(c)
print(f"Count: {ldc} - Card: {c} - {len(deck)} cards left")
if len(deck) > 0:
ldc += 1
else: # ldc != c and no cards left, LOSS
losses += 1
print(f"{TextColor.RED}** LOSS **{TextColor.ENDC}")
Round finished
rounds -= 1
print("")
print("")
print(f"Piles: {piles}")
print(f"Restarts: {restarts}")
print(f"Wins: {wins}")
print(f"Losses: {losses}")