diff --git a/source/week-1/conditions/multilpe-conditions/or_statements.py b/source/week-1/conditions/multilpe-conditions/or_statements.py index 5e80934..e3f9b5c 100644 --- a/source/week-1/conditions/multilpe-conditions/or_statements.py +++ b/source/week-1/conditions/multilpe-conditions/or_statements.py @@ -1,10 +1,9 @@ province = input("What province do you live in? ") tax = 0 -if province == 'Alberta' \ - or province == 'Nunavut': - tax = 0.05 -elif province == 'Ontario': - tax = 0.13 +if province == "Alberta" or province == "Nunavut": + tax = 0.05 +elif province == "Ontario": + tax = 0.13 else: - tax = 0.15 + tax = 0.15 print(tax) diff --git a/source/week-1/conditions/single/code_challenge.py b/source/week-1/conditions/single/code_challenge.py index 4d1a515..9fe84df 100644 --- a/source/week-1/conditions/single/code_challenge.py +++ b/source/week-1/conditions/single/code_challenge.py @@ -7,6 +7,6 @@ if price > 1.00: tax = .07 print('Tax rate is: ' + str(tax)) -else +else: tax = 0 print('Tax rate is: ' + str(tax)) \ No newline at end of file diff --git a/source/week-1/dates/format_date.py b/source/week-1/dates/format_date.py index 031cf2b..49baaf4 100644 --- a/source/week-1/dates/format_date.py +++ b/source/week-1/dates/format_date.py @@ -8,10 +8,15 @@ # to display only part of the date # All these functions return integers # Convert them to strings before concatenating them to another string -print('Day: ' + str(today.day)) +print('Date: ' + str(today.day)) print('Month: ' + str(today.month)) print('Year: ' + str(today.year)) print('Hour: ' + str(today.hour)) print('Minute: ' + str(today.minute)) print('Second: ' + str(today.second)) + +# Using the strftime() Method +print("DD/MM/YY: " + str(today.strftime('%d/%m/%y'))) +print("DD Month YYYY: " + str(today.strftime('%d %B %Y'))) +print("MM/DD/YYYY, H:M:S : " + str(today.strftime("%m/%d/%Y, %H:%M:%S"))) diff --git a/source/week-1/error-handling/runtime.py b/source/week-1/error-handling/runtime.py index 33bfd3d..363312b 100644 --- a/source/week-1/error-handling/runtime.py +++ b/source/week-1/error-handling/runtime.py @@ -1,11 +1,21 @@ -x = 42 -y = 0 +# Error Handling try: - print(x / y) -except ZeroDivisionError as e: - # Optionally, log e somewhere - print('Sorry, something went wrong') + print(x) +except NameError: + print("Variable x is not defined") except: - print('Something really went wrong') + print("Something else went wrong") finally: print('This always runs on success or failure') + +# Use while to create an infinite loop that will run +# # until false is returned (correct input in this case) +while True: + try: + x = int(input("Please enter a number: ")) + break + except ValueError: + print("Oops! That was no valid number. Try again...") + +# Instead of letting python to shout at the user "ValueError" +# we catch the anticipated error and display a polite message \ No newline at end of file diff --git a/source/week-1/error-handling/syntax.py b/source/week-1/error-handling/syntax.py index 2f11395..d9c8e32 100644 --- a/source/week-1/error-handling/syntax.py +++ b/source/week-1/error-handling/syntax.py @@ -1,4 +1,4 @@ x = 42 y = 206 -if x == y - print('Success') \ No newline at end of file +if x == y: + print("Success") diff --git a/source/week-1/variables/string-variables/f_strings.py b/source/week-1/variables/string-variables/f_strings.py new file mode 100644 index 0000000..1ae259f --- /dev/null +++ b/source/week-1/variables/string-variables/f_strings.py @@ -0,0 +1,12 @@ +# Only available for python 3.6+ +# This new way of formatting strings lets you +# use embedded Python expressions inside string constants. + +name = input("What is your name? ") +subject = input("What subject do you like? ") +print(f"{name}'s favorite subject is {subject}.") + +a = 5 +b = 10 +sum = a + b +print(f'{a} plus {b} is equal to {sum}.')