Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lisa Cee #3

Open
wants to merge 52 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
4459769
hello, slice complete
LisaCee Jul 31, 2018
7b5d2ce
datetypes
LisaCee Jul 31, 2018
c315a43
modules
LisaCee Jul 31, 2018
c62f269
printf, but trailing 000s
LisaCee Jul 31, 2018
ab96868
bignum rounding
LisaCee Aug 1, 2018
0770c79
tuples
LisaCee Aug 1, 2018
0c3823a
slice
LisaCee Aug 1, 2018
00ea91e
started comp
LisaCee Aug 1, 2018
cc50a0d
dicts
LisaCee Aug 1, 2018
701c91f
some func
LisaCee Aug 1, 2018
2c8f8ab
args
LisaCee Aug 1, 2018
6592b58
scope
LisaCee Aug 1, 2018
d333ac4
fileio
LisaCee Aug 1, 2018
07144a2
obj
LisaCee Aug 1, 2018
e058db2
cal wip
LisaCee Aug 1, 2018
c8cbe3e
adv printing room and description
LisaCee Aug 2, 2018
afaab8d
move between rooms, no error
LisaCee Aug 2, 2018
b93226b
lots of attempts, still not printing error
LisaCee Aug 3, 2018
4ab1d39
errors working
LisaCee Aug 3, 2018
9b8605f
clean up code
LisaCee Aug 3, 2018
8e15075
updated cal.py formatting
LisaCee Aug 3, 2018
8afd743
items and actions, inventory, menu
LisaCee Aug 7, 2018
785ab35
added new player name functionality
LisaCee Aug 8, 2018
a41c09f
input to lower case
LisaCee Aug 8, 2018
20a02a1
drop method working
LisaCee Aug 8, 2018
093068d
changing theme to Doctor Who
LisaCee Aug 8, 2018
ce69742
continued doctor who theme
LisaCee Aug 8, 2018
9f551da
added treasure, score, moved item methods to item
LisaCee Aug 9, 2018
0f28c87
added light and drop_light
LisaCee Aug 9, 2018
6cc336b
added light-room functionality, need to add if light is in room funct…
LisaCee Aug 9, 2018
9b74655
isinstance of light in room working
LisaCee Aug 9, 2018
2e9a158
mvp, all light messages working
LisaCee Aug 9, 2018
7be8e23
cleaned up code, added method if item is not in room, player does not…
LisaCee Aug 10, 2018
c1819d2
final commit of class
LisaCee Aug 10, 2018
a1076dd
reset to default, without my answers
LisaCee Nov 27, 2019
43c7afa
hello
LisaCee Nov 27, 2019
e084b4a
bignum
LisaCee Nov 27, 2019
c7a8357
data types
LisaCee Nov 27, 2019
5139d96
modules
LisaCee Nov 27, 2019
9ee4148
printing
LisaCee Nov 27, 2019
54f106a
started with lists
LisaCee Nov 27, 2019
f5a03bf
lists
LisaCee Nov 28, 2019
f1b8ddd
tuples
LisaCee Nov 28, 2019
badebe7
slices
LisaCee Nov 28, 2019
0314f69
list comp
LisaCee Dec 1, 2019
b011015
dicts
LisaCee Dec 1, 2019
f972dad
functions
LisaCee Dec 1, 2019
cd4e94c
args
LisaCee Dec 1, 2019
8235756
scope
LisaCee Dec 2, 2019
6a728d2
file io
LisaCee Dec 3, 2019
37c7cc4
calendar
LisaCee Dec 3, 2019
a6838e7
classes with some help
LisaCee Dec 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
618 changes: 618 additions & 0 deletions FAQ.md

Large diffs are not rendered by default.

275 changes: 39 additions & 236 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Intro to Python

## Goals
# Intro to Python I

It's time to learn a new language! Python!

Expand Down Expand Up @@ -57,254 +55,59 @@ best for learning new languages.

You can exit the virtual environment by typing `exit`.

## Day 1

### Goals
## Goals

* Learn the basic syntax and structure of Python

### Summary

* Implement a number of tiny Python programs that demonstrate Python syntax.
## Summary

### Instructions
* Implement a number of tiny Python programs that demonstrate Python syntax and
language concepts.

Take a look in the `src/` directory.
## Instructions

NOTE: `adv/` is for Day 2, so ignore it for today.
Each directory inside the `src/` directory presents exercises revolving around a
particular concept in Python. Not all of these concepts are unique to Python (in
fact, most probably aren't). This means that you can leverage knowledge you've
obtained via exposure to other programming languages towards learning Python.

Suggested order for implementing the toy programs:
The suggested order for going through each of the directories is:

* `hello` -- Hello world
* `bignum` -- Print some big numbers
* `datatypes` -- Experiment with type conversion
* `modules` -- Learn to import from modules
* `printf` -- Formatted print output
* `printing` -- Formatted print output
* `lists` -- Python's version of arrays
* `tuples` -- Immutable lists typically for heterogenous data
* `slice` -- Accessing parts of lists
* `comp` -- List comprehensions
* `dicts` -- Dictionaries
* `func` -- Functions
* `slices` -- Accessing parts of lists
* `comprehensions` -- List comprehensions
* `dictionaries` -- Dictionaries
* `functions` -- Functions
* `args` -- Arguments and Keyword Arguments
* `scope` -- Global, Local, and Non-Local scope
* `fileio` -- Read and write from files
* `cal` -- Experiment with module imports
* `obj` -- Classes and objects

## Day 2

### Goals

* Solidify the Python basics


### Summary

* Implement a basic text adventure game
* Add classes for rooms and the player
* Add a simple parser that reads user input and performs actions

### Instructions

This is in `src/adv/`. Check it out!

* Put the Room class in room.py based on what you see in `adv.py`.

* Put the Player class in `player.py`.

* Follow the instructions `adv.py`.

* Figure out what all those `.pyc` files are that appear after you successfully
run the program.

## Day 3

### Goals

* Prepare for more OOP techniques
* Practice classes and lists

### Summary

* Add items to the game that the user can carry around
* Make rooms able to hold multiple items
* Make the player able to carry multiple items
* Add two-word commands to the parser
* Add the `get` and `drop` commands to the parser

### Instructions

* Add an `Item` class in a file `item.py`.

* This will be the _base class_ for specialized item types to be declared
later.

* The item should have `name` and `description` attributes.

* Hint: the name should be one word for ease in parsing later.

* Add capability to add items to rooms.

* The `Room` class should be extended with a `list` that holds the `Item`s
that are currently in that room.

* Add functionality to the main loop that prints out all the items that are
visible to the player when they are in that room.

* Add capability to add `Item`s to the player's inventory. The inventory can
also be a `list` of items "in" the player, similar to how `Item`s can be in a
`Room`.

* Add a new type of sentence the parser can understand: two words.

* Until now, the parser could just understand one sentence form:

`verb`

such as "n" or "q".

* But now we want to add the form:

`verb` `object`

such as "take coins" or "drop sword".

* Split the entered command and see if it has 1 or 2 words in it to determine
if it's the first or second form.

* Implement support for the verb `get` followed by an `Item` name. This will be
used to pick up `Item`s.

* If the user enters `get` or `take` followed by an `Item` name, look at the
contents of the current `Room` to see if the item is there.

* If it is there, remove it from the `Room` contents, and add it to the
`Player` contents.

* If it's not there, print an error message telling the user so.

* Implement support for the verb `drop` followed by an `Item` name. This is the
opposite of `get`/`take`.

* Add the `i` and `inventory` commands that both show a list of items currently
carried by the player.

## Day 4

### Goals

* Practice inheritance
* Practice method overriding
* Be able to call superclass methods
* Implement a callback/event structure

### Summary

* Add scoring
* Subclass items into treasures
* Subclass items into light sources
* Add methods to notify items when they are picked up or dropped
* Add light and darkness to the game

### Instructions

* Add a `score` to your `Player` class. Set it to 0.

* Add a single word command, `score`, that the user can type in to see their
current score.

* Add a subclass to `Item` called `Treasure`.

* The `Treasure` constructor should accept a name, description, and value.

* During world creation, add three `Treasure`s to convenient `Room`s.

* Add an `on_take` method to `Item`.

* Call this method when the `Item` is picked up by the player.

* The `Item` can use this to run additional code when it is picked up.

* Override `on_take` in `Treasure` so that the player gets the value of the
`Treasure` added to their `score` attribute _but only the first time the
treasure is picked up_.

* If the treasure is dropped and picked up again later, the player should
_not_ have the value added to their score again.

* Add an `on_drop` method to `Item`. Implement it similar to `on_take`.

* Add a subclass to `Item` called `LightSource`.

* During world creation, add a `lamp` `LightSource` to a convenient `Room`.

* Override `on_drop` in `LightSource` that tells the player "It's not wise to
drop your source of light!" if the player drops it. (But still lets them drop
it.)

* Add an attribute to `Room` called `is_light` that is `True` if the `Room` is
naturally illuminated, or `False` if a `LightSource` is required to see what
is in the room.

* Modify the main loop to test if there is light in the `Room` (i.e. if
`is_light` is `True` **or** there is a `LightSource` item in the `Room`'s
contents **or** if there is a `LightSource` item in the `Player`'s contents).

* If there is light in the room, display name, description, and contents as
normal.

* If there isn't, print out "It's pitch black!" instead.

* Hint: `isinstance` might help you figure out if there's a `LightSource`
among all the nearby `Item`s.

* Modify the `get`/`take` code to print "Good luck finding that in the dark!" if
the user tries to pick up an `Item` in the dark.
* `scopes` -- Global, Local, and Non-Local scope
* `file_io` -- Read and write from files
* `cal` -- Experiment with module imports and implement a text-based calendar
* `classes` -- Classes and objects

## Stretch Goals

In arbitrary order:

* Add more rooms.

* Add more items to the game.

* Add a way to win.

* Add more to the parser.

* Remember the last `Item` mentioned and substitute that if the user types
"it" later, e.g.

```
take sword
drop it
```

* Add `Item`s with adjectives, like "rusty sword" and "silver sword".

* Modify the parser to handle commands like "take rusty sword" as well as
"take sword".

* If the user is in a room that contains both the rusty sword _and_ silver
sword, and they type "take sword", the parser should say, "I don't know
which you mean: rusty sword or silver sword."

* Modify the code that calls `on_take` to check the return value. If `on_take`
returns `False`, then don't continue picking up the object. (I.e. prevent the
user from picking it up.)

* This enables you to add logic to `on_take` to code things like "don't allow
the user to pick up the dirt unless they're holding the shovel.

* Add monsters.

* Add the `attack` verb that allows you to specify a monster to attack.

* Add an `on_attack` method to the monster class.

* Similar to the `on_take` return value modification, above, have `on_attack`
prevent the attack from succeeding unless the user possesses a `sword` item.

* Come up with more stretch goals! The sky's the limit!
1. One of Python's main philosophical tenets is its emphasis on readability. To
that end, the Python community has standardized around a style guide called
[PEP 8](https://www.python.org/dev/peps/pep-0008/). Take a look at it and
then go over the code you've written and make sure it adheres to what PEP 8
recommends. Alternatively, PEP 8 linters exist for most code editors (you can
find instructions on installing a Python linter for VSCode
[here](https://code.visualstudio.com/docs/python/linting)). Try installing
one for your editor!

2. Rewrite code challenges you've solved before or projects you've implemented
before in a different language in Python. Start getting in as much practice
with the language as possible!

3. Write a program to determine if a number, given on the command line, is prime.

1. How can you optimize this program?
2. Implement [The Sieve of
Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes), one
of the oldest algorithms known (ca. 200 BC).
2 changes: 2 additions & 0 deletions src/00_hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Print "Hello, world!" to your terminal
print('Hello, world!')
5 changes: 5 additions & 0 deletions src/01_bignum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Print out 2 to the 65536 power
# (try doing the same thing in the JS console and see what it outputs)

# YOUR CODE HERE
print(pow(2, 65536))
22 changes: 22 additions & 0 deletions src/02_datatypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Python is a strongly-typed language under the hood, which means
that the types of values matter, especially when we're trying
to perform operations on them.

Note that if you try running the following code without making any
changes, you'll get a TypeError saying you can't perform an operation
on a string and an integer.
"""

x = 5
y = "7"

# Write a print statement that combines x + y into the integer value 12

# YOUR CODE HERE
print(x + int(y))

# Write a print statement that combines x + y into the string value 57

# YOUR CODE HERE
print(str(x) + y)
32 changes: 32 additions & 0 deletions src/03_modules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
In this exercise, you'll be playing around with the sys module,
which allows you to access many system specific variables and
methods, and the os module, which gives you access to lower-
level operating system functionality.
"""

import os
import sys
# See docs for the sys module: https://docs.python.org/3.7/library/sys.html

# Print out the command line arguments in sys.argv, one per line:
# YOUR CODE HERE
print(sys.argv)
# Print out the OS platform you're using:
# YOUR CODE HERE
print(sys.platform)
# Print out the version of Python you're using:
# YOUR CODE HERE
print(sys.version)

# See the docs for the OS module: https://docs.python.org/3.7/library/os.html

# Print the current process ID
# YOUR CODE HERE
print(os.getpid())
# Print the current working directory (cwd):
# YOUR CODE HERE
print(os.getcwd())
# Print out your machine's login name
# YOUR CODE HERE
print(os.getlogin())
18 changes: 18 additions & 0 deletions src/04_printing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Python provides a number of ways to perform printing. Research
how to print using the printf operator, the `format` string
method, and by using f-strings.
"""

x = 10
y = 2.24552
z = "I like turtles!"

# 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("x is {}, y is {:.2f}, z is \"{}\"".format(x, y, z))
# Finally, print the same thing using an f-string
print(f'x is {x}, y is {round(y, 2)}, z is "{z}"')
Loading