From 4835521faba94f41edb0615f8a5a8bd49b89a8b5 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 13:13:11 +0900 Subject: [PATCH 01/74] initial afterlecture --- src/day-1-toy/fileio.py | 3 +++ src/day-1-toy/hello.py | 4 +++- src/day-1-toy/lists.py | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/fileio.py b/src/day-1-toy/fileio.py index bc8e79b7cc..5d31523478 100644 --- a/src/day-1-toy/fileio.py +++ b/src/day-1-toy/fileio.py @@ -1,7 +1,10 @@ # Use open to open file "foo.txt" for reading +file = open('foo.txt', 'r') # Print all the lines in the file +for line in file: + print(line) # Close the file diff --git a/src/day-1-toy/hello.py b/src/day-1-toy/hello.py index 37968da4d4..bab8bf269e 100644 --- a/src/day-1-toy/hello.py +++ b/src/day-1-toy/hello.py @@ -1 +1,3 @@ -# Write Hello, world \ No newline at end of file +# Write Hello, world + +print("Hello, world") \ No newline at end of file diff --git a/src/day-1-toy/lists.py b/src/day-1-toy/lists.py index 6076f340a9..2e14eb0e55 100644 --- a/src/day-1-toy/lists.py +++ b/src/day-1-toy/lists.py @@ -8,18 +8,22 @@ # Change x so that it is [1, 2, 3, 4] # [command here] +x.append(4) print(x) # Using y, change x so that it is [1, 2, 3, 4, 8, 9, 10] # [command here] +x.extend(y) #x = x+ y print(x) # Change x so that it is [1, 2, 3, 4, 9, 10] # [command here] +x.remove(8) print(x) # Change x so that it is [1, 2, 3, 4, 9, 99, 10] # [command here] +x.insert(5, 99) print(x) # Print the length of list x From d4bdf924ec73ba136aa566d2508ddfd8318f7b78 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 20:15:44 +0900 Subject: [PATCH 02/74] bignum --- src/day-1-toy/bignum.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/bignum.py b/src/day-1-toy/bignum.py index 77e8d66ffa..f017599473 100644 --- a/src/day-1-toy/bignum.py +++ b/src/day-1-toy/bignum.py @@ -1 +1,3 @@ -# Print out 2 to the 65536 power \ No newline at end of file +# Print out 2 to the 65536 power + +print( 2 ** 65536) \ No newline at end of file From 4e130d1b98bdfb3c2dbcef8a1def14f78c919fb5 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 20:20:04 +0900 Subject: [PATCH 03/74] datatypes --- src/day-1-toy/datatypes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/day-1-toy/datatypes.py b/src/day-1-toy/datatypes.py index f5967611a7..aaa2797b2c 100644 --- a/src/day-1-toy/datatypes.py +++ b/src/day-1-toy/datatypes.py @@ -2,7 +2,7 @@ y = "7" # Write a print statement that combines x + y into the integer value 12 -print(x + y) +print(x + int(y)) # Write a print statement that combines x + y into the string value 57 -print(x + y) \ No newline at end of file +print(str(x) + y) \ No newline at end of file From c21f0b3d6324da826376eeb9f2c3e9669b6d503d Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 20:39:15 +0900 Subject: [PATCH 04/74] modules done --- src/day-1-toy/modules.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/day-1-toy/modules.py b/src/day-1-toy/modules.py index 5313fc1934..593abe9fc1 100644 --- a/src/day-1-toy/modules.py +++ b/src/day-1-toy/modules.py @@ -7,12 +7,13 @@ # Print out the command line arguments in sys.argv, one per line: +print(sys.argv[0]) # Print out the plaform from sys: -print() +print(sys.platform) # Print out the Python version from sys: -print() +print(sys.version_info) @@ -21,11 +22,11 @@ # See the docs for the OS module: https://docs.python.org/3.7/library/os.html # Print the current process ID -print() +print(os.getpid()) # Print the current working directory (cwd): -print() +print(os.getcwd()) # Print your login name -print() +print(os.getlogin()) From 3a94ab49d702c7feac1b122bf2f2d3fb0ef9cfee Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 21:37:26 +0900 Subject: [PATCH 05/74] prinf prints one extra 0 --- src/day-1-toy/printf.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/day-1-toy/printf.py b/src/day-1-toy/printf.py index d4bc9abb48..9e3b7337d2 100644 --- a/src/day-1-toy/printf.py +++ b/src/day-1-toy/printf.py @@ -5,6 +5,7 @@ # Using the printf operator (%), print the following feeding in the values of x, # y, and z: # x is 10, y is 2.25, z is "I like turtles!" +print('x is %d, y is %f, z is %s' % (x, y, z)) - -# Use the 'format' string method to print the same thing \ No newline at end of file +# Use the 'format' string method to print the same thing +print( 'x is {0}, y is {1}, z is "{2}"'.format(x, y, z)) \ No newline at end of file From da903a808254e5f501e133234941a61b7f39a2e6 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 21:51:15 +0900 Subject: [PATCH 06/74] lists --- src/day-1-toy/lists.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/lists.py b/src/day-1-toy/lists.py index 2e14eb0e55..e231f260ff 100644 --- a/src/day-1-toy/lists.py +++ b/src/day-1-toy/lists.py @@ -30,4 +30,7 @@ # [command here] print(len(x)) -# Using a for loop, print all the element values multiplied by 1000 \ No newline at end of file +# Using a for loop, print all the element values multiplied by 1000 + +for i in x: + print(i * 1000) \ No newline at end of file From 254945da904f219055456375be7ed722bd63b8f3 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 22:04:21 +0900 Subject: [PATCH 07/74] working on tuples --- src/day-1-toy/tuples.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/day-1-toy/tuples.py b/src/day-1-toy/tuples.py index ec42b0cdf8..42e4ade8a1 100644 --- a/src/day-1-toy/tuples.py +++ b/src/day-1-toy/tuples.py @@ -23,10 +23,14 @@ def dist(a, b): # Write a function that prints all the values in a tuple # def print_tuple(... +def tuple(t, u): -t = (1, 2, 5, 7, 99) -print_tuple(t) # Prints 1 2 5 7 99, one per line - + t = (1, 2, 5, 7, 99) + #print(print_tuple(t)) # Prints 1 2 5 7 99, one per line + print_tuple(t) # Declare a tuple of 1 element then print it -u = (1) # What needs to be added to make this work? -print_tuple(u) + + + u = (1,) # What needs to be added to make this work? + print(u) + # print_tuple(u) From f3f7e3bc2021dac591ec89d6b2c24ece54f58d19 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Wed, 1 Aug 2018 11:33:08 +0900 Subject: [PATCH 08/74] done printf --- src/day-1-toy/printf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/day-1-toy/printf.py b/src/day-1-toy/printf.py index 9e3b7337d2..3151664a4d 100644 --- a/src/day-1-toy/printf.py +++ b/src/day-1-toy/printf.py @@ -5,7 +5,7 @@ # Using the printf operator (%), print the following feeding in the values of x, # y, and z: # x is 10, y is 2.25, z is "I like turtles!" -print('x is %d, y is %f, z is %s' % (x, y, z)) +print('x is %d, y is %.2f, z is %s' % (x, y, z)) # Use the 'format' string method to print the same thing print( 'x is {0}, y is {1}, z is "{2}"'.format(x, y, z)) \ No newline at end of file From b34bea588a8835c9262f5f130f6e6a2d240a03e1 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Wed, 1 Aug 2018 11:33:20 +0900 Subject: [PATCH 09/74] errors with tuples --- src/day-1-toy/tuples.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/day-1-toy/tuples.py b/src/day-1-toy/tuples.py index 42e4ade8a1..4892a4db7c 100644 --- a/src/day-1-toy/tuples.py +++ b/src/day-1-toy/tuples.py @@ -23,14 +23,15 @@ def dist(a, b): # Write a function that prints all the values in a tuple # def print_tuple(... -def tuple(t, u): +#def tuple(t, u): + +t = (1, 2, 5, 7, 99) +print(t) # Prints 1 2 5 7 99, one per line +# print_tuple(t) - t = (1, 2, 5, 7, 99) - #print(print_tuple(t)) # Prints 1 2 5 7 99, one per line - print_tuple(t) # Declare a tuple of 1 element then print it - u = (1,) # What needs to be added to make this work? - print(u) - # print_tuple(u) +u = (1,) # What needs to be added to make this work? +print(u) +# print_tuple(u) From 174e559274adaa3269059ef22712d8ad7efa455a Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Wed, 1 Aug 2018 12:03:10 +0900 Subject: [PATCH 10/74] slice --- src/day-1-toy/slice.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/day-1-toy/slice.py b/src/day-1-toy/slice.py index 3c6cb38730..d365e775b7 100644 --- a/src/day-1-toy/slice.py +++ b/src/day-1-toy/slice.py @@ -1,26 +1,27 @@ a = [2, 4, 1, 7, 9, 6] # Output the second element: 4: -print() +print(a[1]) # Output the second-to-last element: 9 -print() +print(a[4]) # Output the last three elements in the array: [7, 9, 6] -print() +print(a[3: ]) # Output the two middle elements in the array: [1, 7] -print() +print(a[2:4]) # Output every element except the first one: [4, 1, 7, 9, 6] -print() + +print(a[1:5]) # Output every element except the last one: [2, 4, 1, 7, 9] -print() +print(a[0:4]) # 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 e1566921ed13410ce982698233560f81c6dbdc46 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Wed, 1 Aug 2018 13:05:56 +0900 Subject: [PATCH 11/74] last problem for comp --- src/day-1-toy/comp.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/day-1-toy/comp.py b/src/day-1-toy/comp.py index 083e9b9140..419263a294 100644 --- a/src/day-1-toy/comp.py +++ b/src/day-1-toy/comp.py @@ -1,13 +1,13 @@ # Write a list comprehension to produce the array [1, 2, 3, 4, 5] -y = [] +y = [i for i in range(6)] print (y) # 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 = [ i **3 for i in range(10)] print(y) @@ -16,7 +16,7 @@ a = ["foo", "bar", "baz"] -y = [] +y = [str.upper() for str in a] print(y) @@ -26,7 +26,7 @@ x = input("Enter comma-separated numbers: ").split(',') # What do you need between the square brackets to make it work? -y = [] +y = [x for x in y if x%2 == 0] print(y) From 1916ccfdb50375463299c1dcfe516068e3e0c57e Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 2 Aug 2018 11:10:29 +0900 Subject: [PATCH 12/74] tuples done --- src/day-1-toy/tuples.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/day-1-toy/tuples.py b/src/day-1-toy/tuples.py index 4892a4db7c..40a21611f6 100644 --- a/src/day-1-toy/tuples.py +++ b/src/day-1-toy/tuples.py @@ -23,15 +23,16 @@ def dist(a, b): # Write a function that prints all the values in a tuple # def print_tuple(... -#def tuple(t, u): - t = (1, 2, 5, 7, 99) -print(t) # Prints 1 2 5 7 99, one per line -# print_tuple(t) +def print_tuple(t): + for i in t: + print(i) + # Prints 1 2 5 7 99, one per line +print(print_tuple(t)) # Declare a tuple of 1 element then print it u = (1,) # What needs to be added to make this work? -print(u) -# print_tuple(u) +#print(u) +print_tuple(u) From 5f40a32943c671a4abb2675051dd80fc3edb6a7a Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 2 Aug 2018 12:11:56 +0900 Subject: [PATCH 13/74] func done --- src/day-1-toy/func.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/day-1-toy/func.py b/src/day-1-toy/func.py index 2b7f435ffa..bb4cdadce4 100644 --- a/src/day-1-toy/func.py +++ b/src/day-1-toy/func.py @@ -3,4 +3,10 @@ # Read a number from the keyboard num = input("Enter a number: ") +def is_even(num): + if int(num) % 2 == 0: + return "Even!" + else: + return "Odd" +print(is_even(num)) # Print out "Even!" if the number is even. Otherwise print "Odd" \ No newline at end of file From f69b615970f6ced924547bf7fd93181ef1e5022f Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 2 Aug 2018 13:06:57 +0900 Subject: [PATCH 14/74] attempt dicts --- src/day-1-toy/dicts.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/dicts.py b/src/day-1-toy/dicts.py index eac1779a42..3dc1308330 100644 --- a/src/day-1-toy/dicts.py +++ b/src/day-1-toy/dicts.py @@ -21,9 +21,14 @@ "lat": 43, "lon": -122, "name": "a third place" + }, + { + "lat": 42, + "lon": -333, + "name": "a new world" } ] # Write a loop that prints out all the field values for all the waypoints - +print([key for key in waypoints]) # Add a new waypoint to the list From 2ecde8d7cff7613f5d63907e99a344f23b602fa7 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 2 Aug 2018 13:07:24 +0900 Subject: [PATCH 15/74] complete tuples --- src/day-1-toy/tuples.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/day-1-toy/tuples.py b/src/day-1-toy/tuples.py index 40a21611f6..3d86e23a43 100644 --- a/src/day-1-toy/tuples.py +++ b/src/day-1-toy/tuples.py @@ -28,11 +28,11 @@ def print_tuple(t): for i in t: print(i) # Prints 1 2 5 7 99, one per line -print(print_tuple(t)) + print(print_tuple(t)) # Declare a tuple of 1 element then print it u = (1,) # What needs to be added to make this work? -#print(u) +# print(u) print_tuple(u) From 78e6219c0e001ce504c0d1fefc5b86fb98570829 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 2 Aug 2018 13:07:39 +0900 Subject: [PATCH 16/74] working on args --- src/day-1-toy/args.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/day-1-toy/args.py b/src/day-1-toy/args.py index 06a830e4c8..7e12629591 100644 --- a/src/day-1-toy/args.py +++ b/src/day-1-toy/args.py @@ -5,13 +5,19 @@ # the sum. This is what you'd consider to be a regular, normal function. #def f1(... - -print(f1(1, 2)) +def f1(arg1, arg2): + return arg1 + arg2 +print("hello", f1(1, 2)) # Write a function f2 that takes any number of iteger arguments and prints the # sum. Google for "python arbitrary arguments" and look for "*args" # def f2(... +def f2(*args): + total = 0 + for item in args: + total += item + return total print(f2(1)) # Should print 1 print(f2(1, 3)) # Should print 4 From 40cae8cd62d53e96ecf66663c5e07fefb5906192 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 11:18:50 +0900 Subject: [PATCH 17/74] comp done --- src/day-1-toy/comp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/day-1-toy/comp.py b/src/day-1-toy/comp.py index 419263a294..b4bedadfae 100644 --- a/src/day-1-toy/comp.py +++ b/src/day-1-toy/comp.py @@ -26,7 +26,7 @@ x = input("Enter comma-separated numbers: ").split(',') # What do you need between the square brackets to make it work? -y = [x for x in y if x%2 == 0] +y = [int(i) for i in x if int(i) %2 == 0] print(y) From 407c1101c2618d1b010788582026568ab982a10a Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 11:19:06 +0900 Subject: [PATCH 18/74] args done --- src/day-1-toy/args.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/day-1-toy/args.py b/src/day-1-toy/args.py index 7e12629591..246cfde619 100644 --- a/src/day-1-toy/args.py +++ b/src/day-1-toy/args.py @@ -15,9 +15,9 @@ def f1(arg1, arg2): # def f2(... def f2(*args): total = 0 - for item in args: - total += item - return total + for i in args: + total += i + return total print(f2(1)) # Should print 1 print(f2(1, 3)) # Should print 4 @@ -27,14 +27,22 @@ def f2(*args): a = [7, 6, 5, 4] # What thing do you have to add 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 # arguments. Google "python default arguments" for a hint. #def f3(... - +def f3(*args): + total = 0 + if len(args) > 1: + for i in args: + total += i + else: + for i in args: + total += i + 1 + return total print(f3(1, 2)) # Should print 3 print(f3(8)) # Should print 9 @@ -48,7 +56,9 @@ def f2(*args): # Google "python keyword arguments". #def f4(... - +def f4(**kwargs): + for key, value in kwargs.items(): + print(f'key: {key}, value: {value}') # Should print # key: a, value: 12 # key: b, value: 30 @@ -66,4 +76,4 @@ def f2(*args): } # What thing do you have to add to make this work? -f4(d) \ No newline at end of file +f4(**d) \ No newline at end of file From 13df095261098744dfea10a338940753bbae298e Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 11:20:30 +0900 Subject: [PATCH 19/74] some dict solution, not best --- src/day-1-toy/dicts.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/dicts.py b/src/day-1-toy/dicts.py index 3dc1308330..7c0ac9f483 100644 --- a/src/day-1-toy/dicts.py +++ b/src/day-1-toy/dicts.py @@ -30,5 +30,9 @@ ] # Write a loop that prints out all the field values for all the waypoints -print([key for key in waypoints]) +# print([key for key in waypoints]) +for i in waypoints: + print(i["lat"]) + print(i["lon"]) + print(i["name"]) # Add a new waypoint to the list From e084c65e84991dfa63c8d6c0e8d3d49a2556d0f0 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 18:55:12 +0900 Subject: [PATCH 20/74] scope done --- src/day-1-toy/scope.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/scope.py b/src/day-1-toy/scope.py index 68ecc6c412..38348722f1 100644 --- a/src/day-1-toy/scope.py +++ b/src/day-1-toy/scope.py @@ -5,8 +5,9 @@ x = 12 def changeX(): + global x x = 99 - + changeX() # This prints 12. What do we have to modify in changeX() to get it to print 99? @@ -19,6 +20,7 @@ def outer(): y = 120 def inner(): + nonlocal y y = 999 inner() From c9040b154186c1442cadd1977a95ac3cd136f3cb Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 19:03:29 +0900 Subject: [PATCH 21/74] fileio working --- src/day-1-toy/bar.txt | 3 +++ src/day-1-toy/fileio.py | 10 +++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 src/day-1-toy/bar.txt diff --git a/src/day-1-toy/bar.txt b/src/day-1-toy/bar.txt new file mode 100644 index 0000000000..53681d8af4 --- /dev/null +++ b/src/day-1-toy/bar.txt @@ -0,0 +1,3 @@ +Hi +This is +a new line \ No newline at end of file diff --git a/src/day-1-toy/fileio.py b/src/day-1-toy/fileio.py index 5d31523478..1171142c69 100644 --- a/src/day-1-toy/fileio.py +++ b/src/day-1-toy/fileio.py @@ -6,10 +6,14 @@ for line in file: print(line) # Close the file - +file.close() # Use open to open file "bar.txt" for writing - +file = open('bar.txt', 'w') # Use the write() method to write three lines to the file +file.write("Hi\n") +file.write("This is\n") +file.write("a new line") -# Close the file \ No newline at end of file +# Close the file +file.close() \ No newline at end of file From 9323df7fd2124db78c9c1edfbec4efb40e2cc969 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 19:25:50 +0900 Subject: [PATCH 22/74] something wrong with cal --- src/day-1-toy/cal.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/day-1-toy/cal.py b/src/day-1-toy/cal.py index 2a3771eb5b..e138a9b32d 100644 --- a/src/day-1-toy/cal.py +++ b/src/day-1-toy/cal.py @@ -14,3 +14,20 @@ # docs for the calendar module closely. import sys +import calendar +import datetime + +month = sys.argv[1] +year = sys.argv[2] +print(str(month) + ", " + str(year)) +if month == None: + month = 8 + +if year == None: + year = 2018 + +myCal = calendar.TextCalendar() +print( myCal.formatmonth( int(year), int(month))) + + + From 582a8aed62820dcdc3ff868fe058103a6cf35a41 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 19:47:15 +0900 Subject: [PATCH 23/74] almost ok with obj --- src/day-1-toy/obj.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/day-1-toy/obj.py b/src/day-1-toy/obj.py index 84c78a2f53..9c1f4772fe 100644 --- a/src/day-1-toy/obj.py +++ b/src/day-1-toy/obj.py @@ -1,14 +1,32 @@ # Make a class LatLon that can be passed parameters `lat` and `lon` to the # constructor - +class LatLon: + def __init__(self, lat, lon): + self.lat = lat + self.lon = lon # Make a class Waypoint that can be passed parameters `name`, `lat`, and `lon` to the # constructor. It should inherit from LatLon. +class Waypoint(LatLon): + def __init__(self, name, lat, lon): + LatLon.__init__(self, lat, lon) + self.name = name + def __str__(self): + return "Name: " + self.name + "\tLat: " + str(self.lat) + "\tLon: " + str(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? -# Make a new waypoint "Catacombs", 41.70505, -121.51521 +class Geocache(Waypoint): + def __init__(self, name, difficulty, size, lat, lon): + super().__init__(name, lat, lon) + self.difficulty = difficulty + self.size = size + def __str__(self): + return "Name: " + self.name + "\tDifficulty: " + str(self.difficulty) + "\tSize: " + str(self.size) +# Make a new waypoint "Catacombs", 41.70505, -121.51521 +w = Waypoint("Catacombs", 41.3939, -22222) # Print it # # Without changing the following line, how can you make it print into something @@ -16,6 +34,6 @@ print(w) # Make a new geocache "Newberry Views", diff 1.5, size 2, 44.052137, -121.41556 - +g = Geocache("Newberry Views", 1.5, 2, 33.333, -2222 ) # Print it--also make this print more nicely print(g) From 0e0b58948049ea394722d3d40b5375c418a22e75 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Sun, 5 Aug 2018 20:50:46 +0900 Subject: [PATCH 24/74] args fixed --- src/day-1-toy/args.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/day-1-toy/args.py b/src/day-1-toy/args.py index 246cfde619..2d84cb247f 100644 --- a/src/day-1-toy/args.py +++ b/src/day-1-toy/args.py @@ -34,15 +34,18 @@ def f2(*args): # arguments. Google "python default arguments" for a hint. #def f3(... -def f3(*args): - total = 0 - if len(args) > 1: - for i in args: - total += i - else: - for i in args: - total += i + 1 - return total +# def f3(*args): +# total = 0 +# if len(args) > 1: +# for i in args: +# total += i +# else: +# for i in args: +# total += i + 1 +# return total + +def f3(a, b=1): + return a + b print(f3(1, 2)) # Should print 3 print(f3(8)) # Should print 9 From ad44050c522687b0b21b9c6df6257e462a79e554 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Sun, 5 Aug 2018 20:52:38 +0900 Subject: [PATCH 25/74] another way for bignum --- src/day-1-toy/bignum.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/bignum.py b/src/day-1-toy/bignum.py index f017599473..e0a7f0fe7e 100644 --- a/src/day-1-toy/bignum.py +++ b/src/day-1-toy/bignum.py @@ -1,3 +1,5 @@ # Print out 2 to the 65536 power -print( 2 ** 65536) \ No newline at end of file +print( 2 ** 65536) + +# print (pow(2, 65536)) \ No newline at end of file From 2853d74fe42edbd7e99dae945e81e8d5903dea4a Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 13:13:11 +0900 Subject: [PATCH 26/74] initial afterlecture --- src/day-1-toy/fileio.py | 3 +++ src/day-1-toy/hello.py | 4 +++- src/day-1-toy/lists.py | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/fileio.py b/src/day-1-toy/fileio.py index bc8e79b7cc..5d31523478 100644 --- a/src/day-1-toy/fileio.py +++ b/src/day-1-toy/fileio.py @@ -1,7 +1,10 @@ # Use open to open file "foo.txt" for reading +file = open('foo.txt', 'r') # Print all the lines in the file +for line in file: + print(line) # Close the file diff --git a/src/day-1-toy/hello.py b/src/day-1-toy/hello.py index 37968da4d4..bab8bf269e 100644 --- a/src/day-1-toy/hello.py +++ b/src/day-1-toy/hello.py @@ -1 +1,3 @@ -# Write Hello, world \ No newline at end of file +# Write Hello, world + +print("Hello, world") \ No newline at end of file diff --git a/src/day-1-toy/lists.py b/src/day-1-toy/lists.py index 6076f340a9..2e14eb0e55 100644 --- a/src/day-1-toy/lists.py +++ b/src/day-1-toy/lists.py @@ -8,18 +8,22 @@ # Change x so that it is [1, 2, 3, 4] # [command here] +x.append(4) print(x) # Using y, change x so that it is [1, 2, 3, 4, 8, 9, 10] # [command here] +x.extend(y) #x = x+ y print(x) # Change x so that it is [1, 2, 3, 4, 9, 10] # [command here] +x.remove(8) print(x) # Change x so that it is [1, 2, 3, 4, 9, 99, 10] # [command here] +x.insert(5, 99) print(x) # Print the length of list x From 2169b34f7decb0ae4dc85dd2c1e5f877b9e27493 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 20:15:44 +0900 Subject: [PATCH 27/74] bignum --- src/day-1-toy/bignum.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/bignum.py b/src/day-1-toy/bignum.py index 77e8d66ffa..f017599473 100644 --- a/src/day-1-toy/bignum.py +++ b/src/day-1-toy/bignum.py @@ -1 +1,3 @@ -# Print out 2 to the 65536 power \ No newline at end of file +# Print out 2 to the 65536 power + +print( 2 ** 65536) \ No newline at end of file From 273a478370fd9c155f9b82489fd4e0a18aed403e Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 20:20:04 +0900 Subject: [PATCH 28/74] datatypes --- src/day-1-toy/datatypes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/day-1-toy/datatypes.py b/src/day-1-toy/datatypes.py index f5967611a7..aaa2797b2c 100644 --- a/src/day-1-toy/datatypes.py +++ b/src/day-1-toy/datatypes.py @@ -2,7 +2,7 @@ y = "7" # Write a print statement that combines x + y into the integer value 12 -print(x + y) +print(x + int(y)) # Write a print statement that combines x + y into the string value 57 -print(x + y) \ No newline at end of file +print(str(x) + y) \ No newline at end of file From 47bda8d74707ad944058c8cac12de79341471fbd Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 20:39:15 +0900 Subject: [PATCH 29/74] modules done --- src/day-1-toy/modules.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/day-1-toy/modules.py b/src/day-1-toy/modules.py index 5313fc1934..593abe9fc1 100644 --- a/src/day-1-toy/modules.py +++ b/src/day-1-toy/modules.py @@ -7,12 +7,13 @@ # Print out the command line arguments in sys.argv, one per line: +print(sys.argv[0]) # Print out the plaform from sys: -print() +print(sys.platform) # Print out the Python version from sys: -print() +print(sys.version_info) @@ -21,11 +22,11 @@ # See the docs for the OS module: https://docs.python.org/3.7/library/os.html # Print the current process ID -print() +print(os.getpid()) # Print the current working directory (cwd): -print() +print(os.getcwd()) # Print your login name -print() +print(os.getlogin()) From fcb952cafece4fc8ca2173d287b8f0acb09491df Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 21:37:26 +0900 Subject: [PATCH 30/74] prinf prints one extra 0 --- src/day-1-toy/printf.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/day-1-toy/printf.py b/src/day-1-toy/printf.py index d4bc9abb48..9e3b7337d2 100644 --- a/src/day-1-toy/printf.py +++ b/src/day-1-toy/printf.py @@ -5,6 +5,7 @@ # Using the printf operator (%), print the following feeding in the values of x, # y, and z: # x is 10, y is 2.25, z is "I like turtles!" +print('x is %d, y is %f, z is %s' % (x, y, z)) - -# Use the 'format' string method to print the same thing \ No newline at end of file +# Use the 'format' string method to print the same thing +print( 'x is {0}, y is {1}, z is "{2}"'.format(x, y, z)) \ No newline at end of file From df34f4ed59e3aa20227ae46134036e36b9b2acae Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 21:51:15 +0900 Subject: [PATCH 31/74] lists --- src/day-1-toy/lists.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/lists.py b/src/day-1-toy/lists.py index 2e14eb0e55..e231f260ff 100644 --- a/src/day-1-toy/lists.py +++ b/src/day-1-toy/lists.py @@ -30,4 +30,7 @@ # [command here] print(len(x)) -# Using a for loop, print all the element values multiplied by 1000 \ No newline at end of file +# Using a for loop, print all the element values multiplied by 1000 + +for i in x: + print(i * 1000) \ No newline at end of file From 39d9a3661bcb9473f29c46bad029f68319fd7dff Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 31 Jul 2018 22:04:21 +0900 Subject: [PATCH 32/74] working on tuples --- src/day-1-toy/tuples.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/day-1-toy/tuples.py b/src/day-1-toy/tuples.py index ec42b0cdf8..42e4ade8a1 100644 --- a/src/day-1-toy/tuples.py +++ b/src/day-1-toy/tuples.py @@ -23,10 +23,14 @@ def dist(a, b): # Write a function that prints all the values in a tuple # def print_tuple(... +def tuple(t, u): -t = (1, 2, 5, 7, 99) -print_tuple(t) # Prints 1 2 5 7 99, one per line - + t = (1, 2, 5, 7, 99) + #print(print_tuple(t)) # Prints 1 2 5 7 99, one per line + print_tuple(t) # Declare a tuple of 1 element then print it -u = (1) # What needs to be added to make this work? -print_tuple(u) + + + u = (1,) # What needs to be added to make this work? + print(u) + # print_tuple(u) From 2f1b2fba4bc48378fbdac291fddfd75809c3288c Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Wed, 1 Aug 2018 11:33:08 +0900 Subject: [PATCH 33/74] done printf --- src/day-1-toy/printf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/day-1-toy/printf.py b/src/day-1-toy/printf.py index 9e3b7337d2..3151664a4d 100644 --- a/src/day-1-toy/printf.py +++ b/src/day-1-toy/printf.py @@ -5,7 +5,7 @@ # Using the printf operator (%), print the following feeding in the values of x, # y, and z: # x is 10, y is 2.25, z is "I like turtles!" -print('x is %d, y is %f, z is %s' % (x, y, z)) +print('x is %d, y is %.2f, z is %s' % (x, y, z)) # Use the 'format' string method to print the same thing print( 'x is {0}, y is {1}, z is "{2}"'.format(x, y, z)) \ No newline at end of file From eae7fd148df567d6a87a37052bbf0fd3b41c291f Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Wed, 1 Aug 2018 11:33:20 +0900 Subject: [PATCH 34/74] errors with tuples --- src/day-1-toy/tuples.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/day-1-toy/tuples.py b/src/day-1-toy/tuples.py index 42e4ade8a1..4892a4db7c 100644 --- a/src/day-1-toy/tuples.py +++ b/src/day-1-toy/tuples.py @@ -23,14 +23,15 @@ def dist(a, b): # Write a function that prints all the values in a tuple # def print_tuple(... -def tuple(t, u): +#def tuple(t, u): + +t = (1, 2, 5, 7, 99) +print(t) # Prints 1 2 5 7 99, one per line +# print_tuple(t) - t = (1, 2, 5, 7, 99) - #print(print_tuple(t)) # Prints 1 2 5 7 99, one per line - print_tuple(t) # Declare a tuple of 1 element then print it - u = (1,) # What needs to be added to make this work? - print(u) - # print_tuple(u) +u = (1,) # What needs to be added to make this work? +print(u) +# print_tuple(u) From cb2537e8ed392e906fafd9e0d6bad63a09266123 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Wed, 1 Aug 2018 12:03:10 +0900 Subject: [PATCH 35/74] slice --- src/day-1-toy/slice.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/day-1-toy/slice.py b/src/day-1-toy/slice.py index 3c6cb38730..d365e775b7 100644 --- a/src/day-1-toy/slice.py +++ b/src/day-1-toy/slice.py @@ -1,26 +1,27 @@ a = [2, 4, 1, 7, 9, 6] # Output the second element: 4: -print() +print(a[1]) # Output the second-to-last element: 9 -print() +print(a[4]) # Output the last three elements in the array: [7, 9, 6] -print() +print(a[3: ]) # Output the two middle elements in the array: [1, 7] -print() +print(a[2:4]) # Output every element except the first one: [4, 1, 7, 9, 6] -print() + +print(a[1:5]) # Output every element except the last one: [2, 4, 1, 7, 9] -print() +print(a[0:4]) # 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 aa69fe8c0f2451400b47e304189e820fefe705d3 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Wed, 1 Aug 2018 13:05:56 +0900 Subject: [PATCH 36/74] last problem for comp --- src/day-1-toy/comp.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/day-1-toy/comp.py b/src/day-1-toy/comp.py index 083e9b9140..419263a294 100644 --- a/src/day-1-toy/comp.py +++ b/src/day-1-toy/comp.py @@ -1,13 +1,13 @@ # Write a list comprehension to produce the array [1, 2, 3, 4, 5] -y = [] +y = [i for i in range(6)] print (y) # 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 = [ i **3 for i in range(10)] print(y) @@ -16,7 +16,7 @@ a = ["foo", "bar", "baz"] -y = [] +y = [str.upper() for str in a] print(y) @@ -26,7 +26,7 @@ x = input("Enter comma-separated numbers: ").split(',') # What do you need between the square brackets to make it work? -y = [] +y = [x for x in y if x%2 == 0] print(y) From 0b6f12a7cb2c907db7407dbf0b1594771456ac3c Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 2 Aug 2018 11:10:29 +0900 Subject: [PATCH 37/74] tuples done --- src/day-1-toy/tuples.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/day-1-toy/tuples.py b/src/day-1-toy/tuples.py index 4892a4db7c..40a21611f6 100644 --- a/src/day-1-toy/tuples.py +++ b/src/day-1-toy/tuples.py @@ -23,15 +23,16 @@ def dist(a, b): # Write a function that prints all the values in a tuple # def print_tuple(... -#def tuple(t, u): - t = (1, 2, 5, 7, 99) -print(t) # Prints 1 2 5 7 99, one per line -# print_tuple(t) +def print_tuple(t): + for i in t: + print(i) + # Prints 1 2 5 7 99, one per line +print(print_tuple(t)) # Declare a tuple of 1 element then print it u = (1,) # What needs to be added to make this work? -print(u) -# print_tuple(u) +#print(u) +print_tuple(u) From 9a39ac0dd35c935ad2464de0205638620186e3e5 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 2 Aug 2018 12:11:56 +0900 Subject: [PATCH 38/74] func done --- src/day-1-toy/func.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/day-1-toy/func.py b/src/day-1-toy/func.py index 2b7f435ffa..bb4cdadce4 100644 --- a/src/day-1-toy/func.py +++ b/src/day-1-toy/func.py @@ -3,4 +3,10 @@ # Read a number from the keyboard num = input("Enter a number: ") +def is_even(num): + if int(num) % 2 == 0: + return "Even!" + else: + return "Odd" +print(is_even(num)) # Print out "Even!" if the number is even. Otherwise print "Odd" \ No newline at end of file From 1ab38e2438445a423fe41e4a69d2093d186147c4 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 2 Aug 2018 13:06:57 +0900 Subject: [PATCH 39/74] attempt dicts --- src/day-1-toy/dicts.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/dicts.py b/src/day-1-toy/dicts.py index eac1779a42..3dc1308330 100644 --- a/src/day-1-toy/dicts.py +++ b/src/day-1-toy/dicts.py @@ -21,9 +21,14 @@ "lat": 43, "lon": -122, "name": "a third place" + }, + { + "lat": 42, + "lon": -333, + "name": "a new world" } ] # Write a loop that prints out all the field values for all the waypoints - +print([key for key in waypoints]) # Add a new waypoint to the list From 31deb6f7104fc1aca308d86478aff8c17c610abb Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 2 Aug 2018 13:07:24 +0900 Subject: [PATCH 40/74] complete tuples --- src/day-1-toy/tuples.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/day-1-toy/tuples.py b/src/day-1-toy/tuples.py index 40a21611f6..3d86e23a43 100644 --- a/src/day-1-toy/tuples.py +++ b/src/day-1-toy/tuples.py @@ -28,11 +28,11 @@ def print_tuple(t): for i in t: print(i) # Prints 1 2 5 7 99, one per line -print(print_tuple(t)) + print(print_tuple(t)) # Declare a tuple of 1 element then print it u = (1,) # What needs to be added to make this work? -#print(u) +# print(u) print_tuple(u) From 1a43e218fe3c469169197415ff61cee4e8e1f00c Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 2 Aug 2018 13:07:39 +0900 Subject: [PATCH 41/74] working on args --- src/day-1-toy/args.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/day-1-toy/args.py b/src/day-1-toy/args.py index 06a830e4c8..7e12629591 100644 --- a/src/day-1-toy/args.py +++ b/src/day-1-toy/args.py @@ -5,13 +5,19 @@ # the sum. This is what you'd consider to be a regular, normal function. #def f1(... - -print(f1(1, 2)) +def f1(arg1, arg2): + return arg1 + arg2 +print("hello", f1(1, 2)) # Write a function f2 that takes any number of iteger arguments and prints the # sum. Google for "python arbitrary arguments" and look for "*args" # def f2(... +def f2(*args): + total = 0 + for item in args: + total += item + return total print(f2(1)) # Should print 1 print(f2(1, 3)) # Should print 4 From 16ceaad9d933570e4583611346e4383b26a6e7ef Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 11:18:50 +0900 Subject: [PATCH 42/74] comp done --- src/day-1-toy/comp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/day-1-toy/comp.py b/src/day-1-toy/comp.py index 419263a294..b4bedadfae 100644 --- a/src/day-1-toy/comp.py +++ b/src/day-1-toy/comp.py @@ -26,7 +26,7 @@ x = input("Enter comma-separated numbers: ").split(',') # What do you need between the square brackets to make it work? -y = [x for x in y if x%2 == 0] +y = [int(i) for i in x if int(i) %2 == 0] print(y) From 00a09f5262b9a2264202755a64c04ad8d958157c Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 11:19:06 +0900 Subject: [PATCH 43/74] args done --- src/day-1-toy/args.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/day-1-toy/args.py b/src/day-1-toy/args.py index 7e12629591..246cfde619 100644 --- a/src/day-1-toy/args.py +++ b/src/day-1-toy/args.py @@ -15,9 +15,9 @@ def f1(arg1, arg2): # def f2(... def f2(*args): total = 0 - for item in args: - total += item - return total + for i in args: + total += i + return total print(f2(1)) # Should print 1 print(f2(1, 3)) # Should print 4 @@ -27,14 +27,22 @@ def f2(*args): a = [7, 6, 5, 4] # What thing do you have to add 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 # arguments. Google "python default arguments" for a hint. #def f3(... - +def f3(*args): + total = 0 + if len(args) > 1: + for i in args: + total += i + else: + for i in args: + total += i + 1 + return total print(f3(1, 2)) # Should print 3 print(f3(8)) # Should print 9 @@ -48,7 +56,9 @@ def f2(*args): # Google "python keyword arguments". #def f4(... - +def f4(**kwargs): + for key, value in kwargs.items(): + print(f'key: {key}, value: {value}') # Should print # key: a, value: 12 # key: b, value: 30 @@ -66,4 +76,4 @@ def f2(*args): } # What thing do you have to add to make this work? -f4(d) \ No newline at end of file +f4(**d) \ No newline at end of file From 3f0b51e012b2a1da6085fcd5cc2d6ad6bc464cfe Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 11:20:30 +0900 Subject: [PATCH 44/74] some dict solution, not best --- src/day-1-toy/dicts.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/dicts.py b/src/day-1-toy/dicts.py index 3dc1308330..7c0ac9f483 100644 --- a/src/day-1-toy/dicts.py +++ b/src/day-1-toy/dicts.py @@ -30,5 +30,9 @@ ] # Write a loop that prints out all the field values for all the waypoints -print([key for key in waypoints]) +# print([key for key in waypoints]) +for i in waypoints: + print(i["lat"]) + print(i["lon"]) + print(i["name"]) # Add a new waypoint to the list From 08716305cf431ba72052baf914290bc3f130ad3e Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 18:55:12 +0900 Subject: [PATCH 45/74] scope done --- src/day-1-toy/scope.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/scope.py b/src/day-1-toy/scope.py index 68ecc6c412..38348722f1 100644 --- a/src/day-1-toy/scope.py +++ b/src/day-1-toy/scope.py @@ -5,8 +5,9 @@ x = 12 def changeX(): + global x x = 99 - + changeX() # This prints 12. What do we have to modify in changeX() to get it to print 99? @@ -19,6 +20,7 @@ def outer(): y = 120 def inner(): + nonlocal y y = 999 inner() From 312d47bcecda036cff4e253780fb4bb3a2bbc753 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 19:03:29 +0900 Subject: [PATCH 46/74] fileio working --- src/day-1-toy/bar.txt | 3 +++ src/day-1-toy/fileio.py | 10 +++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 src/day-1-toy/bar.txt diff --git a/src/day-1-toy/bar.txt b/src/day-1-toy/bar.txt new file mode 100644 index 0000000000..53681d8af4 --- /dev/null +++ b/src/day-1-toy/bar.txt @@ -0,0 +1,3 @@ +Hi +This is +a new line \ No newline at end of file diff --git a/src/day-1-toy/fileio.py b/src/day-1-toy/fileio.py index 5d31523478..1171142c69 100644 --- a/src/day-1-toy/fileio.py +++ b/src/day-1-toy/fileio.py @@ -6,10 +6,14 @@ for line in file: print(line) # Close the file - +file.close() # Use open to open file "bar.txt" for writing - +file = open('bar.txt', 'w') # Use the write() method to write three lines to the file +file.write("Hi\n") +file.write("This is\n") +file.write("a new line") -# Close the file \ No newline at end of file +# Close the file +file.close() \ No newline at end of file From daf2916f7c53f8e5292d58ed8e5b66bed2b19102 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 19:25:50 +0900 Subject: [PATCH 47/74] something wrong with cal --- src/day-1-toy/cal.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/day-1-toy/cal.py b/src/day-1-toy/cal.py index 2a3771eb5b..e138a9b32d 100644 --- a/src/day-1-toy/cal.py +++ b/src/day-1-toy/cal.py @@ -14,3 +14,20 @@ # docs for the calendar module closely. import sys +import calendar +import datetime + +month = sys.argv[1] +year = sys.argv[2] +print(str(month) + ", " + str(year)) +if month == None: + month = 8 + +if year == None: + year = 2018 + +myCal = calendar.TextCalendar() +print( myCal.formatmonth( int(year), int(month))) + + + From c9a30c2a55656452d2eac30461675b1dfbfaa83b Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 3 Aug 2018 19:47:15 +0900 Subject: [PATCH 48/74] almost ok with obj --- src/day-1-toy/obj.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/day-1-toy/obj.py b/src/day-1-toy/obj.py index 84c78a2f53..9c1f4772fe 100644 --- a/src/day-1-toy/obj.py +++ b/src/day-1-toy/obj.py @@ -1,14 +1,32 @@ # Make a class LatLon that can be passed parameters `lat` and `lon` to the # constructor - +class LatLon: + def __init__(self, lat, lon): + self.lat = lat + self.lon = lon # Make a class Waypoint that can be passed parameters `name`, `lat`, and `lon` to the # constructor. It should inherit from LatLon. +class Waypoint(LatLon): + def __init__(self, name, lat, lon): + LatLon.__init__(self, lat, lon) + self.name = name + def __str__(self): + return "Name: " + self.name + "\tLat: " + str(self.lat) + "\tLon: " + str(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? -# Make a new waypoint "Catacombs", 41.70505, -121.51521 +class Geocache(Waypoint): + def __init__(self, name, difficulty, size, lat, lon): + super().__init__(name, lat, lon) + self.difficulty = difficulty + self.size = size + def __str__(self): + return "Name: " + self.name + "\tDifficulty: " + str(self.difficulty) + "\tSize: " + str(self.size) +# Make a new waypoint "Catacombs", 41.70505, -121.51521 +w = Waypoint("Catacombs", 41.3939, -22222) # Print it # # Without changing the following line, how can you make it print into something @@ -16,6 +34,6 @@ print(w) # Make a new geocache "Newberry Views", diff 1.5, size 2, 44.052137, -121.41556 - +g = Geocache("Newberry Views", 1.5, 2, 33.333, -2222 ) # Print it--also make this print more nicely print(g) From 6be407dd2c5e49a30fa464264e00e4405c8987f5 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Sun, 5 Aug 2018 20:50:46 +0900 Subject: [PATCH 49/74] args fixed --- src/day-1-toy/args.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/day-1-toy/args.py b/src/day-1-toy/args.py index 246cfde619..2d84cb247f 100644 --- a/src/day-1-toy/args.py +++ b/src/day-1-toy/args.py @@ -34,15 +34,18 @@ def f2(*args): # arguments. Google "python default arguments" for a hint. #def f3(... -def f3(*args): - total = 0 - if len(args) > 1: - for i in args: - total += i - else: - for i in args: - total += i + 1 - return total +# def f3(*args): +# total = 0 +# if len(args) > 1: +# for i in args: +# total += i +# else: +# for i in args: +# total += i + 1 +# return total + +def f3(a, b=1): + return a + b print(f3(1, 2)) # Should print 3 print(f3(8)) # Should print 9 From af4ad76ee133f4a544ad7cbc4ad40cdad4304967 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Sun, 5 Aug 2018 20:52:38 +0900 Subject: [PATCH 50/74] another way for bignum --- src/day-1-toy/bignum.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/bignum.py b/src/day-1-toy/bignum.py index f017599473..e0a7f0fe7e 100644 --- a/src/day-1-toy/bignum.py +++ b/src/day-1-toy/bignum.py @@ -1,3 +1,5 @@ # Print out 2 to the 65536 power -print( 2 ** 65536) \ No newline at end of file +print( 2 ** 65536) + +# print (pow(2, 65536)) \ No newline at end of file From eea8e94a3952d88f4ceaf3f06c8ccd6629f6195a Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Sun, 5 Aug 2018 21:49:22 +0900 Subject: [PATCH 51/74] new slice --- src/day-1-toy/slice.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/day-1-toy/slice.py b/src/day-1-toy/slice.py index d365e775b7..632a228091 100644 --- a/src/day-1-toy/slice.py +++ b/src/day-1-toy/slice.py @@ -14,10 +14,10 @@ # Output every element except the first one: [4, 1, 7, 9, 6] -print(a[1:5]) +print(a[1:]) # Output every element except the last one: [2, 4, 1, 7, 9] -print(a[0:4]) +print(a[:-1]) # For string s... From ec74db009e09f37f1a5a642ab55444f806228293 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Sun, 5 Aug 2018 21:50:02 +0900 Subject: [PATCH 52/74] new print --- src/day-1-toy/printf.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/day-1-toy/printf.py b/src/day-1-toy/printf.py index 3151664a4d..d694090f6d 100644 --- a/src/day-1-toy/printf.py +++ b/src/day-1-toy/printf.py @@ -8,4 +8,6 @@ print('x is %d, y is %.2f, z is %s' % (x, y, z)) # Use the 'format' string method to print the same thing -print( 'x is {0}, y is {1}, z is "{2}"'.format(x, y, z)) \ No newline at end of file +# print( 'x is {0}, y is {1}, z is "{2}"'.format(x, y, z)) + +print("x is {}, y is {}, z is {}".format(x, round(y, 2), z)) \ No newline at end of file From 98e76e936508467eb406b345f3fd831ace0e9b60 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Sun, 5 Aug 2018 21:51:02 +0900 Subject: [PATCH 53/74] dicts --- src/day-1-toy/dicts.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/day-1-toy/dicts.py b/src/day-1-toy/dicts.py index 7c0ac9f483..a79af3fa9e 100644 --- a/src/day-1-toy/dicts.py +++ b/src/day-1-toy/dicts.py @@ -31,8 +31,16 @@ # Write a loop that prints out all the field values for all the waypoints # print([key for key in waypoints]) -for i in waypoints: - print(i["lat"]) - print(i["lon"]) - print(i["name"]) +# for i in waypoints: +# print(i["lat"]) +# print(i["lon"]) +# print(i["name"]) + +for w in waypoints: + print( str(w["lat"]) + " " + str(w["lon"]) + " " + str(w["name"])) + # Add a new waypoint to the list +waypoints.append({"lat": 44, "lon": -22, "name": "newwe"}) + +for w in waypoints: + print( str(w["lat"]) + " " + str(w["lon"]) + " " + str(w["name"])) From 2079a6930958554e2667a848aeea6df005eb0624 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Sun, 5 Aug 2018 21:51:56 +0900 Subject: [PATCH 54/74] comp --- src/day-1-toy/comp.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/day-1-toy/comp.py b/src/day-1-toy/comp.py index b4bedadfae..d9839bcaba 100644 --- a/src/day-1-toy/comp.py +++ b/src/day-1-toy/comp.py @@ -30,3 +30,4 @@ print(y) +# or y = [int(num) for num in x if int(num) %2 == 0] From 92c1c8257ea5cd249af388239ca20e4bc9d08fb8 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Sun, 5 Aug 2018 21:52:44 +0900 Subject: [PATCH 55/74] cal --- src/day-1-toy/cal.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/day-1-toy/cal.py b/src/day-1-toy/cal.py index e138a9b32d..12db3e9312 100644 --- a/src/day-1-toy/cal.py +++ b/src/day-1-toy/cal.py @@ -19,6 +19,7 @@ month = sys.argv[1] year = sys.argv[2] + print(str(month) + ", " + str(year)) if month == None: month = 8 @@ -30,4 +31,21 @@ print( myCal.formatmonth( int(year), int(month))) +# current = datetime.datetime.now() +# cal = calendar.TextCalendar() + +# year = input("Enter 4 Digit Year: ") +# month = input("Enter Month 1 - 12: ") + +# if len(sys.argv) == 3: +# year = int(sys.argv[1]) +# month = int(sys.argv[2]) +# print(cal.formatmonth(int(year), int(month))) +# else: +# year = current.year +# month = current.month +# print(cal.formatmonth(int(year), int(month))) + + + From 0369e7e2b48a0b5917823700211f1d707949bf6b Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Sun, 5 Aug 2018 21:59:18 +0900 Subject: [PATCH 56/74] modules.py --- src/day-1-toy/modules.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/day-1-toy/modules.py b/src/day-1-toy/modules.py index 593abe9fc1..a4290b7879 100644 --- a/src/day-1-toy/modules.py +++ b/src/day-1-toy/modules.py @@ -7,13 +7,16 @@ # Print out the command line arguments in sys.argv, one per line: -print(sys.argv[0]) +# print(sys.argv[0]) +for arg in sys.argv: + print(arg) # Print out the plaform from sys: print(sys.platform) # Print out the Python version from sys: -print(sys.version_info) +# print(sys.version_info) +print(sys.version) From 706b7d714a1110422430ed4c9504f8751d5a1b98 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Sun, 5 Aug 2018 22:21:03 +0900 Subject: [PATCH 57/74] game --- src/days-2-4-adv/adv.py | 31 +++++++++++++++++++++++++++++++ src/days-2-4-adv/player.py | 3 +++ src/days-2-4-adv/room.py | 6 +++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index c9e26b0f85..35cdb9a040 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -1,4 +1,5 @@ from room import Room +from player import Player # Declare all the rooms @@ -39,13 +40,43 @@ # Make a new player object that is currently in the 'outside' room. +newplayer = Player(room['outside']) +dir = '' # Write a loop that: # +while not dir == "q": + print(newplayer.room.name) # * Prints the current room name + print(newplayer.room.description) # * Prints the current description (the textwrap module might be useful here). # * Waits for user input and decides what to do. + dir = input("\n Please choose a direction...n, s, e, w, OR q to quit the game.\n ") # # If the user enters a cardinal direction, attempt to move to the room there. + if dir == "n": + if hasattr(newplayer.room, "n_to"): + newplayer.room = newplayer.room.n_to + else: + print("Sorry, you've hit a wall") + elif dir == "s": + if hasattr(newplayer.room, "s_to"): + newplayer.room = newplayer.room.s_to + else: + print("Sorry, you've hit a wall") + elif dir == "w": + if hasattr(newplayer.room, "w_to"): + newplayer.room = newplayer.room.w_to + else: + print("Sorry, you've hit a wall") + elif dir == "e": + if hasattr(newplayer.room, "e_to"): + newplayer.room = newplayer.room.e_to + else: + print("Sorry, you've hit a wall") # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. + elif dir == "q": + print("Thank you for playing!") + else: + print("\nInvalid selection.") diff --git a/src/days-2-4-adv/player.py b/src/days-2-4-adv/player.py index d79a175029..336fe15fa3 100644 --- a/src/days-2-4-adv/player.py +++ b/src/days-2-4-adv/player.py @@ -1,2 +1,5 @@ # Write a class to hold player information, e.g. what room they are in # currently. +class Player: + def __init__( self, room): + self.room = room \ No newline at end of file diff --git a/src/days-2-4-adv/room.py b/src/days-2-4-adv/room.py index 24c07ad4c8..5fc5ef6710 100644 --- a/src/days-2-4-adv/room.py +++ b/src/days-2-4-adv/room.py @@ -1,2 +1,6 @@ # Implement a class to hold room information. This should have name and -# description attributes. \ No newline at end of file +# description attributes. +class Room: + def __init__(self, name, description): + self.name = name + self.description = description \ No newline at end of file From f375a44a582d209afe4fa81b74f0e78ab456d20c Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Mon, 6 Aug 2018 19:20:29 +0900 Subject: [PATCH 58/74] init --- src/mini-challenge/hangman.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/mini-challenge/hangman.py b/src/mini-challenge/hangman.py index 9b73b9249d..e9d910439f 100644 --- a/src/mini-challenge/hangman.py +++ b/src/mini-challenge/hangman.py @@ -5,7 +5,7 @@ # STRETCH GOAL: If you fix all the errors, can you find a way to improve # it? Add a cheat code, more error handling, chose randomly # from a set of win/loss messages...basically, make it better! - +import random # Initial setup bodies = [ " ------\n | |\n | O\n |\n |\n |\n |\n |\n---", " ------\n | |\n | O\n | |\n | |\n |\n |\n |\n---", @@ -15,51 +15,52 @@ " ------\n | |\n | O\n | \|/\n | |\n | / \ \n |\n |\n---" ] strikes = 0 words = [None] -file = open("word.txt", "r") +file = open("words.txt", "r") for line in file: words.append(line) file.close() -targetWord = words[random.randint(0, 100)] -lettersLeft = len(targetWord)-1 +target_word = words[random.randint(0, len(words))] +letters_left = len(targetWord)-1 length = len(targetWord)-1 -curWord = "_" * length -alphabet = [chr(65+x) for x in range(1, 26) ] +cur_word = "_" * length +alphabet = [chr(65+x) for x in range(0, 26) ] # Draw body based on # of incorrect guesses -def drawBody() +def drawBody(): print(bodies[strikes]) # Replace blanks with correctly guessed letters def fillLetters( letter ): for i in range(len(targetWord)-1): if( targetWord[i : i+1]) == letter: - curWord = curWord[0: i] + letter + curWord[i: ] - global lettersLeft - lettersLeft -= 1 + global cur_word + cur_word = cur_word[0: i] + letter + cur_word[i: ] + global letters_left + letters_left -= 1 # Add spaces when displaying letters / blanks for readability def printWord( word ): - prntWord = "" + prnt_word = "" for letter in word: - prntWord += letter + " " - print(prntWord) + prnt_word += letter + " " + print(prnt_word) # Begin game print( "Welcome to Hangmn!" ) -printWord(curWord) +printWord(cur_word) drawBody() print("Letters left:") printWord(alphabet) # Gameplay loop -while strikes < 5 and lettersLeft > 0: +while strikes < 5 and letters_left > 0: letter = input("\nPlease guess a letter...") if letter in targetWord: print("Great!") fillLetters(letter) else: strikes += 1 - print( strikes + " / 5 strikes" ) + print( str(strikes) + " / 5 strikes" ) printWord(curWord) drawBody() alphabet.remove(letter.upper()) From c834c13fbcb75252ce77e63ef7a20933ff2b4122 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Mon, 6 Aug 2018 20:42:24 +0900 Subject: [PATCH 59/74] with solution lecture --- src/mini-challenge/hangman.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/mini-challenge/hangman.py b/src/mini-challenge/hangman.py index e9d910439f..dbaf063089 100644 --- a/src/mini-challenge/hangman.py +++ b/src/mini-challenge/hangman.py @@ -12,7 +12,7 @@ " ------\n | |\n | O\n | |\n | |\n | / \n |\n |\n---", " ------\n | |\n | O\n | |\n | |\n | / \ \n |\n |\n---", " ------\n | |\n | O\n | \|\n | |\n | / \ \n |\n |\n---", -" ------\n | |\n | O\n | \|/\n | |\n | / \ \n |\n |\n---" ] +" ------\n | |\n | O\n | \|/\n | |\n | / \ \n |\n |\n---" ] strikes = 0 words = [None] file = open("words.txt", "r") @@ -20,8 +20,8 @@ words.append(line) file.close() target_word = words[random.randint(0, len(words))] -letters_left = len(targetWord)-1 -length = len(targetWord)-1 +letters_left = len(target_word)-1 +length = len(target_word)-1 cur_word = "_" * length alphabet = [chr(65+x) for x in range(0, 26) ] @@ -31,10 +31,10 @@ def drawBody(): # Replace blanks with correctly guessed letters def fillLetters( letter ): - for i in range(len(targetWord)-1): - if( targetWord[i : i+1]) == letter: + for i in range(len(target_word)-1): + if( target_word[i : i+1]) == letter: global cur_word - cur_word = cur_word[0: i] + letter + cur_word[i: ] + cur_word = cur_word[0: i] + letter + cur_word[i+1: ] global letters_left letters_left -= 1 @@ -46,29 +46,36 @@ def printWord( word ): print(prnt_word) # Begin game -print( "Welcome to Hangmn!" ) +print( "Welcome to Hangman!" ) printWord(cur_word) drawBody() print("Letters left:") printWord(alphabet) - +printWord(target_word) # Gameplay loop while strikes < 5 and letters_left > 0: letter = input("\nPlease guess a letter...") - if letter in targetWord: + if not letter.isalpha(): + print("Please only enter letters") + elif len(letter) > 1: + print("Please guess only a single letter.") + elif not letter.upper() in alphabet: + print("Already guessed this letter!") + elif letter in target_word: print("Great!") fillLetters(letter) + alphabet.remove(letter.upper()) else: strikes += 1 print( str(strikes) + " / 5 strikes" ) - printWord(curWord) + alphabet.remove(letter.upper()) + printWord(cur_word) drawBody() - alphabet.remove(letter.upper()) print("Letters left:") printWord(alphabet) # Game over, print outcome -if lettersLeft < 0: +if letters_left == 0: print("YOU WIN!!") else: - print("YOU LOSE...word was " + targetWord) \ No newline at end of file + print("YOU LOSE...word was " + target_word) \ No newline at end of file From af8f8f617197b304076102ff0ba9ef66a1a74563 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 7 Aug 2018 18:57:20 +0900 Subject: [PATCH 60/74] adding self-created rooms --- src/days-2-4-adv/adv.py | 72 +++++++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 14 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index 35cdb9a040..9a55555b58 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -4,36 +4,80 @@ # Declare all the rooms room = { - 'outside': Room("Outside Cave Entrance", + 'village': Room("Village Hidden in the Spring", "North of you, the cave mount beckons"), - 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty + 'home': Room("Home Sweet Home", """Dim light filters in from the south. Dusty passages run north and east."""), - 'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling + 'attic': Room("Attic Room", """A steep cliff appears before you, falling into the darkness. Ahead to the north, a light flickers in the distance, but there is no way across the chasm."""), - 'narrow': Room("Narrow Passage", """The narrow passage bends here from west + 'forest': Room("Grand Forest", """The narrow passage bends here from west to north. The smell of gold permeates the air."""), - 'treasure': Room("Treasure Chamber", """You've found the long-lost treasure + 'overlook': Room("Mountain Overlook", """A steep mountain appears before you. The forest expands in all directions. Perhaps a further look is needed..."""), + + 'peak': Room("Mountain Peak", """The fresh air soothes your senses, the forest expanse looks peaceful. The village looks minute in the distance."""), + + 'cave': Room("Narrow Passage", """The narrow passage bends here from west +to north. The smell of gold permeates the air."""), + + 'underground1': Room("Underground Passage", """The narrow passage bends here from west +to north. The smell of gold permeates the air."""), + + 'tunnels': Room("Dark Tunnels", """The narrow passage bends here from west +to north. The smell of gold permeates the air."""), + + 'secret': Room("Secret Passage", """The passage holds an air of mystery. The walls glimmer with something hidden beneath."""), + + 'epic': Room("Epic Treasure", """The room contains the most special of treasure chests. Open it to reveal your prize."""), + + 'underground2': Room("Damp Underground Passage", """Moist droplets of water fall from the passage's crevices."""), + + 'dragon': Room("Dragon Lair", """You've found the long-lost treasure chamber! Sadly, it has already been completely emptied by earlier adventurers. The only exit is to the south."""), + + 'swamp': Room("Sinister Swamp", """The narrow passage bends here from west +to north. The smell of gold permeates the air."""), + + 'mangroves': Room("Mangrove Forest", """The narrow passage bends here from west +to north. The smell of gold permeates the air."""), } # Link rooms together -room['outside'].n_to = room['foyer'] -room['foyer'].s_to = room['outside'] -room['foyer'].n_to = room['overlook'] -room['foyer'].e_to = room['narrow'] -room['overlook'].s_to = room['foyer'] -room['narrow'].w_to = room['foyer'] -room['narrow'].n_to = room['treasure'] -room['treasure'].s_to = room['narrow'] - +room['village'].n_to = room['forest'] +room['forest'].s_to = room['village'] +room['village'].s_to = room['home'] +room['home'].n_to = room['village'] +room['home'].u_to = room['attic'] +room['attic'].d_to = room['home'] +room['forest'].w_to = room['overlook'] +room['overlook'].e_to = room['forest'] +room['overlook'].u_to = room['peak'] +room['peak'].d_to = room['overlook'] +room['forest'].e_to = room['mangroves'] +room['mangroves'].w_to = room['forest'] +room['forest'].n_to = room['cave'] +room['cave'].s_to = room['forest'] +room['cave'].d_to = room['underground1'] +room['underground1'].u_to = room['cave'] +room['underground1'].e_to = room['tunnels'] +room['tunnels'].w_to = room['underground1'] +room['tunnels'].e_to = room['dragon'] +room['dragon'].w_to = room['tunnels'] +room['tunnels'].n_to = room['secret'] +room['secret'].s_to = room['tunnels'] +room['tunnels'].s_to = room['underground2'] +room['underground2'].n_to = room['tunnels'] +room['underground2'].u_to = room['swamp'] +room['swamp'].d_to = room['underground2'] +room['swamp'].n_to = room['mangroves'] +room['mangroves'].s_to = room['swamp'] # # Main # From 539fc6ca00d3b9d9baeb92d1e77d66b87a01f1ea Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 7 Aug 2018 19:05:55 +0900 Subject: [PATCH 61/74] directions working, need to update descriptions --- src/days-2-4-adv/adv.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index 9a55555b58..6107e0338b 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -5,7 +5,7 @@ room = { 'village': Room("Village Hidden in the Spring", - "North of you, the cave mount beckons"), + "The noise of bustling streets warm your heart"), 'home': Room("Home Sweet Home", """Dim light filters in from the south. Dusty passages run north and east."""), @@ -21,8 +21,7 @@ 'peak': Room("Mountain Peak", """The fresh air soothes your senses, the forest expanse looks peaceful. The village looks minute in the distance."""), - 'cave': Room("Narrow Passage", """The narrow passage bends here from west -to north. The smell of gold permeates the air."""), + 'cave': Room("North Cave", """Creaking sounds resonate throughout the cave."""), 'underground1': Room("Underground Passage", """The narrow passage bends here from west to north. The smell of gold permeates the air."""), @@ -84,7 +83,7 @@ # Make a new player object that is currently in the 'outside' room. -newplayer = Player(room['outside']) +newplayer = Player(room['village']) dir = '' # Write a loop that: # @@ -117,6 +116,16 @@ newplayer.room = newplayer.room.e_to else: print("Sorry, you've hit a wall") + elif dir == "u": + if hasattr(newplayer.room, "u_to"): + newplayer.room = newplayer.room.u_to + else: + print("Sorry, you've hit a wall") + elif dir == "d": + if hasattr(newplayer.room, "d_to" ): + newplayer.room = newplayer.room.d_to + else: + print("Sorry, you've hit a wall") # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. From 95183e66286aed5ba975225068b78e535fdeb681 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 7 Aug 2018 19:31:27 +0900 Subject: [PATCH 62/74] groundwork for displaying item message --- src/days-2-4-adv/adv.py | 55 +++++++++++++++++++------------------- src/days-2-4-adv/item.py | 4 +++ src/days-2-4-adv/player.py | 5 ++-- src/days-2-4-adv/room.py | 12 +++++++-- 4 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 src/days-2-4-adv/item.py diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index 6107e0338b..e591eddc61 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -1,49 +1,49 @@ from room import Room from player import Player - +from item import Item # Declare all the rooms room = { 'village': Room("Village Hidden in the Spring", - "The noise of bustling streets warm your heart"), + "The noise of bustling streets warms your heart", []), 'home': Room("Home Sweet Home", """Dim light filters in from the south. Dusty -passages run north and east."""), +passages run north and east.""", []), 'attic': Room("Attic Room", """A steep cliff appears before you, falling into the darkness. Ahead to the north, a light flickers in -the distance, but there is no way across the chasm."""), +the distance, but there is no way across the chasm.""", []), 'forest': Room("Grand Forest", """The narrow passage bends here from west -to north. The smell of gold permeates the air."""), +to north. The smell of gold permeates the air.""", []), - 'overlook': Room("Mountain Overlook", """A steep mountain appears before you. The forest expands in all directions. Perhaps a further look is needed..."""), + 'overlook': Room("Mountain Overlook", """A steep mountain appears before you. The forest expands in all directions. Perhaps a further look is needed...""", []), - 'peak': Room("Mountain Peak", """The fresh air soothes your senses, the forest expanse looks peaceful. The village looks minute in the distance."""), + 'peak': Room("Mountain Peak", """The fresh air soothes your senses, the forest expanse looks peaceful. The village looks minute in the distance.""", []), - 'cave': Room("North Cave", """Creaking sounds resonate throughout the cave."""), + 'cave': Room("North Cave", """Creaking sounds resonate throughout the cave.""", []), 'underground1': Room("Underground Passage", """The narrow passage bends here from west -to north. The smell of gold permeates the air."""), +to north. The smell of gold permeates the air.""", []), 'tunnels': Room("Dark Tunnels", """The narrow passage bends here from west -to north. The smell of gold permeates the air."""), +to north. The smell of gold permeates the air.""", []), - 'secret': Room("Secret Passage", """The passage holds an air of mystery. The walls glimmer with something hidden beneath."""), + 'secret': Room("Secret Passage", """The passage holds an air of mystery. The walls glimmer with something hidden beneath.""", []), - 'epic': Room("Epic Treasure", """The room contains the most special of treasure chests. Open it to reveal your prize."""), + 'epic': Room("Epic Treasure", """The room contains the most special of treasure chests. Open it to reveal your prize.""", []), - 'underground2': Room("Damp Underground Passage", """Moist droplets of water fall from the passage's crevices."""), + 'underground2': Room("Damp Underground Passage", """Moist droplets of water fall from the passage's crevices.""", []), 'dragon': Room("Dragon Lair", """You've found the long-lost treasure chamber! Sadly, it has already been completely emptied by -earlier adventurers. The only exit is to the south."""), +earlier adventurers. The only exit is to the south.""", []), 'swamp': Room("Sinister Swamp", """The narrow passage bends here from west -to north. The smell of gold permeates the air."""), +to north. The smell of gold permeates the air.""", []), 'mangroves': Room("Mangrove Forest", """The narrow passage bends here from west -to north. The smell of gold permeates the air."""), +to north. The smell of gold permeates the air.""", []), } @@ -83,45 +83,46 @@ # Make a new player object that is currently in the 'outside' room. -newplayer = Player(room['village']) -dir = '' +newplayer = Player(room['village'], []) +cmd = "" # Write a loop that: # -while not dir == "q": +while not cmd == "q": print(newplayer.room.name) # * Prints the current room name print(newplayer.room.description) # * Prints the current description (the textwrap module might be useful here). # * Waits for user input and decides what to do. - dir = input("\n Please choose a direction...n, s, e, w, OR q to quit the game.\n ") + newplayer.room.view_items() + cmd = input("\n Please choose a direction...n, s, e, w, OR q to quit the game.\n ") # # If the user enters a cardinal direction, attempt to move to the room there. - if dir == "n": + if cmd == "n": if hasattr(newplayer.room, "n_to"): newplayer.room = newplayer.room.n_to else: print("Sorry, you've hit a wall") - elif dir == "s": + elif cmd == "s": if hasattr(newplayer.room, "s_to"): newplayer.room = newplayer.room.s_to else: print("Sorry, you've hit a wall") - elif dir == "w": + elif cmd == "w": if hasattr(newplayer.room, "w_to"): newplayer.room = newplayer.room.w_to else: print("Sorry, you've hit a wall") - elif dir == "e": + elif cmd == "e": if hasattr(newplayer.room, "e_to"): newplayer.room = newplayer.room.e_to else: print("Sorry, you've hit a wall") - elif dir == "u": + elif cmd == "u": if hasattr(newplayer.room, "u_to"): newplayer.room = newplayer.room.u_to else: print("Sorry, you've hit a wall") - elif dir == "d": + elif cmd == "d": if hasattr(newplayer.room, "d_to" ): newplayer.room = newplayer.room.d_to else: @@ -129,7 +130,7 @@ # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. - elif dir == "q": + elif cmd == "q": print("Thank you for playing!") else: print("\nInvalid selection.") diff --git a/src/days-2-4-adv/item.py b/src/days-2-4-adv/item.py new file mode 100644 index 0000000000..70e70cef64 --- /dev/null +++ b/src/days-2-4-adv/item.py @@ -0,0 +1,4 @@ +class Item: + def __init__(self, name, description): + self.name = name + self.description = description \ No newline at end of file diff --git a/src/days-2-4-adv/player.py b/src/days-2-4-adv/player.py index 336fe15fa3..9c598d467f 100644 --- a/src/days-2-4-adv/player.py +++ b/src/days-2-4-adv/player.py @@ -1,5 +1,6 @@ # Write a class to hold player information, e.g. what room they are in # currently. class Player: - def __init__( self, room): - self.room = room \ No newline at end of file + def __init__( self, room, items): + self.room = room + self.items = items \ No newline at end of file diff --git a/src/days-2-4-adv/room.py b/src/days-2-4-adv/room.py index 5fc5ef6710..beb12f0193 100644 --- a/src/days-2-4-adv/room.py +++ b/src/days-2-4-adv/room.py @@ -1,6 +1,14 @@ # Implement a class to hold room information. This should have name and # description attributes. class Room: - def __init__(self, name, description): + def __init__(self, name, description, items): self.name = name - self.description = description \ No newline at end of file + self.description = description + self.items = items + def view_items(self): + print("Items found in this room: ") + if len(self.items) == 0: + print( " none") + else: + for i in self.items: + print("\t" + i) \ No newline at end of file From b4e95bff581671bf4214b1744e3f1c0fe634650b Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Tue, 7 Aug 2018 20:07:36 +0900 Subject: [PATCH 63/74] something wrong with action --- src/days-2-4-adv/adv.py | 109 +++++++++++++++++++++++++-------------- src/days-2-4-adv/item.py | 5 +- src/days-2-4-adv/room.py | 3 +- 3 files changed, 77 insertions(+), 40 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index e591eddc61..e4b8e78978 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -8,7 +8,7 @@ "The noise of bustling streets warms your heart", []), 'home': Room("Home Sweet Home", """Dim light filters in from the south. Dusty -passages run north and east.""", []), +passages run north and east.""", [Item("Meal", "This item restores your health.")]), 'attic': Room("Attic Room", """A steep cliff appears before you, falling into the darkness. Ahead to the north, a light flickers in @@ -94,43 +94,76 @@ # * Prints the current description (the textwrap module might be useful here). # * Waits for user input and decides what to do. newplayer.room.view_items() - cmd = input("\n Please choose a direction...n, s, e, w, OR q to quit the game.\n ") + cmd = input("\n Please choose a direction...n, s, e, w, OR q to quit the game.\n ").lower() # + parsed_cmd = cmd.split() + if len(parsed_cmd) > 1: + action = parsed_cmd[0] + item = "" + for i in range(1, len(parsed_cmd)): + item += parsed_cmd[i] + " " + item = item.strip()#confused + + if action == "g" or action == "grab": + for i in newplayer.room.items: + print(i) + for i in newplayer.room.items: + if parsed_cmd[i] == i.name: + print("\n...grabbing" +".") + newplayer.room.items.remove(i) + newplayer.items.append(i) + else: + print("\nitem not available to grab") + for i in newplayer.room.items: + print("after") + print(i) + elif action == "d" or action == "drop": + if item in newplayer.items: + print("...dropping" + item + ".") + newplayer.items.remove(item) + newplayer.room.items.append(item) + else: + print("item not available to drop!") + elif cmd == "i" or cmd =="inventory": + print("Inventory: ") + for i in newplayer.items: + print("\t" + i) + else: #parsed_cmd length =1 # If the user enters a cardinal direction, attempt to move to the room there. - if cmd == "n": - if hasattr(newplayer.room, "n_to"): - newplayer.room = newplayer.room.n_to - else: - print("Sorry, you've hit a wall") - elif cmd == "s": - if hasattr(newplayer.room, "s_to"): - newplayer.room = newplayer.room.s_to - else: - print("Sorry, you've hit a wall") - elif cmd == "w": - if hasattr(newplayer.room, "w_to"): - newplayer.room = newplayer.room.w_to - else: - print("Sorry, you've hit a wall") - elif cmd == "e": - if hasattr(newplayer.room, "e_to"): - newplayer.room = newplayer.room.e_to - else: - print("Sorry, you've hit a wall") - elif cmd == "u": - if hasattr(newplayer.room, "u_to"): - newplayer.room = newplayer.room.u_to + if cmd == "n": + if hasattr(newplayer.room, "n_to"): + newplayer.room = newplayer.room.n_to + else: + print("Sorry, you've hit a wall") + elif cmd == "s": + if hasattr(newplayer.room, "s_to"): + newplayer.room = newplayer.room.s_to + else: + print("Sorry, you've hit a wall") + elif cmd == "w": + if hasattr(newplayer.room, "w_to"): + newplayer.room = newplayer.room.w_to + else: + print("Sorry, you've hit a wall") + elif cmd == "e": + if hasattr(newplayer.room, "e_to"): + newplayer.room = newplayer.room.e_to + else: + print("Sorry, you've hit a wall") + elif cmd == "u": + if hasattr(newplayer.room, "u_to"): + newplayer.room = newplayer.room.u_to + else: + print("Sorry, you've hit a wall") + elif cmd == "d": + if hasattr(newplayer.room, "d_to" ): + newplayer.room = newplayer.room.d_to + else: + print("Sorry, you've hit a wall") + # Print an error message if the movement isn't allowed. + # + # If the user enters "q", quit the game. + elif cmd == "q": + print("Thank you for playing!") else: - print("Sorry, you've hit a wall") - elif cmd == "d": - if hasattr(newplayer.room, "d_to" ): - newplayer.room = newplayer.room.d_to - else: - print("Sorry, you've hit a wall") -# Print an error message if the movement isn't allowed. -# -# If the user enters "q", quit the game. - elif cmd == "q": - print("Thank you for playing!") - else: - print("\nInvalid selection.") + print("\nInvalid selection.") diff --git a/src/days-2-4-adv/item.py b/src/days-2-4-adv/item.py index 70e70cef64..fa7ba9b0eb 100644 --- a/src/days-2-4-adv/item.py +++ b/src/days-2-4-adv/item.py @@ -1,4 +1,7 @@ class Item: def __init__(self, name, description): self.name = name - self.description = description \ No newline at end of file + self.description = description + + def __str__(self): + return self.name + ": " + self.description \ No newline at end of file diff --git a/src/days-2-4-adv/room.py b/src/days-2-4-adv/room.py index beb12f0193..98c35488ee 100644 --- a/src/days-2-4-adv/room.py +++ b/src/days-2-4-adv/room.py @@ -11,4 +11,5 @@ def view_items(self): print( " none") else: for i in self.items: - print("\t" + i) \ No newline at end of file + # print("\t" + i.name) + print( " " + str(i)) \ No newline at end of file From 0c43e9cc67ac5dd3e7c0aae985f78ecf08761b28 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Wed, 8 Aug 2018 12:08:44 +0900 Subject: [PATCH 64/74] grab and drop working --- src/days-2-4-adv/adv.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index e4b8e78978..f7e3e11cc5 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -102,24 +102,30 @@ item = "" for i in range(1, len(parsed_cmd)): item += parsed_cmd[i] + " " - item = item.strip()#confused - + item = item.strip()# array of commands + + if action == "g" or action == "grab": + # for i in newplayer.room.items: + # print("-->", i.name.lower() == parsed_cmd[1]) + # print(parsed_cmd[1]) for i in newplayer.room.items: - print(i) - for i in newplayer.room.items: - if parsed_cmd[i] == i.name: + print(i) + if i.name.lower() == parsed_cmd[1]: print("\n...grabbing" +".") + print(i) newplayer.room.items.remove(i) - newplayer.items.append(i) + newplayer.items.append(i.name.lower()) + # print("player items", newplayer.items[0].name) + print("player items", newplayer.items[0]) else: print("\nitem not available to grab") for i in newplayer.room.items: - print("after") - print(i) + print("after", i) elif action == "d" or action == "drop": + print("checked", item, newplayer.items[0]) if item in newplayer.items: - print("...dropping" + item + ".") + print("\t...dropping .") newplayer.items.remove(item) newplayer.room.items.append(item) else: From b97f923e0a05a6a9a9763398237c2076141f8f51 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Wed, 8 Aug 2018 13:08:28 +0900 Subject: [PATCH 65/74] code for i not working --- src/days-2-4-adv/adv.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index f7e3e11cc5..9ed22efa19 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -130,10 +130,10 @@ newplayer.room.items.append(item) else: print("item not available to drop!") - elif cmd == "i" or cmd =="inventory": + elif action == "i" or action =="inventory": print("Inventory: ") - for i in newplayer.items: - print("\t" + i) + for item in newplayer.items: + print("\t" + item) else: #parsed_cmd length =1 # If the user enters a cardinal direction, attempt to move to the room there. if cmd == "n": From 991ce24da9350536d28b32385792ef115f844006 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Wed, 8 Aug 2018 13:09:28 +0900 Subject: [PATCH 66/74] not working --- src/day-1-toy/cal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/day-1-toy/cal.py b/src/day-1-toy/cal.py index 12db3e9312..2bcac44c29 100644 --- a/src/day-1-toy/cal.py +++ b/src/day-1-toy/cal.py @@ -19,8 +19,8 @@ month = sys.argv[1] year = sys.argv[2] - print(str(month) + ", " + str(year)) + if month == None: month = 8 From 20b2d15b29ba8600902057dd2a383b12ee5cbf93 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 9 Aug 2018 02:41:14 +0900 Subject: [PATCH 67/74] cannot get i to work on action line but works on cmd lines/ bug when trying to pick and drop items repeatedly --- src/days-2-4-adv/adv.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index 9ed22efa19..8cd9b4fb8f 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -127,13 +127,15 @@ if item in newplayer.items: print("\t...dropping .") newplayer.items.remove(item) - newplayer.room.items.append(item) + newplayer.room.items.append(item.capitalize()) #bug - cannot pick and drop multiple times else: print("item not available to drop!") - elif action == "i" or action =="inventory": - print("Inventory: ") - for item in newplayer.items: - print("\t" + item) + # i does not work if it's here, but works down? + # elif action == "i" or action =="inventory": + # print("Inventory: ") + # # for item in newplayer.items: + # print("\t" + str(newplayer.items)) + else: #parsed_cmd length =1 # If the user enters a cardinal direction, attempt to move to the room there. if cmd == "n": @@ -141,6 +143,10 @@ newplayer.room = newplayer.room.n_to else: print("Sorry, you've hit a wall") + elif cmd == "i" or cmd =="inventory": + print("Inventory: ") + for item in newplayer.items: + print("\t" + item.capitalize()) elif cmd == "s": if hasattr(newplayer.room, "s_to"): newplayer.room = newplayer.room.s_to From 835405d23bf7f389c0006a9ccc82891f12272809 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 9 Aug 2018 22:46:25 +0900 Subject: [PATCH 68/74] adding crayons --- src/days-2-4-adv/adv.py | 22 ++++++++++------------ src/days-2-4-adv/player.py | 7 +++++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index 8cd9b4fb8f..08032ee44e 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -1,6 +1,7 @@ from room import Room from player import Player from item import Item +import crayons # Declare all the rooms room = { @@ -83,8 +84,10 @@ # Make a new player object that is currently in the 'outside' room. -newplayer = Player(room['village'], []) +name = input('Enter your name: ') +newplayer = Player(name, room['village'], []) cmd = "" +print(crayons.green(f'\n \t Welcome, {newplayer.name}!\n')) # Write a loop that: # while not cmd == "q": @@ -130,11 +133,6 @@ newplayer.room.items.append(item.capitalize()) #bug - cannot pick and drop multiple times else: print("item not available to drop!") - # i does not work if it's here, but works down? - # elif action == "i" or action =="inventory": - # print("Inventory: ") - # # for item in newplayer.items: - # print("\t" + str(newplayer.items)) else: #parsed_cmd length =1 # If the user enters a cardinal direction, attempt to move to the room there. @@ -142,7 +140,7 @@ if hasattr(newplayer.room, "n_to"): newplayer.room = newplayer.room.n_to else: - print("Sorry, you've hit a wall") + print("Sorry, you can't go that way") elif cmd == "i" or cmd =="inventory": print("Inventory: ") for item in newplayer.items: @@ -151,27 +149,27 @@ if hasattr(newplayer.room, "s_to"): newplayer.room = newplayer.room.s_to else: - print("Sorry, you've hit a wall") + print("Sorry, you can't go that way") elif cmd == "w": if hasattr(newplayer.room, "w_to"): newplayer.room = newplayer.room.w_to else: - print("Sorry, you've hit a wall") + print("Sorry, you can't go that way") elif cmd == "e": if hasattr(newplayer.room, "e_to"): newplayer.room = newplayer.room.e_to else: - print("Sorry, you've hit a wall") + print("Sorry, you can't go that way") elif cmd == "u": if hasattr(newplayer.room, "u_to"): newplayer.room = newplayer.room.u_to else: - print("Sorry, you've hit a wall") + print("Sorry, you can't go that way") elif cmd == "d": if hasattr(newplayer.room, "d_to" ): newplayer.room = newplayer.room.d_to else: - print("Sorry, you've hit a wall") + print("Sorry, you can't go that way") # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. diff --git a/src/days-2-4-adv/player.py b/src/days-2-4-adv/player.py index 9c598d467f..7a15b57c81 100644 --- a/src/days-2-4-adv/player.py +++ b/src/days-2-4-adv/player.py @@ -1,6 +1,9 @@ # Write a class to hold player information, e.g. what room they are in # currently. class Player: - def __init__( self, room, items): + def __init__( self, name, room, items): + self.name = name self.room = room - self.items = items \ No newline at end of file + self.items = items + self.score = 0 + self.light = False \ No newline at end of file From 4ae2f58ba115d51bb6cd44f3bf067239718f18d3 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 9 Aug 2018 23:02:55 +0900 Subject: [PATCH 69/74] added inventory message --- src/days-2-4-adv/adv.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index 08032ee44e..d1723bdf26 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -1,7 +1,8 @@ from room import Room from player import Player from item import Item -import crayons + +# import crayons # Declare all the rooms room = { @@ -87,7 +88,7 @@ name = input('Enter your name: ') newplayer = Player(name, room['village'], []) cmd = "" -print(crayons.green(f'\n \t Welcome, {newplayer.name}!\n')) +print(f'\n \t Welcome, {newplayer.name}!\n') #crayons.green( # Write a loop that: # while not cmd == "q": @@ -142,9 +143,12 @@ else: print("Sorry, you can't go that way") elif cmd == "i" or cmd =="inventory": - print("Inventory: ") - for item in newplayer.items: - print("\t" + item.capitalize()) + if len(newplayer.items) == 0: + print("Inventory is empty") + else: + print("You are carrying: \n") + for item in newplayer.items: + print("\t" + item.capitalize()) elif cmd == "s": if hasattr(newplayer.room, "s_to"): newplayer.room = newplayer.room.s_to From faf52a2b9eb47fa27b86139f2dd560511fd5c5e0 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Thu, 9 Aug 2018 23:21:04 +0900 Subject: [PATCH 70/74] fixed bug --- src/days-2-4-adv/adv.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index d1723bdf26..94e6f4b683 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -110,30 +110,34 @@ if action == "g" or action == "grab": - # for i in newplayer.room.items: - # print("-->", i.name.lower() == parsed_cmd[1]) - # print(parsed_cmd[1]) for i in newplayer.room.items: - print(i) if i.name.lower() == parsed_cmd[1]: print("\n...grabbing" +".") print(i) newplayer.room.items.remove(i) - newplayer.items.append(i.name.lower()) + newplayer.items.append(i) # print("player items", newplayer.items[0].name) print("player items", newplayer.items[0]) + # if i.name.lower() == parsed_cmd[1]: + # print("\n...grabbing" +".") + # print(i) + # newplayer.room.items.remove(i) + # newplayer.items.append(i.name.lower()) + # # print("player items", newplayer.items[0].name) + # print("player items", newplayer.items[0]) else: print("\nitem not available to grab") for i in newplayer.room.items: - print("after", i) + print("after", i.name) elif action == "d" or action == "drop": - print("checked", item, newplayer.items[0]) - if item in newplayer.items: - print("\t...dropping .") - newplayer.items.remove(item) - newplayer.room.items.append(item.capitalize()) #bug - cannot pick and drop multiple times - else: - print("item not available to drop!") + print("checked", i, newplayer.items[0]) + for i in newplayer.items: + if i.name.lower() == parsed_cmd[1]: + print("\t...dropping .") + newplayer.items.remove(i) + newplayer.room.items.append(i) #bug - cannot pick and drop multiple times + else: + print("item not available to drop!") else: #parsed_cmd length =1 # If the user enters a cardinal direction, attempt to move to the room there. @@ -148,7 +152,7 @@ else: print("You are carrying: \n") for item in newplayer.items: - print("\t" + item.capitalize()) + print("\t" + str(item)) elif cmd == "s": if hasattr(newplayer.room, "s_to"): newplayer.room = newplayer.room.s_to From 2c4382c0f02e4d9ca5d08bfa2ca582d905f5e48a Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 10 Aug 2018 10:41:23 +0900 Subject: [PATCH 71/74] added score display --- src/days-2-4-adv/adv.py | 23 ++++++++++++----------- src/days-2-4-adv/item.py | 7 ++++++- src/days-2-4-adv/room.py | 4 ++-- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index 94e6f4b683..7d3d4fd5e3 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -1,8 +1,8 @@ from room import Room from player import Player -from item import Item +from item import Item, Treasure -# import crayons +import crayons # Declare all the rooms room = { @@ -88,7 +88,9 @@ name = input('Enter your name: ') newplayer = Player(name, room['village'], []) cmd = "" -print(f'\n \t Welcome, {newplayer.name}!\n') #crayons.green( +print(crayons.green(f'\n \t Welcome, {newplayer.name}!\n')) #crayons.green( + + # Write a loop that: # while not cmd == "q": @@ -116,15 +118,9 @@ print(i) newplayer.room.items.remove(i) newplayer.items.append(i) + newplayer.score += i.value # print("player items", newplayer.items[0].name) print("player items", newplayer.items[0]) - # if i.name.lower() == parsed_cmd[1]: - # print("\n...grabbing" +".") - # print(i) - # newplayer.room.items.remove(i) - # newplayer.items.append(i.name.lower()) - # # print("player items", newplayer.items[0].name) - # print("player items", newplayer.items[0]) else: print("\nitem not available to grab") for i in newplayer.room.items: @@ -135,7 +131,7 @@ if i.name.lower() == parsed_cmd[1]: print("\t...dropping .") newplayer.items.remove(i) - newplayer.room.items.append(i) #bug - cannot pick and drop multiple times + newplayer.room.items.append(i) else: print("item not available to drop!") @@ -146,6 +142,11 @@ newplayer.room = newplayer.room.n_to else: print("Sorry, you can't go that way") + elif cmd == "score": + # cmd = input( f'Score: {newplayer.score} \n') + print( f'Score: {newplayer.score} \n') + # elif cmd == "?" + # cmd = print_help() elif cmd == "i" or cmd =="inventory": if len(newplayer.items) == 0: print("Inventory is empty") diff --git a/src/days-2-4-adv/item.py b/src/days-2-4-adv/item.py index fa7ba9b0eb..eae2a62e3f 100644 --- a/src/days-2-4-adv/item.py +++ b/src/days-2-4-adv/item.py @@ -4,4 +4,9 @@ def __init__(self, name, description): self.description = description def __str__(self): - return self.name + ": " + self.description \ No newline at end of file + return self.name + ": " + self.description + +class Treasure(Item): + def __init__( self, name, description, value): + super().__init__(self, name, description) + self.value = value \ No newline at end of file diff --git a/src/days-2-4-adv/room.py b/src/days-2-4-adv/room.py index 98c35488ee..c718f9c814 100644 --- a/src/days-2-4-adv/room.py +++ b/src/days-2-4-adv/room.py @@ -8,8 +8,8 @@ def __init__(self, name, description, items): def view_items(self): print("Items found in this room: ") if len(self.items) == 0: - print( " none") + print( "\n \t none") else: for i in self.items: # print("\t" + i.name) - print( " " + str(i)) \ No newline at end of file + print( "\t " + str(i)) \ No newline at end of file From 53a9b389ac0eb7b52491aafe9a3e12c13a2e7fbd Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 10 Aug 2018 11:21:03 +0900 Subject: [PATCH 72/74] treasure working --- src/days-2-4-adv/adv.py | 19 +++++++++++++------ src/days-2-4-adv/item.py | 16 ++++++++++++++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index 7d3d4fd5e3..3e6d2c39ff 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -7,7 +7,7 @@ room = { 'village': Room("Village Hidden in the Spring", - "The noise of bustling streets warms your heart", []), + "The noise of bustling streets warms your heart", [Treasure("Coin", "Starting money, how lucky!", 10)]), 'home': Room("Home Sweet Home", """Dim light filters in from the south. Dusty passages run north and east.""", [Item("Meal", "This item restores your health.")]), @@ -90,6 +90,9 @@ cmd = "" print(crayons.green(f'\n \t Welcome, {newplayer.name}!\n')) #crayons.green( +# print_room_info() +# print() +# cmd = print_help() # Write a loop that: # @@ -114,11 +117,15 @@ if action == "g" or action == "grab": for i in newplayer.room.items: if i.name.lower() == parsed_cmd[1]: - print("\n...grabbing" +".") - print(i) - newplayer.room.items.remove(i) - newplayer.items.append(i) - newplayer.score += i.value + i.grab_item(newplayer) + + # if i.picked_up == False: + # newplayer.score += i.value + # print("\n...grabbing" +".") + # print(i) + # newplayer.room.items.remove(i) + # newplayer.items.append(i) + # newplayer.score += i.value # print("player items", newplayer.items[0].name) print("player items", newplayer.items[0]) else: diff --git a/src/days-2-4-adv/item.py b/src/days-2-4-adv/item.py index eae2a62e3f..f8ff572c29 100644 --- a/src/days-2-4-adv/item.py +++ b/src/days-2-4-adv/item.py @@ -6,7 +6,19 @@ def __init__(self, name, description): def __str__(self): return self.name + ": " + self.description + def grab_item(self, newplayer): + print("\n...grabbing" +".") + newplayer.room.items.remove(self) + newplayer.items.append(self) + class Treasure(Item): def __init__( self, name, description, value): - super().__init__(self, name, description) - self.value = value \ No newline at end of file + super().__init__(name, description) + self.value = value + self.picked_up = False + def grab_item(self, newplayer): + super().grab_item(newplayer) + if self.picked_up == False: + newplayer.score += self.value + self.picked_up = True + From 83fc3f8080b0529b77fbaa631688620583d0e743 Mon Sep 17 00:00:00 2001 From: Stephanie Bowles Date: Fri, 10 Aug 2018 11:24:45 +0900 Subject: [PATCH 73/74] quick crayon --- src/days-2-4-adv/item.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/days-2-4-adv/item.py b/src/days-2-4-adv/item.py index f8ff572c29..2619448561 100644 --- a/src/days-2-4-adv/item.py +++ b/src/days-2-4-adv/item.py @@ -1,3 +1,5 @@ +import crayons + class Item: def __init__(self, name, description): self.name = name @@ -7,7 +9,7 @@ def __str__(self): return self.name + ": " + self.description def grab_item(self, newplayer): - print("\n...grabbing" +".") + print(crayons.blue("\n...grabbing" +".")) newplayer.room.items.remove(self) newplayer.items.append(self) From d9f1ea18e275b1426cbc1939b3f9cde75c063d31 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Wed, 6 May 2020 12:15:56 +0800 Subject: [PATCH 74/74] latest --- src/days-2-4-adv/adv.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index 3e6d2c39ff..fa0b9a12d6 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -1,6 +1,6 @@ from room import Room from player import Player -from item import Item, Treasure +from item import Item, Treasure, LightSource import crayons # Declare all the rooms @@ -118,9 +118,6 @@ for i in newplayer.room.items: if i.name.lower() == parsed_cmd[1]: i.grab_item(newplayer) - - # if i.picked_up == False: - # newplayer.score += i.value # print("\n...grabbing" +".") # print(i) # newplayer.room.items.remove(i)