Skip to content

Instantly share code, notes, and snippets.

View TomColBee's full-sized avatar

Thomas Beeson TomColBee

View GitHub Profile
@TomColBee
TomColBee / HealthPotion.py
Last active June 6, 2018 18:40
Python Bible: User Defined Function, Conditional Statements, While Loops, Error Handling
# Creates a function potion which asks you your starting health, the difficulty rating (which determines which range the
# health potion will heal you by. Finally you're asked if you would like to pick up the potion.
# Import the random package to generate random numbers
import random
# Define function potion
def potion(player_health, difficulty, pickup):
# Create health potion and convert to integer
@TomColBee
TomColBee / HelloYou.py
Created June 6, 2018 18:37
Python Bible: Hello You - Strings, Input and Format
# Ask user for name
# Strip removes white space leading or trailing the answer
name = input("Hello, what is your name? ").strip()
# Ask user for age
age = input("How old are you in years? ").strip()
# Ask user for city
city = input("What city do you live in? ").strip()
@TomColBee
TomColBee / EmailSlicer.py
Created June 6, 2018 18:38
Python Bible: Email Slicer - String Index, Format and Print
# Get user email address
email = input("What is your email address?: ").strip()
# Slice out the user name
user_name = email[:email.index("@")]
# Slice the domain name
domain_name = email[email.index("@")+1:]
# Format message
@TomColBee
TomColBee / TravisSystem.py
Created June 10, 2018 19:24
Python Bible: Travis Project - Lists (Create, Append, Remove), Conditional Statements, Loops
# Create a list of known users
known_users = ["Alice","Bob","Claire","Dan","Emma","Fred","Georgie","Harry"]
# How many users are there?
print(len(known_users))
while True:
print("Hi! My name is Travis.")
# Create input varaible name and strip to remove blank spaces and capitalise first letter
@TomColBee
TomColBee / Dict_Practice.py
Created June 12, 2018 18:58
Python Bible: Dictionary Practice
# Create a dictionary: students
students = {
"Alice": ["ID0001", 26, "A"]
, "Bob": ["ID0002", 27, "B"]
, "Claire": ["ID0003", 17, "C"]
, "Dan": ["ID0004", 21, "D"]
, "Emma": ["ID0005", 22, "E"]
}
@TomColBee
TomColBee / Cinema.py
Created June 13, 2018 22:33
Python Bible: Cinema Project - While Loop - Dictionary - Conditional Statements - Input
# List films that we have at the cinema [age requirement, number of tickets available]
films = {
"Finding Dory": {"Age": 3, "Tickets Available": 5},
"Bourne": {"Age": 15, "Tickets Available": 10},
"Tarzan": {"Age": 12, "Tickets Available": 7},
"Ghost Busters": {"Age": 12, "Tickets Available": 2},
"High School Musical 3": {"Age": 5, "Tickets Available": 25},
"The Brother's Grimm": {"Age": 12, "Tickets Available": 3},
"Harry Potter": {"Age": 11, "Tickets Available": 1}
}
@TomColBee
TomColBee / BabySimulator.py
Created June 17, 2018 16:26
Python Bible: Baby Simulator Project - While Loop - Random Choice
# Import choice from random package
from random import choice
# List of questions
questions = [
"Why is the sky blue?: ",
"Why is there a face on the moon?: ",
"Where are all the dinosaurs?: "
]
@TomColBee
TomColBee / SimpleWhileLoops.py
Created June 17, 2018 16:27
Python Bible - Simple While Loop examples
# set x eq to 1
x= 1
# prints if x is less than 11
while x < 11:
print(x)
x = x+1
# prints if x is even but less than 100
while x < 100:
@TomColBee
TomColBee / ForLoopExamples.py
Created June 17, 2018 16:40
Python Bible: For Loops Examples
# print numbers 1 to 50
for number in range(1,50):
print(number)
# print letters a,b,c,d
for letter in 'abcd':
print(letter)
# Count number of vowels and consonants
@TomColBee
TomColBee / ListComprehensionExamples.py
Created June 17, 2018 16:47
Python Bible: List comprehension examples
# Create even numbers
even_numbers = [x for x in range(1,101) if x % 2 == 0]
print(even_numbers)
# Create odd numbers
odd_numbers = [x for x in range(1,101) if x % 2 == 1]
print(odd_numbers)
# Examples of list comprehensions
words = ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]