Skip to content

Instantly share code, notes, and snippets.

View davegotz's full-sized avatar

David Gotz davegotz

View GitHub Profile
@davegotz
davegotz / forecast.py
Created February 7, 2019 15:30
Snow or rain or dry?
# Get the chance of precipitation
def get_chance_of_precip():
# Read in precip chance as a float between 0 and 1.
precip = -1
while precip < 0 or precip > 1:
# Read in the chance of precip from the user.
precip = float(input("What is the chance of precipitation [0.0-1.0]:"))
return precip
@davegotz
davegotz / homeprices.py
Created February 7, 2019 15:48
Comparing price of a home to comps
def get_info_about_house_to_buy():
bathrooms = int(input("Enter the number of baths: "))
bedrooms = int(input("Enter the number of beds: "))
return bedrooms, bathrooms
def get_comps(beds, baths):
return 0, 0
@davegotz
davegotz / name_rank.py
Created February 14, 2019 15:48
Determines the popularity of a name.
# Determines the popularity rank for name_to_fine and returns that
# as an integer.
def get_name_rank(name_to_find, name_file):
line = name_file.readline().rstrip('\n')
count = 1
while (line != name_to_find) and (line != ''):
# Read the next line.
line = name_file.readline().rstrip('\n')
count += 1
# Validated input function for numbers 1-10.
# This function will ensure that numbers are:
# 1. Not too big
# 2. Not too small
# 3. Valid integers
def input_integer_1to10():
user_num = None
while user_num == None:
try:
# Validated input function for numbers min-max.
# This function will ensure that numbers are:
# 1. Not too big
# 2. Not too small
# 3. Valid integers
def input_integer_in_range(prompt, min_val, max_val):
user_num = None
while user_num == None:
try:
@davegotz
davegotz / playpen1.py
Created March 5, 2019 18:39
Passing objects (like lists) vs. primitives (like ints) to a function
def convert_oneval_to_int(num_to_convert):
num_to_convert = int(num_to_convert)
return num_to_convert
def convert_vals_to_int(list_to_convert):
#list_to_convert = list(list_to_convert)
# Convert each item in the list to an integer.
@davegotz
davegotz / playpen2.py
Created March 5, 2019 18:40
Functions with "search in a book" example.
import random
def query(book):
query_string = input("What do you want to look for?")
if query_string in book:
print("Yes, it is in the book.")
else:
print("No, it is not in the book")
@davegotz
davegotz / parse_url.py
Created March 7, 2019 01:38
Parsing CSV data from the web.
import urllib.request
import ssl
# Required because the Census website
ssl._create_default_https_context = ssl._create_unverified_context
# If you follow the instructions on this Stackoverflow page, you should be able to omit the line above.
# https://stackoverflow.com/questions/35569042/ssl-certificate-verify-failed-with-python3/43855394#43855394
#
# Go to the folder where Python is installed, e.g., in my case it is installed in the Applications folder with the
# folder name 'Python 3.6'. Now double click on 'Install Certificates.command'. After that error was gone.
@davegotz
davegotz / student_directory.py
Created March 19, 2019 14:21
Example with indexing by name
all_students_by_name[stud_name] = [one_student]
@davegotz
davegotz / university_colors.py
Created March 19, 2019 14:47
Dictionary example with university colors.
# Get university colors from the user and return as a dict
def get_colors():
color_dict = {}
univ_name = input("Enter a university name (or enter to stop): ")
while univ_name != '':
univ_color = input("Enter the color: ")
color_dict[univ_name] = univ_color
# Get the next university name...