Created
January 18, 2014 17:32
-
-
Save macloo/8493590 to your computer and use it in GitHub Desktop.
These tiny, simple files are derived from Zed Shaw's free online book _Learn Python the Hard Way_. These files accompany Zed's exercises 18 and 19, in which he introduces functions. I wrote a quiz for students to test themselves - see the URL at the top of each file here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # this is the answer to questions 16 and 17 in Practice with Python, Part 2 - Self-Quiz | |
| # see http://bit.ly/mmpython2 for the quiz | |
| def myname(): | |
| print 'Mindy' | |
| print 'McAdams' | |
| def anyname(a, b): | |
| print a | |
| print b | |
| myname() | |
| anyname('Mindy', 'McAdams') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # this is the answer to questions 18 and 19 in Practice with Python, Part 2 - Self-Quiz | |
| # see http://bit.ly/mmpython2 for the quiz | |
| a = 5 | |
| b = 3 | |
| c = 2 | |
| def multiply(x, y): | |
| num1 = x * y | |
| num2 = num1 * x | |
| print num2 | |
| multiply(c, a) | |
| multiply(b, c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # this is an answer to question 20 in Practice with Python, Part 2 - Self-Quiz | |
| # see http://bit.ly/mmpython2 for the quiz | |
| subtotal = raw_input("What is the total before tax and tip? ") | |
| subtotal = int(subtotal) # convert string to integer | |
| mytax = 0.09 # meals tax in Florida is 9%, in many cities | |
| mytip = 0.2 # assuming a 20% tip | |
| def restaurant_calc(base, tax, tip): | |
| sub1 = base * tax | |
| sub2 = base * tip | |
| total = base + sub1 + sub2 | |
| print "$%.2f" % total | |
| # The format string %.2f gives you exactly 2 decimal places | |
| restaurant_calc(subtotal, mytax, mytip) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment