Skip to content

Instantly share code, notes, and snippets.

View davegotz's full-sized avatar

David Gotz davegotz

View GitHub Profile
@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...
@davegotz
davegotz / student_directory.py
Created March 19, 2019 14:49
Student directory with dictionaries
# Create a student directory by allowing the user to enter all data.
def create_student_directory():
all_students_by_onyen = {}
all_students_by_email = {}
all_students_by_name = {}
# Get as many students as the user wants to enter,
# storing each in “students” onyen as key.
stud_name = input('Enter student name (enter blank line to quit): ')
while stud_name != '':
__author__ = 'David Gotz, gotz@unc.edu, Onyen = gotz'
import random
# Simulates a random card drawing. Returns a dictionary with suit and value
# keys.
def draw_card():
suit = random.choice(["Spades", "Clubs", "Diamonds", "Hearts"])
value = random.randint(1,13)