Skip to content

Instantly share code, notes, and snippets.

@joshtwo
Last active December 14, 2015 04:28
Show Gist options
  • Save joshtwo/5027886 to your computer and use it in GitHub Desktop.
Save joshtwo/5027886 to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
# Joshua Blume; INT 1111 LD04
# IMPORTANT NOTE FOR PROFESSOR THOMPSON:
#
# Python comes in two different major versions: Python 2 and Python 3.
# There are many notable differences between both distributions, which means
# that code written for one version may not work in another. There are two
# differences that I find important to point out with respect to the
# "Python Limited Manual" that you put up in Blackboard:
#
# * in Python 2, printing text to standard output is done with the 'print' statement
# like this:
# >>> print 'Hello World!'
# while in Python 3, it's done with a function called 'print' instead like this:
# >>> print('Hello World!')
#
# * in Python 2, one normally grabs text from standard input using the raw_input
# function like this:
# >>> studentName = raw_input('What is your name? ')
# while in Python 3, it's normally done with the input function:
# >>> studentName = input('What is your name? ')
#
# These very important and basic distinctions between the two versions of Python are
# mixed up in the Python Limited Manual on Blackboard. Not only does it not specify which
# version of Python the code in the examples is, but that example code shows the use of the
# 'print' statement in Python 2 mixed with the use of the 'input' function as it is used in
# Python 3. I assume that many students will be frustrated when they get to class on Monday
# if they hadn't went out of their way to look up the differences on their own when making
# their programs (I happened to know enough Python before this class to already know
# these differences).
#
# I wanted to mention this because it might be something important that needs correction
# in the future of this course.
#
# This program is written for Python 2.7.3, but if one ran 2to3.py on it, it would be a valid Python 3 program.
# I wouldn't imagine you to expect code that's this advanced for our first
# lab assignment, but I wanted to do a little extra to demonstrate
# the knowledge of Python that I already have while also giving a more
# "correct" answer to the problem (for example, adding error checking).
def main():
# I'm using documentation strings, which show up most notably when you
# run help() on a function.
# example: help(main) includes the below message:
"""Starts the program."""
# get basic information from the student
print 'This program will calculate how many credits you need to graduate.'
print "Please enter the following information:"
# you don't pass a second argument to get value for just getting strings
studentName = get_value('> Your name: ')
degreeName = get_value('> Your degree name: ')
# but here I pass a float type constructor to convert the value after I get it
# to numbers, as I can then calculate the needed credits
takenCredits = get_value('> Number of credits taken so far: ', float)
totalCredits = get_value('> Total credits needed for the degree: ', float)
# calculate how many credits are needed
neededCredits = totalCredits - takenCredits
print "\n=== Here are the results ==="
# display necessary information, using printf-style syntax for string interpolation
# student name
print "Student name: %s" % studentName
# degree name
print "Degree name: %s" % degreeName
# credits left to graduate
print "Credits left: %.2f" % neededCredits
# this grabs a value from stdin from the user with checks to ensure its validity
# and optionally using a
def get_value(prompt, converter=None):
"""
Gets a value from the user and then runs the 'converter' function on it.
If the value is empty or an exception is thrown, it'll reprompt the user
for the value.
"""
# this code will continue to ask the user for a value until it gets one
while True: # the loop will be broken out of with the return statement
text = raw_input(prompt)
if text == '':
print "You didn't enter a value. Please try again."
else:
try:
# same as the ternary statement in C++/Java
return converter(text) if converter is not None else text
except ValueError:
# if a user, say, put a string when a number was expected do this
print "The value you entered is invalid. Please try again."
except Exception:
# anything else that happens
print "Something went wrong. Please try again."
# if you want to debug your program in the Python interpreter later, you can
# make it so that the 'main' function isn't ran when you import it in the
# interpreter by checking if the value of __name__ isn't '__main__'.
# __name__ is a variable which stores the name of the module encompassing the current
# scope, and when that module happens to be the entry point of the program (basically,
# you started this file first), that name will be '__main__'. When it isn't, it'll be
# similar to the name of the file. If you imported this module into the interpreter,
# name would have been 'lab1'.
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment