Skip to content

Instantly share code, notes, and snippets.

View nharrell04's full-sized avatar

Nick nharrell04

View GitHub Profile
@nharrell04
nharrell04 / doesnt_work
Created March 18, 2013 22:03
Why does the first one work, but the second doesn't?
pyg = 'ay'
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
if first == "a" or "e" or "i" or "o" or "u":
print "vowel"
else:
@nharrell04
nharrell04 / At a Bare Minimum
Last active December 15, 2015 04:29
I get this error: Traceback (most recent call last): File "python", line 9, in <module> File "python", line 7, in get_min NameError: global name 'balance' is not defined Oops, try again. Did you create a function called get_min? from the messageboard it seems other folks got it to work w/ similar code.
def hotel_cost(nights):
return nights * 140
bill = hotel_cost(5)
def get_min(balance, rate):
return balance * rate
print get_min(bill,.02)
@nharrell04
nharrell04 / dictionary
Created March 27, 2013 03:13
I'm not sure why this works, particularly line 8. I guess expected to need another for loop to run through stock.
prices = {"banana" : 4, "apple" : 2, "orange" : 1.5, "pear" : 3}
stock = {"banana":6, "apple": 0, "orange": 32, "pear" : 15}
for z in prices:
print z
print "prices: "+ str(prices[z])
print "stock: "+ str(stock[z])
print
print
@nharrell04
nharrell04 / distributive
Last active December 15, 2015 15:29
The prompt: 2. Modifying each element in a list in a function This exercise shows how to modify each element in a list. It is useful to do so in a function as you can easily put in a list of any length and get the same functionality. As you can see, len(n) is the length of the list. Create a function called myFun that takes a single argument x (…
n = [3,5,7]
def myFun(x):
z = []
for i in x:
z.append(i*2)
print z
myFun(n)
@nharrell04
nharrell04 / gist:5327038
Created April 6, 2013 18:13
INSTRUCTIONS Write a for loop to go through the characters of the string s and print them. However! If a character is an 'A' or an 'a', print a capital 'X' instead. Note: You'll want to follow the thing you're printing with a comma (,) to prevent your output from including newlines. Check the Hint for more! Hint You can use the same for syntax, …
s = "A bird in the hand..."
# Add your for loop
for x in s:
if x == "A" or x == "a":
print "X",
else:
print x,
@nharrell04
nharrell04 / Battleship
Last active December 18, 2015 18:48
I'm having problems with the second tier if statement on tier 32
from random import randint
board = []
for x in range(0, 5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print " ".join(row)
@nharrell04
nharrell04 / test_prime
Created June 22, 2013 22:29
Determines whether a number is prime
def test_prime(x):
if x <2:
print False
elif x == 2:
print True
# maybe i'm being lazy but i couldn't think of a way to make 2 return as prime w/o doing this.
else:
for i in range(2,x-1):
y = x%i
if y > 0:
@nharrell04
nharrell04 / median
Last active December 18, 2015 22:09
return x[w] TypeError: list indices must be integers, not float
def median(some_list):
x = sorted(some_list)
if len(x)%2 == 0:
q = len(x)/2
t = x[q]
u = x[(q-1)]
v = (t+u)/2.0
return v
else:
w = (len(x)/2)+.5
from urllib2 import urlopen
from hashlib import md5
from collections import namedtuple
class research_guides:
def __init__(self, url, hashes, title, author, subject):
self.url= url
self.hashes = hashes
self.title = title
self.author = author
from urllib2 import urlopen
def gethtml(url):
return urlopen(url).read()
def extract(url):
html = gethtml(url)
return html.find('guides')
# finds the location of the first mention of "guides"