From 2a95e51b8c25ed9da8b58a0a33f709d82e68d746 Mon Sep 17 00:00:00 2001 From: Timothy Wangwe Date: Tue, 8 Dec 2020 16:20:59 +0300 Subject: [PATCH 1/6] Ommit unnecessary explicit line break --- .../conditions/multilpe-conditions/or_statements.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) 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) From d05bd371307a891cb15f3fcd0414e9fba70ffbec Mon Sep 17 00:00:00 2001 From: Timothy Wangwe Date: Tue, 8 Dec 2020 16:22:28 +0300 Subject: [PATCH 2/6] Add missing colon --- source/week-1/conditions/single/code_challenge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 1f64b6e5d4531512638a84dfe1809d6fcfc200d6 Mon Sep 17 00:00:00 2001 From: Timothy Wangwe Date: Tue, 8 Dec 2020 16:59:08 +0300 Subject: [PATCH 3/6] Add strftime() time formatting method --- source/week-1/dates/format_date.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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"))) From 6f61462a57f0302bb6d0fc5756cf9e99f202f753 Mon Sep 17 00:00:00 2001 From: Timothy Wangwe Date: Tue, 8 Dec 2020 17:21:05 +0300 Subject: [PATCH 4/6] Add missing colon --- source/week-1/error-handling/syntax.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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") From 4c31c34ba5ee2cfc3222e13298c78a9a18d9be37 Mon Sep 17 00:00:00 2001 From: Timothy Wangwe Date: Tue, 8 Dec 2020 17:22:47 +0300 Subject: [PATCH 5/6] Introduce while loop to test error handling better --- source/week-1/error-handling/runtime.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) 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 From 3a5d79ef79838e55cdd3943d7bbcf5b0d4d89e18 Mon Sep 17 00:00:00 2001 From: Timothy Wangwe Date: Tue, 8 Dec 2020 17:36:44 +0300 Subject: [PATCH 6/6] Add new string formatting method, f-string --- .../week-1/variables/string-variables/f_strings.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 source/week-1/variables/string-variables/f_strings.py 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}.')