From f4e4849b6968e78956216ab24a1658c981b7df59 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Wed, 2 Sep 2020 18:08:55 -0700 Subject: [PATCH 01/20] Updated File --- src/00_hello.py | 5 ++++- src/01_bignum.py | 3 ++- src/02_datatypes.py | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/00_hello.py b/src/00_hello.py index 268998dfc7..637e206f25 100644 --- a/src/00_hello.py +++ b/src/00_hello.py @@ -1 +1,4 @@ -# Print "Hello, world!" to your terminal \ No newline at end of file +# Print "Hello, world!" to your terminal + +hello = "Hello , world" +print(hello) \ No newline at end of file diff --git a/src/01_bignum.py b/src/01_bignum.py index c020928d63..0347ae3f4b 100644 --- a/src/01_bignum.py +++ b/src/01_bignum.py @@ -1,4 +1,5 @@ # Print out 2 to the 65536 power # (try doing the same thing in the JS console and see what it outputs) -# YOUR CODE HERE \ No newline at end of file +# YOUR CODE HERE +print(2**65536) \ No newline at end of file diff --git a/src/02_datatypes.py b/src/02_datatypes.py index 245193da34..51f9b4b559 100644 --- a/src/02_datatypes.py +++ b/src/02_datatypes.py @@ -14,8 +14,10 @@ # Write a print statement that combines x + y into the integer value 12 # YOUR CODE HERE +print(f'{x} + {y} = 12') # Write a print statement that combines x + y into the string value 57 -# YOUR CODE HERE \ No newline at end of file +# YOUR CODE HERE +print(f'{x}{y}') \ No newline at end of file From 04915340f83e1ccfa08f34fe74162612afc8044d Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Wed, 2 Sep 2020 18:34:39 -0700 Subject: [PATCH 02/20] Finished with Data Types Document --- src/02_datatypes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/02_datatypes.py b/src/02_datatypes.py index 51f9b4b559..60c3402ff7 100644 --- a/src/02_datatypes.py +++ b/src/02_datatypes.py @@ -14,8 +14,8 @@ # Write a print statement that combines x + y into the integer value 12 # YOUR CODE HERE -print(f'{x} + {y} = 12') - +#print(f'{x} + {y} = 12') +print(int(y) + x) # Write a print statement that combines x + y into the string value 57 From 42c1521fe770b570828e95da82c5637099305868 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Wed, 2 Sep 2020 22:33:43 -0700 Subject: [PATCH 03/20] Ready with Printing out the OS and the Python version --- src/03_modules.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/03_modules.py b/src/03_modules.py index 97eba053c7..ebf4eccb99 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -6,17 +6,44 @@ """ import sys +import argparse +import platform +import os # See docs for the sys module: https://docs.python.org/3.7/library/sys.html # Print out the command line arguments in sys.argv, one per line: # YOUR CODE HERE +# Total Arguments: +n = len(sys.argv) +print("Total arguments passed:", n) + +# Arguments passed: +print("\nName of Python script:", sys.argv[0]) + +print("\nArguments passed:", end = " ") +for i in range(1, n): + print(sys.argv[i], end = " ") + +# Addition of numbers: +Sum = 0 +# Using argparse module +for i in range(1, n): + Sum += int(sys.argv[i]) + print("\n\nResult:", Sum) + +# for arg in sys.argv[1:]: +# print(arg) + # Print out the OS platform you're using: # YOUR CODE HERE +print("My OS is:", os.name) # Print out the version of Python you're using: # YOUR CODE HERE +print("My Python Version is:", sys.version) +print("My Python Version Info is:", sys.version_info) import os # See the docs for the OS module: https://docs.python.org/3.7/library/os.html From 4c23759db3f35a2f99b88971cd18ed56167f6350 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Wed, 2 Sep 2020 22:52:43 -0700 Subject: [PATCH 04/20] Modules Completed --- src/03_modules.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/03_modules.py b/src/03_modules.py index ebf4eccb99..95f210b6d0 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -9,6 +9,7 @@ import argparse import platform import os +import getpass # See docs for the sys module: https://docs.python.org/3.7/library/sys.html # Print out the command line arguments in sys.argv, one per line: @@ -51,8 +52,19 @@ # Print the current process ID # YOUR CODE HERE +print("This is my Process ID:", os.getpid()) + # Print the current working directory (cwd): # YOUR CODE HERE +print("My Current working directory:", os.getcwd()) + + # Print out your machine's login name # YOUR CODE HERE + +username = os.getuid() +username2 = getpass.getuser() + +print("My Computer Username2 is:", username2) +print("My Computer Username is:", username) From 59177a47f63771213d9c20468cc680f4554c4778 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Wed, 2 Sep 2020 23:23:22 -0700 Subject: [PATCH 05/20] Printing Completed --- src/04_printing.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/04_printing.py b/src/04_printing.py index 06aaa7ff16..d6fba14a77 100644 --- a/src/04_printing.py +++ b/src/04_printing.py @@ -12,6 +12,16 @@ # y, and z: # x is 10, y is 2.25, z is "I like turtles!" +print("x: %2d, y: %2.5f, z: I like turtles!" %(10, 2.24552)) + +print("==========================================") + # Use the 'format' string method to print the same thing -# Finally, print the same thing using an f-string \ No newline at end of file +print('x = {} \ny = {} \nz = {}'.format(10, 2.24552, 'I like turtles!')) + +print("==========================================") + +# Finally, print the same thing using an f-string + +print(f"x = {x}, \ny = {y}, \nz = {z}") \ No newline at end of file From a4102c15687c2f7375baad5499c5e2c422fde624 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Fri, 4 Sep 2020 23:51:10 -0700 Subject: [PATCH 06/20] Finished Printing, Lists, and Slices --- src/04_printing.py | 6 +++--- src/05_lists.py | 1 + src/07_slices.py | 27 +++++++++++++++++++++------ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/04_printing.py b/src/04_printing.py index d6fba14a77..52402e0299 100644 --- a/src/04_printing.py +++ b/src/04_printing.py @@ -12,16 +12,16 @@ # y, and z: # x is 10, y is 2.25, z is "I like turtles!" -print("x: %2d, y: %2.5f, z: I like turtles!" %(10, 2.24552)) +print("x: %2d, y: %2.2f, z: \"%s\"" %(x, y, z)) print("==========================================") # Use the 'format' string method to print the same thing -print('x = {} \ny = {} \nz = {}'.format(10, 2.24552, 'I like turtles!')) +print("x = {} \ny = {:.2f} \nz = \"{}\"".format(x, y, z)) print("==========================================") # Finally, print the same thing using an f-string -print(f"x = {x}, \ny = {y}, \nz = {z}") \ No newline at end of file +print(f"x = {x}, \ny = {y:.2f}, \nz = \"{z}\"") \ No newline at end of file diff --git a/src/05_lists.py b/src/05_lists.py index cfccc4e945..8cc2ad9729 100644 --- a/src/05_lists.py +++ b/src/05_lists.py @@ -8,6 +8,7 @@ # Change x so that it is [1, 2, 3, 4] # YOUR CODE HERE +x.append(4) print(x) # Using y, change x so that it is [1, 2, 3, 4, 8, 9, 10] diff --git a/src/07_slices.py b/src/07_slices.py index 5e0b3bd8ee..b7f56a7b63 100644 --- a/src/07_slices.py +++ b/src/07_slices.py @@ -15,23 +15,38 @@ print() # Output the second-to-last element: 9 -print() +print(a[-2]) +# print(a[-2]) This print the second to last element of the array, no matter hon long the array is. +# print(a[:4]) This prints [2, 4, 1, 7] +# print(a[4:]) Starts at 4 and includes the las number of the array. # Output the last three elements in the array: [7, 9, 6] -print() +print(a[-3:]) + +myArray = [2, 4, 1, 7, 9, 6] +myNewArray = [] + +for (index, numbers) in enumerate(myArray): + if index >= len(myArray) - 3: + myNewArray.append(numbers) +print(myNewArray) + # Output the two middle elements in the array: [1, 7] -print() +print(a[2:4]) +print(a[2:4:]) # Output every element except the first one: [4, 1, 7, 9, 6] -print() +print(a[1:]) # Output every element except the last one: [2, 4, 1, 7, 9] -print() +print(a[:5]) +print(a[:6:2]) #This is includes all the negative numbers (wrap around) from -1, -2, -3, -4, -5 **** The third number shows how the steping should be. +print(a[:-1]) # For string s... s = "Hello, world!" # Output just the 8th-12th characters: "world" -print() \ No newline at end of file +print(s[7:12]) \ No newline at end of file From 9586e334f253c3ec58e96cfd173b7d0bd90cd854 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Sat, 5 Sep 2020 00:25:34 -0700 Subject: [PATCH 07/20] Finished list --- src/05_lists.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/05_lists.py b/src/05_lists.py index 8cc2ad9729..4767f95fdf 100644 --- a/src/05_lists.py +++ b/src/05_lists.py @@ -13,18 +13,33 @@ # Using y, change x so that it is [1, 2, 3, 4, 8, 9, 10] # YOUR CODE HERE + +# This could be written also like this: +#newArray = [] +#newArray.append(x + y) +# x = newArray + +x += y print(x) # Change x so that it is [1, 2, 3, 4, 9, 10] # YOUR CODE HERE +x.remove(8) print(x) # Change x so that it is [1, 2, 3, 4, 9, 99, 10] # YOUR CODE HERE + +x.insert(5, 99) # We're making sure that we're specifying the index first and the number we want to add to the list. print(x) # Print the length of list x # YOUR CODE HERE +print(len(x)) +print(x.count) + # Print all the values in x multiplied by 1000 -# YOUR CODE HERE \ No newline at end of file +# YOUR CODE HERE + +print(list(map(lambda a: a*1000, x))) \ No newline at end of file From 9e6b7bbfc6c0c225bf3b40610fabb27e8f3158a4 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Sat, 5 Sep 2020 11:10:32 -0700 Subject: [PATCH 08/20] Tumples Completed --- src/06_tuples.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/06_tuples.py b/src/06_tuples.py index 36754da73b..95fa2f2b04 100644 --- a/src/06_tuples.py +++ b/src/06_tuples.py @@ -33,12 +33,14 @@ def dist(a, b): # Write a function `print_tuple` that prints all the values in a tuple - # YOUR CODE HERE +def print_tuple(tuple): + for element in tuple: + print(element) t = (1, 2, 5, 7, 99) print_tuple(t) # Prints 1 2 5 7 99, one per line # Declare a tuple of 1 element then print it -u = (1) # What needs to be added to make this work? +u = (1,) # What needs to be added to make this work? print_tuple(u) From 4761161dee3253f39217c4f53e75da6b977ac2bd Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Sat, 5 Sep 2020 12:16:33 -0700 Subject: [PATCH 09/20] Finished with comprehensions --- src/07_slices.py | 4 ++-- src/08_comprehensions.py | 34 ++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/07_slices.py b/src/07_slices.py index b7f56a7b63..943fd56ea0 100644 --- a/src/07_slices.py +++ b/src/07_slices.py @@ -12,7 +12,7 @@ a = [2, 4, 1, 7, 9, 6] # Output the second element: 4: -print() +print(a[1]) # Output the second-to-last element: 9 print(a[-2]) @@ -33,7 +33,7 @@ # Output the two middle elements in the array: [1, 7] -print(a[2:4]) +print(a[2:5]) print(a[2:4:]) # Output every element except the first one: [4, 1, 7, 9, 6] diff --git a/src/08_comprehensions.py b/src/08_comprehensions.py index 67eb742e50..d9c80f85db 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -9,33 +9,51 @@ """ # Write a list comprehension to produce the array [1, 2, 3, 4, 5] +# Do a For in loop in one line and a regular For in loop. -y = [] +y = [n for n in range(1, 6)] # Same line For in loop. +print ("This is the short version:", y) -print (y) +#Long version: +y1 = [] +for num in range(1, 6): + y1.append(num) + print("This is the long version:", y1) # Write a list comprehension to produce the cubes of the numbers 0-9: # [0, 1, 8, 27, 64, 125, 216, 343, 512, 729] -y = [] +y = [n**3 for n in range(10)] +print("This is the short version:", y) -print(y) +#Long version: +y2 = [] + +for num1 in range(10): + y2.append(num1 ** 3) + print("This is my long version", y2) # Write a list comprehension to produce the uppercase version of all the # elements in array a. Hint: "foo".upper() is "FOO". a = ["foo", "bar", "baz"] -y = [] +y = [word.upper() for word in a] +print("This is the short version:", y) -print(y) +# The long version: +a1 = ["foo", "bar", "baz"] +y3 = [] +for word in a1: + y3.append(word.upper()) + print("This is the long version:", y3) # Use a list comprehension to create a list containing only the _even_ elements # the user entered into list x. +print("Please enter a number:") x = input("Enter comma-separated numbers: ").split(',') # What do you need between the square brackets to make it work? -y = [] - +y = [num for num in x if int(num) %2 == 0] print(y) \ No newline at end of file From c2070e9bc50aa5c263bea5d4b09d0b29a110454f Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Sat, 5 Sep 2020 13:18:24 -0700 Subject: [PATCH 10/20] Finished Dictionaries --- src/09_dictionaries.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index a8b2911f64..b72f93581b 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -15,6 +15,8 @@ - name: a name string for this location """ +import json + waypoints = [ { "lat": 43, @@ -36,12 +38,43 @@ # Add a new waypoint to the list # YOUR CODE HERE + +print("\n\n=========================================") +print("\n\nThis is how a json list is printed out on Python") +waypoints.append( { + + "lat": 120, + "lon": -11, + "name": "A place" +}) + +print("\nThis is my waypoint:", waypoints) + +json_data = '[{"ID":10,"Name":"Pankaj","Role":"CEO"},' \ + '{"ID":20,"Name":"David Lee","Role":"Editor"}]' +json_object = json.loads(json_data) +json_formatted_str = json.dumps(json_object, indent=2) +print(json_formatted_str) + +print("\n\nAnother way to add waypoint to the list: ====================") +anotherPlace = { "lat": 42, "lon": -120, "name": "a fourth place"} +waypoints.append(anotherPlace) +print(waypoints) + + # Modify the dictionary with name "a place" such that its longitude # value is -130 and change its name to "not a real place" # Note: It's okay to access the dictionary using bracket notation on the # waypoints list. # YOUR CODE HERE +waypoints[0]["lon"] = -130 +waypoints[0]["name"] = "not a real place" # Write a loop that prints out all the field values for all the waypoints -# YOUR CODE HERE \ No newline at end of file +# YOUR CODE HERE + +for i, waypoint in enumerate(waypoints): + print(f"Waypoint #{i + 1}") + for key, value in waypoint.items(): + print(f" { key }: {value}") \ No newline at end of file From 90e3b80f3a339aac37cd2d80a927f6295c5ef533 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Sat, 5 Sep 2020 13:37:17 -0700 Subject: [PATCH 11/20] Functions Finished --- src/10_functions.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/10_functions.py b/src/10_functions.py index 5830100c2c..ec179f9bb6 100644 --- a/src/10_functions.py +++ b/src/10_functions.py @@ -1,6 +1,8 @@ # Write a function is_even that will return true if the passed-in number is even. # YOUR CODE HERE +def isEven(n): + return n % 2 == 0 # Read a number from the keyboard num = input("Enter a number: ") @@ -10,3 +12,8 @@ # YOUR CODE HERE +if isEven(num): + print("Even!") +else: + print("Odd!") + From da971386956125df60e31ade0b5c293f77719f3f Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Sat, 5 Sep 2020 22:17:19 -0700 Subject: [PATCH 12/20] args Finished --- src/11_args.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/11_args.py b/src/11_args.py index 8c467ea47f..ad055ccd21 100644 --- a/src/11_args.py +++ b/src/11_args.py @@ -5,6 +5,8 @@ # the sum. This is what you'd consider to be a regular, normal function. # YOUR CODE HERE +def f1(num1, num2): + return num1 + num2 print(f1(1, 2)) @@ -14,6 +16,12 @@ # YOUR CODE HERE +def f2(*args): + sum = 0 + for num in args: + sum += num + return sum + print(f2(1)) # Should print 1 print(f2(1, 3)) # Should print 4 print(f2(1, 4, -12)) # Should print -7 @@ -22,7 +30,7 @@ a = [7, 6, 5, 4] # How do you have to modify the f2 call below to make this work? -print(f2(a)) # Should print 22 +print(f2(*a)) # Should print 22 # Write a function f3 that accepts either one or two arguments. If one argument, # it returns that value plus 1. If two arguments, it returns the sum of the @@ -31,8 +39,11 @@ # YOUR CODE HERE +def f3(num1, num2 = 1): + return num1 + num2 + print(f3(1, 2)) # Should print 3 -print(f3(8)) # Should print 9 +print("This is 9 =", f3(8)) # Should print 9 # Write a function f4 that accepts an arbitrary number of keyword arguments and @@ -44,6 +55,9 @@ # Note: Google "python keyword arguments". # YOUR CODE HERE +def f4(**kwargs): + for key, value in kwargs.items(): + print(f"key: {key}, value: {value}") # Should print # key: a, value: 12 @@ -54,7 +68,7 @@ # key: city, value: Berkeley # key: population, value: 121240 # key: founded, value: "March 23, 1868" -f4(city="Berkeley", population=121240, founded="March 23, 1868") +f4(city = "Berkeley", population = 121240, founded = "March 23, 1868") d = { "monster": "goblin", @@ -62,4 +76,4 @@ } # How do you have to modify the f4 call below to make this work? -f4(d) +f4(**d) From 8531cf229aa11d49e140a91923d746ec67c76cbd Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Sat, 5 Sep 2020 22:22:26 -0700 Subject: [PATCH 13/20] Scopes Finished --- src/12_scopes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/12_scopes.py b/src/12_scopes.py index bc467fa423..adc77a4549 100644 --- a/src/12_scopes.py +++ b/src/12_scopes.py @@ -5,6 +5,7 @@ x = 12 def change_x(): + global x x = 99 change_x() @@ -19,6 +20,7 @@ def outer(): y = 120 def inner(): + nonlocal y y = 999 inner() From 2f8f0083caa5e2e167a4e1e4dcf9c701b6a97183 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Sat, 5 Sep 2020 22:51:03 -0700 Subject: [PATCH 14/20] _file Finished --- src/13_file_io.py | 32 +++++++++++++++++++++++++++++++- src/bar.txt | 3 +++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/bar.txt diff --git a/src/13_file_io.py b/src/13_file_io.py index 3c68f8aba2..70bbb4467e 100644 --- a/src/13_file_io.py +++ b/src/13_file_io.py @@ -10,10 +10,40 @@ # Note: pay close attention to your current directory when trying to open "foo.txt" # YOUR CODE HERE +import os.path + +# get the file relative file to the current script +my_path = os.path.dirname(__file__) +foo_path = os.path.join(my_path, 'foo.txt') + +with open(foo_path) as f: + print('Contents of \'foo.txt\':') + for line in f: + print(' ' + line, end='') + +f.close() # Open up a file called "bar.txt" (which doesn't exist yet) for # writing. Write three lines of arbitrary content to that file, # then close the file. Open up "bar.txt" and inspect it to make # sure that it contains what you expect it to contain -# YOUR CODE HERE \ No newline at end of file +# YOUR CODE HERE +arbitrary_text = '''First line of arbitrary text. +Second line of arbitrary text. +Third line of arbitrary text.''' + +bar_path = os.path.join(my_path, 'bar.txt') + +# Create (or override existing) file and write arbitrary text +with open(bar_path, 'w') as f: + f.write(arbitrary_text) + +f.close() + +# Open newly created file and print its contents +with open(bar_path) as f: + print('\nContents of \'bar.txt\':') + for line in f: + print(' ' + line, end = '') +f.close() \ No newline at end of file diff --git a/src/bar.txt b/src/bar.txt new file mode 100644 index 0000000000..7221260ea2 --- /dev/null +++ b/src/bar.txt @@ -0,0 +1,3 @@ +First line of arbitrary text. +Second line of arbitrary text. +Third line of arbitrary text. \ No newline at end of file From 2b218bc203f2cfeb82c0fc92f16f09f1b0e13dcd Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Sat, 5 Sep 2020 22:59:24 -0700 Subject: [PATCH 15/20] calendar Finished --- src/14_cal.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/14_cal.py b/src/14_cal.py index 30bb10d113..1f7be90a9e 100644 --- a/src/14_cal.py +++ b/src/14_cal.py @@ -29,4 +29,15 @@ import sys import calendar -from datetime import datetime \ No newline at end of file +from datetime import datetime + +num_args = len(sys.argv) - 1 + +if num_args > 2: + print("Usage: 14_cal.py [month] [year]\n\n14_cal.py: error: too many arguments") + sys.exit() + + mm = int(sys.argv[1]) if num_args > 0 else datetime.today().month + yy = int(sys.argv[2]) if num_args > 1 else datetime.today().year + + print(calendar.month(yy, mm)) From 290bbeb8459bdf76b232ffa658f784b6f03697b8 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Sat, 5 Sep 2020 23:23:09 -0700 Subject: [PATCH 16/20] Classes Finished --- src/15_classes.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/15_classes.py b/src/15_classes.py index 2355dd20b7..391dfbfe37 100644 --- a/src/15_classes.py +++ b/src/15_classes.py @@ -2,20 +2,46 @@ # constructor # YOUR CODE HERE +class LatLon: + def _init_(self, lat, lon): + self.lat = lat + self.lon = lon + + def _str_(self): + return f"lat: {self.lat}, lon: {self.lon}" # Make a class Waypoint that can be passed parameters `name`, `lat`, and `lon` to the # constructor. It should inherit from LatLon. Look up the `super` method. # YOUR CODE HERE +class Waypoint(LatLon): + def _init_(self, name, lat, lon): + self.name = name + super()._init_(lat, lon) + + def __str__(self): + return f"Waypoint: {self.name}, {super()._str_()}" # Make a class Geocache that can be passed parameters `name`, `difficulty`, # `size`, `lat`, and `lon` to the constructor. What should it inherit from? # YOUR CODE HERE +class Geocache(Waypoint): + def _init_(self, name, diff, size, lat, lon): + self.diff = diff + self.size = size + super()._init_(name, lat, lon) + + + def __str__(self): + lat_and_lon = super().__str__()[11 + len(self.name):] + return f"Geocache: {self.name}, {lat_and_lon}, diff: {self.diff}, size: {self.size}" # Make a new waypoint and print it out: "Catacombs", 41.70505, -121.51521 # YOUR CODE HERE +waypoint = Waypoint("Catacombs", 41.70505, -121.51521) +print(waypoint) # Without changing the following line, how can you make it print into something # more human-readable? Hint: Look up the `object.__str__` method @@ -24,6 +50,7 @@ # Make a new geocache "Newberry Views", diff 1.5, size 2, 44.052137, -121.41556 # YOUR CODE HERE +geocache = Geocache("Newberry Views", 1.5, 2, 44.052137, -121.41556) # Print it--also make this print more nicely print(geocache) From 322d187775d77bee8865f15726216ed8269bd91e Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Sat, 5 Sep 2020 23:24:58 -0700 Subject: [PATCH 17/20] Updated documents --- src/07_slices.py | 4 ++-- src/11_args.py | 1 + src/14_cal.py | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/07_slices.py b/src/07_slices.py index 943fd56ea0..e0da305f1d 100644 --- a/src/07_slices.py +++ b/src/07_slices.py @@ -33,7 +33,7 @@ # Output the two middle elements in the array: [1, 7] -print(a[2:5]) +print(a[2:4]) print(a[2:4:]) # Output every element except the first one: [4, 1, 7, 9, 6] @@ -42,7 +42,7 @@ # Output every element except the last one: [2, 4, 1, 7, 9] print(a[:5]) print(a[:6:2]) #This is includes all the negative numbers (wrap around) from -1, -2, -3, -4, -5 **** The third number shows how the steping should be. -print(a[:-1]) +print(a[:-1]) #It prints the entire list up to the last number, we're just using the wrap around system with negative numbers using the negative index. # For string s... diff --git a/src/11_args.py b/src/11_args.py index ad055ccd21..d549caff5f 100644 --- a/src/11_args.py +++ b/src/11_args.py @@ -55,6 +55,7 @@ def f3(num1, num2 = 1): # Note: Google "python keyword arguments". # YOUR CODE HERE +print("This is to pass a variable a number of values by not stating exactly how many numbers we're going to get as an input.. **kwargs means unknown") def f4(**kwargs): for key, value in kwargs.items(): print(f"key: {key}, value: {value}") diff --git a/src/14_cal.py b/src/14_cal.py index 1f7be90a9e..8c5b176d95 100644 --- a/src/14_cal.py +++ b/src/14_cal.py @@ -40,4 +40,4 @@ mm = int(sys.argv[1]) if num_args > 0 else datetime.today().month yy = int(sys.argv[2]) if num_args > 1 else datetime.today().year - print(calendar.month(yy, mm)) + print(calendar.month(yy, mm)) \ No newline at end of file From 03971a5854b2c8de17f3889bf8c509c69192649e Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Mon, 7 Sep 2020 22:13:51 -0700 Subject: [PATCH 18/20] Updated the calendar file, now I'm able to see the actual calendar and print out the month and year --- src/08_comprehensions.py | 3 ++- src/14_cal.py | 55 +++++++++++++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/08_comprehensions.py b/src/08_comprehensions.py index d9c80f85db..2f835370df 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -53,7 +53,8 @@ print("Please enter a number:") x = input("Enter comma-separated numbers: ").split(',') +print(x) # What do you need between the square brackets to make it work? y = [num for num in x if int(num) %2 == 0] -print(y) \ No newline at end of file +#print(y) \ No newline at end of file diff --git a/src/14_cal.py b/src/14_cal.py index 8c5b176d95..5a7280023a 100644 --- a/src/14_cal.py +++ b/src/14_cal.py @@ -31,13 +31,54 @@ import calendar from datetime import datetime -num_args = len(sys.argv) - 1 +print("We can do it like this: ") -if num_args > 2: - print("Usage: 14_cal.py [month] [year]\n\n14_cal.py: error: too many arguments") - sys.exit() +# num_args = len(sys.argv) - 1 - mm = int(sys.argv[1]) if num_args > 0 else datetime.today().month - yy = int(sys.argv[2]) if num_args > 1 else datetime.today().year +# if num_args > 2: +# print("Usage: 14_cal.py [month] [year]\n\n14_cal.py: error: too many arguments") +# sys.exit() - print(calendar.month(yy, mm)) \ No newline at end of file +# mm = int(sys.argv[1]) if num_args > 0 else datetime.today().month +# yy = int(sys.argv[2]) if num_args > 1 else datetime.today().year + +# print(calendar.month(yy, mm)) + +print("Or we can do it like this too =================================") + +# Fetch command line arguments for this program: +num_args = len(sys.argv) + +# User didn't pass in any arguments: +if num_args == 1: + # get the current month and year: + month = datetime.now().month + year = datetime.now().year + # Render the cal for that + cal = calendar.TextCalendar() + cal.prmonth(year, month) + + # User passed in one argument: +elif num_args == 2: + # Assume the arg is the specified month + # Render the cal for that month of the current year + year = datetime.now().year + month = int(sys.argv[1]) #index 0 is always the name of the program. NOt what we want right now. + cal = calendar.TextCalendar() + cal.prmonth(year, month) + +# User passed in 2 arguments +elif num_args == 3: + # Render the cal for the specified month and specified year. + month = int(sys.argv[1]) + year = int(sys.argv[2]) + cal = calendar.TextCalendar() + cal.prmonth(year, month) + +# User passed in some other number of arguments: +else: + # Print a usage statement + print("Usage: 14_cal.py [month] [year]") + + # exit the program + sys.exit(1) # 1 means that something incorrect happened in the program. From 6dae45d710829b895db5fd9bba92a81e5d22f176 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Tue, 8 Sep 2020 01:06:27 -0700 Subject: [PATCH 19/20] Added a new file Called Store. Very useful for the first Project --- src/15_classes.py | 14 ++++++-------- src/Store.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 src/Store.py diff --git a/src/15_classes.py b/src/15_classes.py index 391dfbfe37..79495b487b 100644 --- a/src/15_classes.py +++ b/src/15_classes.py @@ -17,10 +17,11 @@ def _str_(self): class Waypoint(LatLon): def _init_(self, name, lat, lon): self.name = name - super()._init_(lat, lon) + super()._init_(lat, lon) # This lines prevents us from building another initializer for the same class LatLon. We're just calling the method from LatLon and saving some time by doing so. +# This is the string representation I would like to use when printing this information: def __str__(self): - return f"Waypoint: {self.name}, {super()._str_()}" + return f"Waypoint: {self.name}, {self.lat}, {self.lon}" # Make a class Geocache that can be passed parameters `name`, `difficulty`, # `size`, `lat`, and `lon` to the constructor. What should it inherit from? @@ -30,18 +31,15 @@ class Geocache(Waypoint): def _init_(self, name, diff, size, lat, lon): self.diff = diff self.size = size - super()._init_(name, lat, lon) - + super()._init_(name, lat, lon) # This line is inheriting from Waypoint, and we're just adding an extra parameter "Difficulty" and " Size" to it. +# This is the string representation I would like to use when printing this information: def __str__(self): - lat_and_lon = super().__str__()[11 + len(self.name):] - return f"Geocache: {self.name}, {lat_and_lon}, diff: {self.diff}, size: {self.size}" + return f"Geocache: Name: {self.name}, Latitude: {self.lat}, Longitude: {self.lon}, diff: {self.diff}, size: {self.size}" # Make a new waypoint and print it out: "Catacombs", 41.70505, -121.51521 - # YOUR CODE HERE waypoint = Waypoint("Catacombs", 41.70505, -121.51521) -print(waypoint) # Without changing the following line, how can you make it print into something # more human-readable? Hint: Look up the `object.__str__` method diff --git a/src/Store.py b/src/Store.py new file mode 100644 index 0000000000..4e04e5bd41 --- /dev/null +++ b/src/Store.py @@ -0,0 +1,45 @@ +class Store: + def _init_(self, name, departments): + self.name = name + self.departments = departments + + def print_welcome(self): + print(f"Welcome to {self.name}! Which deparment would you like to use it?") + + for d in self.departments: + print(d) + +# Should department inherit from Store? +class Department: + def __init__(self, name, products, id): + self.id = id + self.name = name + self.products = products + + + def __str__(self): + return f"{self.id}: {self.name}" + +store = Store("The Dugout", [ + Department(1, "Baseball", []), + Department(2, "Basketball", []), + Department(3, "Football", []), + Department(4, "Golf", []), +]) +# Loop forever while the user is browsing through departments: +# Use the "input" function to prompt the user to select a department to browse + +while True: + # Print a welcome message for the store. + store.print_welcome() + + #get the apartment number the user wants to visit: + selection = input("What department would you like to visit? ") + + # If the user types "Quit", exit the program + if selection == "quit": + break + + chosen_department = store.departments[int(selection) - 1] + + print("You picked the {chosen_department.name} department.") From d92772f85a6d5806f64f5c0d623795b44788d9ee Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Wed, 9 Sep 2020 20:34:19 -0700 Subject: [PATCH 20/20] Modified store --- src/14_cal.py | 13 +++++++------ src/Store.py | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/14_cal.py b/src/14_cal.py index 5a7280023a..67254bb565 100644 --- a/src/14_cal.py +++ b/src/14_cal.py @@ -55,8 +55,6 @@ month = datetime.now().month year = datetime.now().year # Render the cal for that - cal = calendar.TextCalendar() - cal.prmonth(year, month) # User passed in one argument: elif num_args == 2: @@ -64,16 +62,12 @@ # Render the cal for that month of the current year year = datetime.now().year month = int(sys.argv[1]) #index 0 is always the name of the program. NOt what we want right now. - cal = calendar.TextCalendar() - cal.prmonth(year, month) # User passed in 2 arguments elif num_args == 3: # Render the cal for the specified month and specified year. month = int(sys.argv[1]) year = int(sys.argv[2]) - cal = calendar.TextCalendar() - cal.prmonth(year, month) # User passed in some other number of arguments: else: @@ -82,3 +76,10 @@ # exit the program sys.exit(1) # 1 means that something incorrect happened in the program. + +cal = calendar.TextCalendar() +cal.prmonth(year, month) + + # In order to try this we I have to enter this: /usr/local/bin/python3 "/Users/christianlorenzo/Library/Mobile Documents/com~apple~CloudDocs/Python Development/Intro-Python-I/src/14_cal.py" + # And then leave a space and then enter the number for the month that I'm interested in printing out, or the number for the month and the year + diff --git a/src/Store.py b/src/Store.py index 4e04e5bd41..5d382b46c7 100644 --- a/src/Store.py +++ b/src/Store.py @@ -1,5 +1,5 @@ class Store: - def _init_(self, name, departments): + def __init__(self, name, departments): self.name = name self.departments = departments @@ -42,4 +42,4 @@ def __str__(self): chosen_department = store.departments[int(selection) - 1] - print("You picked the {chosen_department.name} department.") + print("You picked the {chosen_department.name} department.\n")