Created
September 5, 2016 22:06
-
-
Save s2t2/d2c4d177adc9fe19ba4a660e7b349ffe to your computer and use it in GitHub Desktop.
This file contains 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
# | |
# My awesome program! (basic version without integer input validation) | |
# | |
# ask the user how many cookies: | |
cookieCount = input("HOW MANY COOKIES DO YOU WANT TO MAKE? ") | |
# provide affirmative feedback to the user: | |
print "OK, CALCULATING RECIPE FOR " + str(cookieCount) + " COOKIES ..." | |
# calculate the recipe based on the user input: | |
sugarCups = round((cookieCount * (1.5 / 48)), 2) # should be 1.5 for a batch of 48 cookies | |
butterCups = round((cookieCount * (1.0 / 48)), 2) # should be 1 for a batch of 48 cookies | |
flourCups = round((cookieCount * (2.75 / 48)), 2) # should be 2.75 for a batch of 48 cookies | |
# communicate the recipe to the user: | |
print(" " + str(sugarCups) + " CUPS OF SUGAR") | |
print(" " + str(butterCups) + " CUPS OF BUTTER") | |
print(" " + str(flourCups) + " CUPS OF FLOUR") |
This file contains 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
# | |
# My awesome program! | |
# | |
# ask the user how many cookies | |
cookieCount = input("HOW MANY COOKIES DO YOU WANT TO MAKE? ") | |
print "OK, CALCULATING RECIPE FOR " + str(cookieCount) + " COOKIES ..." | |
try: | |
# make sure the user provided an integer number of cookies. | |
# if cookieCount is not an integer or convertable into an integer, the rest of code in the "try" block will be skipped | |
# ... and the "except" block will be executed instead | |
int(cookieCount) # check if cookieCount is an integer | |
# provide affirmative feedback to the user | |
print "OK, CALCULATING RECIPE FOR " + str(cookieCount) + " COOKIES ..." | |
# calculate the recipe based on the user input | |
sugarCups = round((cookieCount * (1.5 / 48)), 2) # should be 1.5 for a batch of 48 cookies | |
butterCups = round((cookieCount * (1.0 / 48)), 2) # should be 1 for a batch of 48 cookies | |
flourCups = round((cookieCount * (2.75 / 48)), 2) # should be 2.75 for a batch of 48 cookies | |
# communicate the recipe to the user | |
print(" " + str(sugarCups) + " CUPS OF SUGAR") | |
print(" " + str(butterCups) + " CUPS OF BUTTER") | |
print(" " + str(flourCups) + " CUPS OF FLOUR") | |
except ValueError: | |
# this block will be executed if any code within the "try" block throws an error of the type "ValueError", which is the kind of error that gets thrown when calling "int()" on a string | |
print("OOPS, PLEASE PROVIDE AN INTEGER NUMBER OF COOKIES!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment