Kyser Clark
100 Days of Code - Day 73

Today was a medium-length day. In four hours I was able to answer Chapter 4's practice questions, and complete the practice projects in Automate the Boring Stuff with Python by Al Sweigart. I then moved on and read the entire length of chapter 5 as well as completing the practice questions and coding out the practice projects, except the chess project, I didn't feel like doing that one in all honesty. It didn't seem as fun as the fanstay_game_inventory. Below is the code for each short project I completed. These were a lot of fun!
Comma Code:
import sys
def comma_code(choice_list):
"""
Take any python list and print it out without brackets and
separate each index with a comma.
"""
for object in choice_list:
sys.stdout.write(object)
if object != choice_list[-1]:
sys.stdout.write(', ')
spam = ['apples', 'bananas', 'tofu', 'cats']
comma_code(spam)
Coin Flip Streaks:
import random
numberOfStreaks = 0
coin = ['heads', 'tails']
results = []
print('Flipping coin 10,000 times...\n')
for experimentNumber in range(10_000):
# Code that creates a list of 10000 'heads' or 'tails' values.
this_flip = (random.choice(coin))
print(this_flip)
results.append(this_flip)
# Code that checks if there is a streak of 6 heads or tails in a row.
if results[-6:] == ['tails'] * 6 or results[-6:] == ['heads'] * 6:
numberOfStreaks += 1
percentage = numberOfStreaks / 100
print(f'\nNumber of six in a row streaks that happened: {numberOfStreaks}\n')
print(f'Chance of having six in a row: {percentage}')
Character Picture Grid:
grid = [['.', '.', '.', '.', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['O', 'O', 'O', 'O', 'O', '.'],
['.', 'O', 'O', 'O', 'O', 'O'],
['O', 'O', 'O', 'O', 'O', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
# Take grid and rotate it 90 degrees to the right and print it on screen
second_list_count = 0
for i in range(6):
for i in range(9):
print(grid[i][second_list_count], end='')
second_list_count += 1
print('')
Fantasy Game Inventory:
# Inventories
stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
stuff_2 = {'gold coin': 42, 'rope': 1}
# Loot of defeated foes
dragon_loot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
troll_loot = ['gold coin', 'spike bat', 'shield', 'troll horn', 'troll horn', 'arrow']
def display_inventory(inventory):
""" Print out inventory in formatted way. """
print("Inventory:")
item_total = 0
for k, v in inventory.items():
print(f'\t{v} {k}')
item_total += int(v)
print("\nTotal number of items: " + str(item_total))
def add_to_inventory(inventory, added_items):
""" Take defeated foe's loot and add it to inventory. """
for item in added_items:
inventory.setdefault(item, 0)
new_item_value = inventory[item] + 1
inventory[item] = new_item_value
add_to_inventory(stuff, troll_loot)
display_inventory(stuff)
Total Time Dedicated to Python Learning = 208.5 hours