Skip to content

Instantly share code, notes, and snippets.

@ibirnam
Last active September 11, 2019 22:17
Show Gist options
  • Select an option

  • Save ibirnam/e5932da5ba04dc46963951f6c989f910 to your computer and use it in GitHub Desktop.

Select an option

Save ibirnam/e5932da5ba04dc46963951f6c989f910 to your computer and use it in GitHub Desktop.
# CS 1.1 - Code Quality, Reuse, & Error Handling code snippets
def foo(x):
y = 0
for z in x:
if z == 'e':
y = y +1
if y == 2:
return True
else:
return False
def two_e(str):
count = 0
# This loops over each char in the string
for ch in str:
if che == 'e':
count = count +1
if count == 2:
return True
else:
return False
def printevens(x):
y = 0
while(y < len(x)):
if y % 2 == 0:
print(x[y], end = " ")
y += 1
def print_evens(num_lst):
"""
Prints the values of all even index items of a list of numbers
Uses the % operator to determine if a number is even or not.
Parameters:
num_lst (list): list of numbers
Returns:
list: list of only even numbers
"""
index = 0
while index < len(num_lst):
# if the index divided by 2 has no remainder, then it must be an even index
if index % 2 == 0:
print(num_lst[index], end = " ")
index += 1
"""
Summary line.
Extended description of function.
Parameters:
arg1 (int): Description of arg1
Returns:
int: Description of return value
"""
madlib = (madlib.replace(wordtype, input("Enter a {}: ".format(wordtype)), 1))
for x in range(len(stories[story]["words"])):
stories[story]["words"][x] = input(stories[story]["words"][x] + ": ")
exec("""\n\n# print the time\nfrom datetime import datetime\n\ndef what_time():\n\treturn 'Hey what time is it?'\n\nprint(what_time() + '\\n\\t' + str(datetime.now()))\n""")
# print the time
from datetime import datetime
def what_time():
return 'Hey what time is it?'
print(what_time() + '\n\t' + str(datetime.now()))
#<!doctype html>
#<html lang="en">
#<head>
# <meta charset="utf-8">
# <title>The HTML5 Herald</title>
# <meta name="description" content="The HTML5 Herald">
# <meta name="author" content="SitePoint">
# <link rel="stylesheet" href="css/styles.css?v=1.0">
#</head>
#<body>
# <script src="js/scripts.js"></script>
#</body>
#</html>
# Returns the overtime pay a worker gets based on
# the number of overtime hours worked (ot_hours)
def overtime_wage(ot_hours):
pay = 0
if ot_hours < 5:
pay = 400 + (ot_hours * 15)
if ot_hours >= 5 and ot_hours < 10:
pay = 400 + (ot_hours * 20)
else:
pay = 400 + (ot_hours * 25)
return pay
def pay(ot_hours, ot_rate):
return 400 + (ot_hours * ot_rate)
# Returns the overtime pay a worker gets based on
# the number of overtime hours worked (ot_hours)
def overtime_wage(ot_hours):
ot_payment = 0
if ot_hours < 5:
ot_payment = pay(ot_hours, 15)
if ot_hours >= 5 and ot_hours < 10:
ot_payment = pay(ot_hours, 20)
else:
ot_payment = pay(ot_hours, 25)
return ot_payment
# Try running this code
try:
keyboard_interaction()
# if running keyboard_interaction() raises an AssertionError,
# we'll catch it here
except AssertionError as error:
print(error)
# otherwise, we execute the code in the else block
else:
# you can nest try/excepts into the else to catch other errors
try:
with open('file.log') as file:
read_data = file.read()
except FileNotFoundError as fnf_error:
print(fnf_error)
# the code here runs regardless of exceptions being raised or not
finally:
print('Cleaning up, irrespective of any exceptions.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment