Code Toolkit: Python, Fall 2020

Week 11 — Homework assignment

due: Tues, November 17, 8pm

  1. Review the class notes for this week.

    Please note: we also have a reading for this week. Check the schedule page (under week 10).

  2. [Python] Create a simple word definition lookup program. Open your text editor (Atom) and create a new file. Create a new dictionary variable. Add key-value pairs that correspond to words and their definitions. You can add as many as you'd like.

    Now, use the Python command raw_input() to ask the user for a word. The argument to this command is a prompt, and the return value is the string that the user entered. You can read more in the Python documentation here. So for example:

    user_text = raw_input("Please enter a word: ")
    
    The variable user_text will now contain the user input, which you can use as a key with your dictionary.

    Look up the definition for the word in your dictionary and print the definition to the user.

    Save your new file as homework_11_part_1.py — you could use any name you'd like, but you should at least make sure to use .py at the end as the file extension. Run the program by going to the command line and typing:

    $ python homework_11_part_1.py
    

    Try entering some words for which you have definitions, and also try entering a word for which you don't have a definition. You should get a KeyError. There are several ways to fix this, but one way would be with an if statement that uses the in operator. If the user input is in your dictionary, print the definition to the user. Otherwise (else) print an error message.

    Bonus. Can you make it so that after printing a definition, you ask the user for another word and repeat indefinitely? Hint 1: try putting the whole thing in a while loop. But how will the user ever escape?! Hint 2: offer them a special exit word. For example:

    user_text = ""
    while user_text != "exit":
        user_text = raw_input("Please enter a word ('exit' to escape): ")
        # etc ...
    

  3. [Processing] Create a Processing program that saves some state so that when you re-open the program next time, the user sees where they left off.

    1. Create a simple sketch that draws something to the screen with 2-4 variables, and add a keyPressed() block to let the user change those variables somehow.

    2. Add import json as the first line of your sketch. Read the Python documentation about the JSON library if you'd like.

    3. Read the documentation on the Processing loadStrings() command. Add the following code snippet into your setup() block:

          savedText = loadStrings("saved-state.json")
          if savedText:
              j = json.loads(savedText[0])
              x = j["x"]
              y = j["y"]
          else:
              x = 250
              y = 250
      
      Replace x and y with whatever variables you have added to your sketch from step 1 (more than two is fine). Replace 250 with whatever default values you wish those variables to have when the user first runs your program. saved-state.json will be the name of the file that gets created. You can name it whatever you'd like but should at least add .json as the file extension.

    4. Read the documentation on the Processing createWriter() command. Add the following code snippet into your keyPressed() block:

          if key == ' ':
              d = {
                   "x": x,
                   "y": y
                   }
              output = createWriter("saved-state.json")
              output.print(json.dumps(d))
              output.flush()
              output.close()
      
      Again, replace x and y with whatever variables you have used (again, more than two is fine). saved-state.json must be the same filename that you used from step 3.

    If you follow those steps, then the user should be able use some key presses to modify a composition, then press the space key to save whatever variables you are using, then close and re-open the sketch to see what they had done before.

    I had imagined you using variables for things like the x and y position of a shape, or its color. Can you also add a variable that controls the number of things being drawn? (Hint, simply use the variable to control the number of repetitions of a loop.) Or something else entirely.