From 1a6786e527d31be8c8e6bc82df5094176082deea Mon Sep 17 00:00:00 2001 From: kammagik Date: Mon, 30 Jul 2018 20:28:11 -0700 Subject: [PATCH 01/18] initial commit --- src/day-1-toy/args.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/day-1-toy/args.py b/src/day-1-toy/args.py index 06a830e4c8..edb77fee8f 100644 --- a/src/day-1-toy/args.py +++ b/src/day-1-toy/args.py @@ -1,5 +1,5 @@ # Experiment with positional arguments, arbitrary arguments, and keyword -# arguments. +# arguments # Write a function f1 that takes two integer positional arguments and returns # the sum. This is what you'd consider to be a regular, normal function. From 9d9eaa239db6c91b77da94addffc980679b3dd2b Mon Sep 17 00:00:00 2001 From: kammagik Date: Tue, 31 Jul 2018 18:08:56 -0700 Subject: [PATCH 02/18] Day 1 --- src/day-1-toy/bignum.py | 3 ++- src/day-1-toy/cal.py | 5 +++++ src/day-1-toy/comp.py | 8 ++++---- src/day-1-toy/datatypes.py | 4 ++-- src/day-1-toy/dicts.py | 4 +++- src/day-1-toy/func.py | 9 ++++++++- src/day-1-toy/hello.py | 3 ++- src/day-1-toy/lists.py | 14 ++++++++------ src/day-1-toy/printf.py | 3 ++- 9 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/day-1-toy/bignum.py b/src/day-1-toy/bignum.py index 77e8d66ffa..8ad38fc735 100644 --- a/src/day-1-toy/bignum.py +++ b/src/day-1-toy/bignum.py @@ -1 +1,2 @@ -# 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 diff --git a/src/day-1-toy/cal.py b/src/day-1-toy/cal.py index 2a3771eb5b..920fe96e4c 100644 --- a/src/day-1-toy/cal.py +++ b/src/day-1-toy/cal.py @@ -14,3 +14,8 @@ # docs for the calendar module closely. import sys +import calendar + +year = int(input("enter year:")) +month = int(input("enter month:")) +print(calendar.month(year, month)) diff --git a/src/day-1-toy/comp.py b/src/day-1-toy/comp.py index 083e9b9140..942823ea84 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(1,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 = [i.upper() for i 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 = [] #need help print(y) 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 diff --git a/src/day-1-toy/dicts.py b/src/day-1-toy/dicts.py index eac1779a42..701d2fe61f 100644 --- a/src/day-1-toy/dicts.py +++ b/src/day-1-toy/dicts.py @@ -25,5 +25,7 @@ ] # Write a loop that prints out all the field values for all the waypoints - +for w in waypoints: + print(w['lat'], w['lon'], w['name']) # Add a new waypoint to the list +waypoints.append({'lat':1, 'lon':2, 'name':'nowhere'})# doesnt seem to add waypoint \ No newline at end of file diff --git a/src/day-1-toy/func.py b/src/day-1-toy/func.py index 2b7f435ffa..844452543c 100644 --- a/src/day-1-toy/func.py +++ b/src/day-1-toy/func.py @@ -3,4 +3,11 @@ # Read a number from the keyboard num = input("Enter a number: ") -# Print out "Even!" if the number is even. Otherwise print "Odd" \ No newline at end of file +# Print out "Even!" if the number is even. Otherwise print "Odd" +def is_even(num): + if int(num) % 2 == 0: + print("even") + else: + print("odd") + +is_even(num) \ No newline at end of file diff --git a/src/day-1-toy/hello.py b/src/day-1-toy/hello.py index 37968da4d4..6ee1f01872 100644 --- a/src/day-1-toy/hello.py +++ b/src/day-1-toy/hello.py @@ -1 +1,2 @@ -# 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..fc6be3b957 100644 --- a/src/day-1-toy/lists.py +++ b/src/day-1-toy/lists.py @@ -7,23 +7,25 @@ # For the following, DO NOT USE AN ASSIGNMENT (=). # Change x so that it is [1, 2, 3, 4] -# [command here] +x.insert(3,4) print(x) # Using y, change x so that it is [1, 2, 3, 4, 8, 9, 10] -# [command here] +x.extend(y) print(x) # Change x so that it is [1, 2, 3, 4, 9, 10] -# [command here] +x.pop(4) 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 -# [command here] +x.count 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 diff --git a/src/day-1-toy/printf.py b/src/day-1-toy/printf.py index d4bc9abb48..e17266d55d 100644 --- a/src/day-1-toy/printf.py +++ b/src/day-1-toy/printf.py @@ -7,4 +7,5 @@ # x is 10, y is 2.25, z is "I like turtles!" -# 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("{} {} {}".format(10, 2.24552, "I like turtles!")) \ No newline at end of file From a97bcef24b8a9990a8fdfab99fb7772c77d29245 Mon Sep 17 00:00:00 2001 From: kammagik Date: Tue, 31 Jul 2018 18:37:55 -0700 Subject: [PATCH 03/18] obj.py --- src/day-1-toy/obj.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/day-1-toy/obj.py b/src/day-1-toy/obj.py index 84c78a2f53..60de120614 100644 --- a/src/day-1-toy/obj.py +++ b/src/day-1-toy/obj.py @@ -1,21 +1,45 @@ # 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): + super().__init__(lat, lon) + self.name = name + def change(self): + return "{}: latitude {}, longitude {}".format( + self.name, self.lat, self.lon) # Make a class Geocache that can be passed parameters `name`, `difficulty`, # `size`, `lat`, and `lon` to the constructor. What should it inherit from? - +class Geocache(Waypoint): + def __init__(self, name, difficulty, size, lat, lon): + super().__init__(name, lat, lon) + self.size = size + self.name = name + self.difficulty = difficulty + def change(self): + return "{}: difficulty {}, size {}, latitude {}, longitude {}".format( + self.name, self.difficulty, self.size, self.lat, self.lon) # Make a new waypoint "Catacombs", 41.70505, -121.51521 +newWaypoint = Waypoint("Catacombs", 41.70505, -121.51521) # Print it -# +print(newWaypoint.change()) + # Without changing the following line, how can you make it print into something # more human-readable? +w = newWaypoint.change() print(w) # Make a new geocache "Newberry Views", diff 1.5, size 2, 44.052137, -121.41556 +newGeocache = Geocache("Newberry Views", 1.5, 2, 44.052137, -121.41556) # Print it--also make this print more nicely +g = newGeocache.change() print(g) From f8a78557938eff33b3375df5a0909197d497299a Mon Sep 17 00:00:00 2001 From: kammagik Date: Tue, 31 Jul 2018 18:58:05 -0700 Subject: [PATCH 04/18] printf --- src/day-1-toy/printf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/day-1-toy/printf.py b/src/day-1-toy/printf.py index e17266d55d..95760fe1be 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 %.2f, z is "%s"' % (x,y,z)) # Use the 'format' string method to print the same thing -print("{} {} {}".format(10, 2.24552, "I like turtles!")) \ No newline at end of file +print('x is {}, y is {:.2f}, z is "{}"'.format(x, y, z)) \ No newline at end of file From 8c57d66233e5013f5bf90af5a282776a3acb7402 Mon Sep 17 00:00:00 2001 From: kammagik Date: Tue, 31 Jul 2018 20:47:16 -0700 Subject: [PATCH 05/18] fileio, slice --- src/day-1-toy/bar.txt | 0 src/day-1-toy/fileio.py | 12 ++++++++---- src/day-1-toy/slice.py | 14 +++++++------- 3 files changed, 15 insertions(+), 11 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..e69de29bb2 diff --git a/src/day-1-toy/fileio.py b/src/day-1-toy/fileio.py index bc8e79b7cc..cf2420cf8d 100644 --- a/src/day-1-toy/fileio.py +++ b/src/day-1-toy/fileio.py @@ -1,12 +1,16 @@ # 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 - +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 - -# Close the file \ No newline at end of file +file.write(3) +# Close the file +file.close() \ No newline at end of file diff --git a/src/day-1-toy/slice.py b/src/day-1-toy/slice.py index 3c6cb38730..32f99edb01 100644 --- a/src/day-1-toy/slice.py +++ b/src/day-1-toy/slice.py @@ -1,26 +1,26 @@ 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[-2]) # 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:6]) # Output every element except the last one: [2, 4, 1, 7, 9] -print() +print(a[0:5]) # For string s... s = "Hello, world!" # Output just the 8th-12th characters: "world" -print() \ No newline at end of file +print(s[8:13]) \ No newline at end of file From 3acae1aad6be5631996b7b0ec06495d52e747d7d Mon Sep 17 00:00:00 2001 From: kammagik Date: Tue, 31 Jul 2018 20:58:25 -0700 Subject: [PATCH 06/18] added missing list for comp --- 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 942823ea84..6af0dbea58 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 = [] #need help +y = [i for i in x if int(i) % 2 == 0] print(y) From 89f81bab677a0bb6e22735083ac40b8bec0c63d4 Mon Sep 17 00:00:00 2001 From: kammagik Date: Tue, 31 Jul 2018 21:37:35 -0700 Subject: [PATCH 07/18] modules --- 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..7b7d5ec6b3 100644 --- a/src/day-1-toy/modules.py +++ b/src/day-1-toy/modules.py @@ -9,10 +9,11 @@ # Print out the plaform from sys: -print() +for argv in sys.argv: + print(argv) # Print out the Python version from sys: -print() +print(sys.platform) @@ -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 19c436d0be8f7b5fc58d23428afe202dae464e1b Mon Sep 17 00:00:00 2001 From: kammagik Date: Tue, 31 Jul 2018 21:52:02 -0700 Subject: [PATCH 08/18] tuples --- src/day-1-toy/tuples.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/day-1-toy/tuples.py b/src/day-1-toy/tuples.py index ec42b0cdf8..899539d7b2 100644 --- a/src/day-1-toy/tuples.py +++ b/src/day-1-toy/tuples.py @@ -22,11 +22,12 @@ def dist(a, b): # Write a function that prints all the values in a tuple -# def print_tuple(... - +def print_tuple(t): + for i in t: + print(i) t = (1, 2, 5, 7, 99) print_tuple(t) # Prints 1 2 5 7 99, one per line # Declare a tuple of 1 element then print it -u = (1) # What needs to be added to make this work? +u = (1,) # What needs to be added to make this work? print_tuple(u) From 0cd3320bc92ad229b71e3826bd08df778f1d861c Mon Sep 17 00:00:00 2001 From: kammagik Date: Thu, 2 Aug 2018 19:14:01 -0700 Subject: [PATCH 09/18] added room and player class --- src/days-2-4-adv/player.py | 3 +++ src/days-2-4-adv/room.py | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/days-2-4-adv/player.py b/src/days-2-4-adv/player.py index d79a175029..8c7fe43a74 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..d538f9aa2e 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 From 74c3318435c445d01f2600cd7742b17a4f1eb364 Mon Sep 17 00:00:00 2001 From: kammagik Date: Thu, 2 Aug 2018 19:33:05 -0700 Subject: [PATCH 10/18] changed player class --- src/days-2-4-adv/player.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/days-2-4-adv/player.py b/src/days-2-4-adv/player.py index 8c7fe43a74..4e9b012ada 100644 --- a/src/days-2-4-adv/player.py +++ b/src/days-2-4-adv/player.py @@ -1,5 +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 + def __init__(self, location): + self.location = rooms[location] \ No newline at end of file From f657810aaf76c36401409fdc4298678e00a94adb Mon Sep 17 00:00:00 2001 From: kammagik Date: Fri, 3 Aug 2018 15:07:48 -0700 Subject: [PATCH 11/18] player can move to directions --- src/days-2-4-adv/adv.py | 39 ++++++++++++++++++++++++++++++++++---- src/days-2-4-adv/player.py | 4 ++-- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index c9e26b0f85..ec118078e1 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 @@ -38,14 +39,44 @@ # # 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": + # * Prints the current room name + print(newPlayer.room.name) + # * Prints the current description (the textwrap module might be useful here). + print(newPlayer.room.description) # * Waits for user input and decides what to do. -# + dir = input("What why would you like to go? Enter n, w, e, s or q to quit") + # 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("Try another direction") + elif dir == "s": + if hasattr(newPlayer.room, "s_to"): + newPlayer.room = newPlayer.room.s_to + else: + print("Try another direction") + elif dir == "e": + if hasattr(newPlayer.room, "e_to"): + newPlayer.room = newPlayer.room.e_to + else: + print("Try another direction") + elif dir == "w": + if hasattr(newPlayer.room, "w_to"): + newPlayer.room = newPlayer.room.w_to + else: + print("Try another direction") + # Print an error message if the movement isn't allowed. -# # If the user enters "q", quit the game. + elif dir == "q": + print("Thank you, come again!") + else: + print("This movement isn't allowed") \ 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 4e9b012ada..96e9e533bb 100644 --- a/src/days-2-4-adv/player.py +++ b/src/days-2-4-adv/player.py @@ -1,5 +1,5 @@ # Write a class to hold player information, e.g. what room they are in # currently. class Player: - def __init__(self, location): - self.location = rooms[location] \ No newline at end of file + def __init__(self, room): + self.room = room \ No newline at end of file From f2771f129f604d7e085471d5bed555ee83b333cb Mon Sep 17 00:00:00 2001 From: kammagik Date: Fri, 3 Aug 2018 20:57:00 -0700 Subject: [PATCH 12/18] having trouble adding item --- src/days-2-4-adv/adv.py | 24 ++++++++++++++++++++++-- src/days-2-4-adv/item.py | 6 ++++++ src/days-2-4-adv/player.py | 3 ++- 3 files changed, 30 insertions(+), 3 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 ec118078e1..6e456676c3 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -1,5 +1,7 @@ from room import Room from player import Player +from item import Item + # Declare all the rooms @@ -22,6 +24,11 @@ earlier adventurers. The only exit is to the south."""), } +#Add Items +item = { + 'gold' : Item("Gold Coin", """Shiny gold coin"""), + 'silver' : Item("Silver Coin", """Shiny silver coin""") +} # Link rooms together @@ -34,12 +41,17 @@ room['narrow'].n_to = room['treasure'] room['treasure'].s_to = room['narrow'] -# + +#Add to item room +room['foyer'].addItem(Item['Gold']) +room['outside'].addItem(Item['Silver']) + # Main # # 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": @@ -73,7 +85,15 @@ newPlayer.room = newPlayer.room.w_to else: print("Try another direction") - + + # Check inventory + elif command[1] in ["i", "inventory"]: + if len(newplayer.inventory) > 0: + print("Your inventory is:") + for i in newPlayer.inventory: + print(" " + i.description) + else: + print("There is nothing in your inventory") # Print an error message if the movement isn't allowed. # If the user enters "q", quit the game. elif dir == "q": diff --git a/src/days-2-4-adv/item.py b/src/days-2-4-adv/item.py new file mode 100644 index 0000000000..b20cb11381 --- /dev/null +++ b/src/days-2-4-adv/item.py @@ -0,0 +1,6 @@ +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 96e9e533bb..50945aef31 100644 --- a/src/days-2-4-adv/player.py +++ b/src/days-2-4-adv/player.py @@ -2,4 +2,5 @@ # currently. class Player: def __init__(self, room): - self.room = room \ No newline at end of file + self.room = room + self.inventory = [] \ No newline at end of file From 69ad2c6cff7554ba2e0fa296e3f7b4fe235d454e Mon Sep 17 00:00:00 2001 From: kammagik Date: Sat, 4 Aug 2018 10:22:12 -0700 Subject: [PATCH 13/18] a few letters can be added until error occurs --- src/mini-challenge/hangman.py | 74 ++++++++++++++++++++++++++++++++ src/mini-challenge/words.txt | 80 +++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 src/mini-challenge/hangman.py create mode 100644 src/mini-challenge/words.txt diff --git a/src/mini-challenge/hangman.py b/src/mini-challenge/hangman.py new file mode 100644 index 0000000000..769cd9e19a --- /dev/null +++ b/src/mini-challenge/hangman.py @@ -0,0 +1,74 @@ +# There are at least 10 "mistakes" in this Hangman game +# These could include syntax errors, logical errors, spelling errors... +# Find the mistakes and fix the game! +# +# 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! + +# Initial setup +import random +bodies = [ " ------\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---", +" ------\n | |\n | O\n | \|/\n | |\n | / \ \n |\n |\n---" ] +strikes = 0 +words = [None] +file = open("words.txt", "r") +for line in file: + words.append(line) +file.close() +targetWord = words[random.randint(0, 100)] +lettersLeft = len(targetWord)-1 +length = len(targetWord)-1 +curWord = "_" * length +alphabet = [chr(65+x) for x in range(1, 26) ] + +# Draw body based on # of incorrect guesses +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 + +# Add spaces when displaying letters / blanks for readability +def printWord( word ): + prntWord = "" + for letter in word: + prntWord += letter + " " + print(prntWord) + +# Begin game +print( "Welcome to Hangmn!" ) +printWord(curWord) +drawBody() +print("Letters left:") +printWord(alphabet) + +# Gameplay loop +while strikes < 5 and lettersLeft > 0: + letter = input("\nPlease guess a letter...") + if letter in targetWord: + print("Great!") + fillLetters(letter) + else: + strikes += 1 + print(str( strikes) + " / 5 strikes" ) + printWord(curWord) + drawBody() + alphabet.remove(letter.upper()) + print("Letters left:") + printWord(alphabet) + +# Game over, print outcome +if lettersLeft < 0: + print("YOU WIN!!") +else: + print("YOU LOSE...word was " + targetWord) \ No newline at end of file diff --git a/src/mini-challenge/words.txt b/src/mini-challenge/words.txt new file mode 100644 index 0000000000..d62f474859 --- /dev/null +++ b/src/mini-challenge/words.txt @@ -0,0 +1,80 @@ +adhesive +amuse +arson +battery +blimp +bunny +buzzcut +carjack +carriage +cashbox +cocoon +colonel +crushing +demolishment +doberman +drizzle +elephant +exclaim +fermentation +fighting +flimsy +gazelle +generation +glowing +gritty +headquarters +hearse +heart +heretical +hopper +honeybee +injured +itching +jacuzzi +jukebox +ketchup +kickoff +kleenex +lacquer +legendary +limitless +liquify +marble +mittens +mummify +mystical +necrotic +nickle +nitpick +north +orphanage +overjoy +patient +photograph +plantation +quack +queen +quickly +refugee +sizzled +smallpox +southern +squawks +taxicab +thickly +tweezer +undersea +useless +violent +vulture +walkway +warning +waxlike +wheezed +xeroxed +yawning +yellow +zephyr +zipper +zombie \ No newline at end of file From 3dce6b94e5855097d0a1d0b43506d2a67fa2f506 Mon Sep 17 00:00:00 2001 From: kammagik Date: Sat, 4 Aug 2018 10:31:20 -0700 Subject: [PATCH 14/18] i think it works now --- src/mini-challenge/hangman.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mini-challenge/hangman.py b/src/mini-challenge/hangman.py index 769cd9e19a..c09e7de2a5 100644 --- a/src/mini-challenge/hangman.py +++ b/src/mini-challenge/hangman.py @@ -34,6 +34,7 @@ def drawBody(): def fillLetters( letter ): for i in range(len(targetWord)-1): if( targetWord[i : i+1]) == letter: + global curWord # maybe this works? curWord = curWord[0: i] + letter + curWord[i: ] global lettersLeft lettersLeft -= 1 From 8fb2ee4c1381fca13ebebe817a7209cbe9f64837 Mon Sep 17 00:00:00 2001 From: kammagik Date: Sat, 4 Aug 2018 10:52:11 -0700 Subject: [PATCH 15/18] does not allow all letters --- src/mini-challenge/hangman.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mini-challenge/hangman.py b/src/mini-challenge/hangman.py index c09e7de2a5..79f5e0e520 100644 --- a/src/mini-challenge/hangman.py +++ b/src/mini-challenge/hangman.py @@ -47,7 +47,7 @@ def printWord( word ): print(prntWord) # Begin game -print( "Welcome to Hangmn!" ) +print( "Welcome to Hangman!" ) printWord(curWord) drawBody() print("Letters left:") From e1509580de86fb7d9b1cb16d71b7ab0e9ecfa0c4 Mon Sep 17 00:00:00 2001 From: kammagik Date: Sat, 4 Aug 2018 11:22:02 -0700 Subject: [PATCH 16/18] included solution code --- src/mini-challenge/hangman.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/mini-challenge/hangman.py b/src/mini-challenge/hangman.py index 79f5e0e520..c0532b3924 100644 --- a/src/mini-challenge/hangman.py +++ b/src/mini-challenge/hangman.py @@ -13,18 +13,18 @@ " ------\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") for line in file: words.append(line) file.close() -targetWord = words[random.randint(0, 100)] +targetWord = words[random.randint(0, len(words))] lettersLeft = len(targetWord)-1 length = len(targetWord)-1 curWord = "_" * length -alphabet = [chr(65+x) for x in range(1, 26) ] +alphabet = [chr(65+x) for x in range(0, 26) ] # Draw body based on # of incorrect guesses def drawBody(): @@ -35,7 +35,7 @@ def fillLetters( letter ): for i in range(len(targetWord)-1): if( targetWord[i : i+1]) == letter: global curWord # maybe this works? - curWord = curWord[0: i] + letter + curWord[i: ] + curWord = curWord[0: i] + letter + curWord[i+1: ] global lettersLeft lettersLeft -= 1 @@ -69,7 +69,7 @@ def printWord( word ): printWord(alphabet) # Game over, print outcome -if lettersLeft < 0: +if lettersLeft == 0: print("YOU WIN!!") else: print("YOU LOSE...word was " + targetWord) \ No newline at end of file From 1ff9a1eadef397b0068988fffae10bf0bb711c1f Mon Sep 17 00:00:00 2001 From: kammagik Date: Thu, 9 Aug 2018 19:28:22 -0700 Subject: [PATCH 17/18] items appear in room --- src/days-2-4-adv/adv.py | 90 ++++++++++++++++++++++++++++---------- src/days-2-4-adv/item.py | 24 +++++++++- src/days-2-4-adv/player.py | 5 ++- src/days-2-4-adv/room.py | 11 ++++- 4 files changed, 103 insertions(+), 27 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index 6e456676c3..d78bb47c36 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -1,33 +1,33 @@ from room import Room from player import Player -from item import Item +from item import Item, Treasure # Declare all the rooms room = { 'outside': Room("Outside Cave Entrance", - "North of you, the cave mount beckons"), + "North of you, the cave mount beckons", []), 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty passages run north and east."""), 'overlook': Room("Grand Overlook", """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.""", []), 'narrow': Room("Narrow 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.""", []), 'treasure': Room("Treasure Chamber", """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.""", []), } #Add Items item = { - 'gold' : Item("Gold Coin", """Shiny gold coin"""), - 'silver' : Item("Silver Coin", """Shiny silver coin""") + 'sword' : Item("small sword", "small sword to fight with"), + 'shield' : Item("wooden shield", "durable shild to protect you") } # Link rooms together @@ -43,11 +43,10 @@ #Add to item room -room['foyer'].addItem(Item['Gold']) -room['outside'].addItem(Item['Silver']) +room['foyer'].addItem(item['sword']) +room['overlook'].addItem(item['shield']) -# Main -# +#treasure gold, silver # Make a new player object that is currently in the 'outside' room. newPlayer = Player(room['outside']) @@ -55,39 +54,84 @@ dir = "" # Write a loop that: while not dir == "q": - + def room_info(): # * Prints the current room name - print(newPlayer.room.name) + print(newPlayer.room.name) # * Prints the current description (the textwrap module might be useful here). - print(newPlayer.room.description) -# * Waits for user input and decides what to do. - dir = input("What why would you like to go? Enter n, w, e, s or q to quit") + print(newPlayer.room.description) +# see items in the room + + if len( newPlayer.room.items ) == 0: + print(" nothing") + else: + print("There is an item in this room: ") + for i in newPlayer.room.items: + print(" " + str(i)) +# * Waits for user input and decides what to do. + dir = input("Where would you like to go? Enter n, w, e, s or q to quit") + splt_cmd = dir.split() + if len(splt_cmd) > 1: + action = splt_cmd[0] + item = "" + for i in range(1, len(splt_cmd[i])): + item += splt_cmd[i] + " " + item = item.strip() + +#grabbing or dropping + elif action =="g" or action == "grab": + for i in newPlayer.room.items: + if splt_cmd[1] == i.name: + i.on_grab( newPlayer ) + + cmd = input("you grabbed an item") + else: + print("invalid selection") + +#drop + if action == "d" or action == "drop": + for i in newPlayer.room.items: + if splt_cmd[1] == i.name: + i.on_grab( newPlayer ) + + cmd = input("you dropped an item") + else: + print("invalid selection") +#inventory + if cmd == "i" or cmd == "inventory": + print("inventory: ") + if len(newPlayer.items ) == 0: + print("you don't have anything in your inventory") + for i in newplayer.item: + print("\t" + str(i)) +#score + elif cmd == "score": + print("score: " + str(newPlayer.score) + "\n") # If the user enters a cardinal direction, attempt to move to the room there. - if dir == "n": +if dir == "n": if hasattr(newPlayer.room, "n_to"): newPlayer.room = newPlayer.room.n_to else: print("Try another direction") - elif dir == "s": +elif dir == "s": if hasattr(newPlayer.room, "s_to"): newPlayer.room = newPlayer.room.s_to else: print("Try another direction") - elif dir == "e": +elif dir == "e": if hasattr(newPlayer.room, "e_to"): newPlayer.room = newPlayer.room.e_to else: print("Try another direction") - elif dir == "w": +elif dir == "w": if hasattr(newPlayer.room, "w_to"): newPlayer.room = newPlayer.room.w_to else: print("Try another direction") # Check inventory - elif command[1] in ["i", "inventory"]: +elif command[1] in ["i", "inventory"]: if len(newplayer.inventory) > 0: print("Your inventory is:") for i in newPlayer.inventory: @@ -96,7 +140,7 @@ print("There is nothing in your inventory") # Print an error message if the movement isn't allowed. # If the user enters "q", quit the game. - elif dir == "q": +elif dir == "q": print("Thank you, come again!") - else: +else: print("This movement isn't allowed") \ No newline at end of file diff --git a/src/days-2-4-adv/item.py b/src/days-2-4-adv/item.py index b20cb11381..391deff3ce 100644 --- a/src/days-2-4-adv/item.py +++ b/src/days-2-4-adv/item.py @@ -3,4 +3,26 @@ def __init__(self, name, description): self.name = name self.description = description - \ No newline at end of file + def __str__(self): + return f"{self.name}" + + def on_grab(self, player): + print("item picked up") + # player has item + newPlayer.item.append(self) + #remove item from room + + newPlayer.room.item.remove(self) + +class Treasure(Item): + def __init__ (self, name , descrition, value): + super ().__init__(self, name, descrition) + self.value = value + self.picked_up = False + + def on_grab(self, player): + super().on_grab(player) + if self.picked_up == False: + #score updated + newPlayer.score += self.value + self.pickeup_up == True \ 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 50945aef31..1f820ee852 100644 --- a/src/days-2-4-adv/player.py +++ b/src/days-2-4-adv/player.py @@ -1,6 +1,7 @@ # Write a class to hold player information, e.g. what room they are in # currently. class Player: - def __init__(self, room): + def __init__(self, room, inventory=[]): self.room = room - self.inventory = [] \ No newline at end of file + self.inventory = inventory + self.score = 0 \ 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 d538f9aa2e..1a3a2bf5fb 100644 --- a/src/days-2-4-adv/room.py +++ b/src/days-2-4-adv/room.py @@ -1,6 +1,15 @@ # 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 + self.items = items + + + def addItem (self, item): + self.items.append(item) + + + + From aea3aa17db674a43355612e1c9b4bbf86ef0d1ff Mon Sep 17 00:00:00 2001 From: kammagik Date: Thu, 9 Aug 2018 20:46:46 -0700 Subject: [PATCH 18/18] getting errors line77 --- src/days-2-4-adv/adv.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/days-2-4-adv/adv.py b/src/days-2-4-adv/adv.py index d78bb47c36..6c1e217eb6 100644 --- a/src/days-2-4-adv/adv.py +++ b/src/days-2-4-adv/adv.py @@ -43,7 +43,7 @@ #Add to item room -room['foyer'].addItem(item['sword']) +room['outside'].addItem(item['sword']) room['overlook'].addItem(item['shield']) #treasure gold, silver @@ -74,39 +74,38 @@ def room_info(): splt_cmd = dir.split() if len(splt_cmd) > 1: action = splt_cmd[0] - item = "" - for i in range(1, len(splt_cmd[i])): - item += splt_cmd[i] + " " + for i in range(1, len(splt_cmd[i])): + item += splt_cmd[i] + " " item = item.strip() #grabbing or dropping - elif action =="g" or action == "grab": - for i in newPlayer.room.items: +if action =="g" or action == "grab": + for i in newPlayer.room.items: if splt_cmd[1] == i.name: i.on_grab( newPlayer ) - cmd = input("you grabbed an item") - else: - print("invalid selection") + cmd = input("you grabbed an item") + # else: + # print("invalid selection") #drop - if action == "d" or action == "drop": - for i in newPlayer.room.items: +if action == "d" or action == "drop": + for i in newPlayer.room.items: if splt_cmd[1] == i.name: i.on_grab( newPlayer ) - cmd = input("you dropped an item") - else: - print("invalid selection") + cmd = input("you dropped an item") + # else: + # print("invalid selection") #inventory - if cmd == "i" or cmd == "inventory": +if cmd == "i" or cmd == "inventory": print("inventory: ") if len(newPlayer.items ) == 0: print("you don't have anything in your inventory") for i in newplayer.item: print("\t" + str(i)) #score - elif cmd == "score": +if cmd == "score": print("score: " + str(newPlayer.score) + "\n") # If the user enters a cardinal direction, attempt to move to the room there. if dir == "n":