Skip to content

Instantly share code, notes, and snippets.

@claraj
Last active September 30, 2020 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claraj/b993219477d015d58341c4f3c1950a22 to your computer and use it in GitHub Desktop.
Save claraj/b993219477d015d58341c4f3c1950a22 to your computer and use it in GitHub Desktop.
# Why do we make functions?
# Several reasons, including
# To split a complex task in to simpler subtasks
# To move detail out of the flow of the program
# It can help find bugs in programs - is there a bug in this program?
# Validation is a good example of removing detail
price_per_item = 5
quantity = int(input('Enter quantity, between 0 and 100: '))
# What happens if I type 'cat' instead of a number?
if quantity < 0 or quantity > 100:
print('Error, invalid quantity')
quantity = int(input('Enter quantity, between 0 and 100: '))
total = price_per_item * quantity
print(f'The total for {quantity} items at ${price_per_item} each is ${total}')
# Why do we make functions?
# Several reasons, including
# To split a complex task in to simpler sub-tasks
# To move detail out of the flow of the program
# It can be easier to spot bugs in a function - less code to think about
# Validation is a good example of removing detail
def main():
price_per_item = 5
quantity = get_quantity_between_0_and_100()
total = price_per_item * quantity
print(f'The total for {quantity} items at ${price_per_item} each is ${total}')
def get_quantity_between_0_and_100():
quantity = -1
# What happens if I type 'cat' instead of a number?
while quantity < 0 or quantity > 100:
try:
quantity = int(input('Enter quantity, between 0 and 100: '))
except ValueError:
print('Error - quantity is not an integer')
return quantity
main()
# Why do we make functions?
# Several reasons, including
# To split a complex task in to simpler subtasks
# To move detail out of the flow of the program
# It can be easier to spot bugs in a function - less code to think about
# Validation is a good example of removing detail
def main():
price_per_item = 5
quantity = get_quantity_between_0_and_100()
total = price_per_item * quantity
print(f'The total for {quantity} items at ${price_per_item} each is ${total}')
def get_quantity_between_0_and_100():
quantity = int(input('Enter quantity, between 0 and 100: '))
# What happens if I type 'cat' instead of a number?
while quantity < 0 or quantity > 100:
print('Error, invalid quantity')
quantity = int(input('Enter quantity, between 0 and 100: '))
return quantity
main()
# Why do we make functions?
# Several reasons, including
# To split a complex task in to simpler sub-tasks
# To move detail out of the flow of the program
# It can be easier to spot bugs in a function - less code to think about
# Validation is a good example of removing detail
def main():
price_per_item = 5
quantity = get_quantity_between_0_and_100()
total = calculate_total(price_per_item, quantity) # arguments
print(f'The total for {quantity} items at ${price_per_item} each is ${total}')
def calculate_total(price, number_of_items): # 2 parameters
total = price * number_of_items
return total
def get_quantity_between_0_and_100():
quantity = -1
# What happens if I type 'cat' instead of a number?
while quantity < 0 or quantity > 100:
try:
quantity = int(input('Enter quantity, between 0 and 100: '))
except ValueError:
print('Error - quantity is not an integer')
return quantity
main()
# Print welcome message
print('Welcome! College ID Card Generator Program')
# Get user's name as it should be printed on their ID card.
# Make sure not empty, not longer than 60 characters
name = ''
while len(name) == 0 or len(name) > 60:
name = input('Please enter your name to print on your ID card. \nIt should not be longer than 60 characters: ')
# get star ID. make sure 8 letters!
star_id = ''
while len(star_id) != 8:
star_id = input('Please enter your StarID. Remember it should be 8 characters long: ')
# StarID should be displayed in lowercase
star_id = star_id.lower()
print('Here is a preview of your ID card')
# Print ID Card
college = 'Minneapolis College'
school_image = '🏫'
width = 60
text_width = width - 2
horizontal_line = '+' + '-' * text_width + '+'
spaces = ' ' * text_width
print(horizontal_line)
print(f'|\u001b[35;1m{college.center(text_width)}\u001b[0m|') # https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
print(f'|{school_image.center(text_width)}|')
print(f'|{spaces}|')
print(f'|{name.center(text_width)}|')
print(f'|{spaces}|')
print(f'|{star_id.center(text_width)}|')
print(horizontal_line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment