Skip to content

Instantly share code, notes, and snippets.

View tmitzka's full-sized avatar

Thomas Mitzka tmitzka

View GitHub Profile
@tmitzka
tmitzka / cat_age_converter.py
Last active April 9, 2023 17:33
Cat age converter
"""Convert cat years to human years."""
# Build conversion dictionary: keys are cat years, values are human years.
conversion = {1: 15}
for age in (2, 3):
conversion[age] = conversion[age - 1] + 6
for age in range(4, 21):
conversion[age] = conversion[age - 1] + 4
@tmitzka
tmitzka / lotto.py
Created June 22, 2019 16:22
Lotto drawings (6 out of 45)
"""Show the number of lotto drawings to win."""
import random
LOTTO_POOL = list(range(1, 50))
lucky_numbers = random.sample(LOTTO_POOL, 6)
lucky_numbers.sort()
my_numbers = []
@tmitzka
tmitzka / guess_my_number.py
Created May 21, 2018 10:33
Guess my number: try to guess a random number as fast as possible.
"""A game in which the player tries to guess a random number."""
import random
def guess_number():
"""Create variables and run the game loop."""
number = random.randint(1, 100)
print("I'm thinking of a number between 1 and 100.")
print("Can you guess it?")
@tmitzka
tmitzka / rps.py
Last active April 15, 2018 19:26
An improved version of Rock, Paper, Scissors.
"""Rock, Paper, Scissors.
There are two playing modes: user vs computer, and computer vs computer.
The game will continue until one player reaches a set score number.
Finally the user can decide whether to start a new game.
2018-03
"""
import random
@tmitzka
tmitzka / hangman.py
Last active April 15, 2018 18:28
A text-based game of Hangman.
"""A game of Hangman."""
import string
import random
import sys
# Define constants.
FILENAME = "words.txt"
WRONG_MAX = 10
@tmitzka
tmitzka / retirement_date.py
Last active April 3, 2018 06:48
This program shows on what day the user will be able to retire.
"""This program shows on what day the user will be able to retire."""
from datetime import datetime
from dateutil.relativedelta import relativedelta
def get_dob():
"""Get the user's date of birth."""
print("Please enter your date of birth (dd.mm.yyyy):")
dob = ""
@tmitzka
tmitzka / cat.py
Created March 14, 2018 21:51
Getting to grips with classes and inheritance
class Cat():
"""A prototype model for a cat."""
def __init__(self, name, age, eye_color, weight_kg = 3):
"""Initialize attributes."""
self.name = name
self.age = age
self.eye_color = eye_color
self.weight_kg = weight_kg
print(f"Welcome, {self.name}!")
@tmitzka
tmitzka / rock_paper_scissors.py
Created February 24, 2018 19:25
A game of Rock, Paper, Scissors against the computer.
#!/usr/bin/env python3
"""A game of Rock, Paper, Scissors against the computer."""
import random
import time
FORMS = ("Rock", "Paper", "Scissors")
@tmitzka
tmitzka / caesar.py
Last active February 11, 2018 10:38
Encrypts a message using a Caesar cipher.
#!/usr/bin/env python3
"""Encrypts a message using a Caesar cipher.
https://en.wikipedia.org/wiki/Caesar_cipher
"""
from string import ascii_uppercase
class Caesar():