Collab3 (Fri 2-Feb)

Collaborative, in-person, in recitation.


Notes:
  1. The same basic rules as collab1 apply here.
  2. Be sure to read this entire write-up before starting!
  3. Do not use lists, dicts, sets, or any topics not covered in Week3 or Hw3.

Part 1: install and run Python 3.11 locally
For this collab, you must run Python 3.11 on your laptop and not in CS Academy. If you are sure that you can run Python 3.11 locally (on your laptop) using the IDE of your choice (IDLE, VS Code, Sublime, etc), then you can skip this step.

First, importantly: do not install the most-recent version of Python (3.12 or 3.13). These may not work with CS Academy graphics!

Instead, install Python 3.11 from here. Scroll to the "Files" section, then find the macOS or Windows installer you need, download it, and run it.

Next, if you do not have an IDE installed, to keep things simple this week, just use IDLE, which is the default IDE that is installed when you install Python. After you install Python, you can launch the IDLE app like any other app.

When IDLE first runs, it only shows the shell (the output console). So the first thing you'll want to do (usually) is "File / Open..." to open an existing Python file, or (as you should do here) "File / New File" to create a new Python file. Then save that in a file name ending in ".py", something like "foo.py".

Finally, to be sure you have Python installed properly, run this code on your laptop:
def main():
    print('I do have Python working on my laptop!')

main()
If these steps do not work for you, a TA will be happy to assist.
Part 2: pip install and use pyjokes
Before we do the main task, we will have some fun while making sure you can install third-party modules using pip. In particular, we will see some silly jokes using the pyjokes module.

First, you need to pip install pyjokes. The thing is, you may have several versions of Python on your laptop, and you want to make sure you install pyjokes in the version of Python that you are using. Here is a simple fix: using the same IDE that you will run your collab code in, first run the following code to install a pip module:
import sys, os

def runPipCommand(pipCommand, pipPackage=None):
    # get quoted executable path
    quote = '"' if 'win' in sys.platform else "'"
    executablePath = f'{quote}{sys.executable}{quote}'
    # first upgrade pip:
    command = f'{executablePath} -m pip -q install --upgrade pip'
    os.system(command)
    # input the package from the user if it's not supplied in the call:
    if pipPackage == None:
        pipPackage = input(f'Enter the pip package for {pipCommand} --> ')
    # then run pip command:
    command = f'{executablePath} -m pip {pipCommand} {pipPackage}'
    os.system(command)

runPipCommand('install', 'pyjokes') # <-- you can replace 'pyjokes' with other module names to install them!
If pyjokes was already installed, you will get a message like "Requirement already satisfied". That's fine.

If these steps do not work for you, a TA will be happy to assist.

Once you have pyjokes installed properly, run the following code and have fun with it! :-)
import pyjokes

def main():
    while True:
        print('*********************************')
        print(pyjokes.get_joke())
        print('*********************************')
        response = input('\nPress enter to continue, q to quit --> ')
        if response == 'q': break
        print()
    print('Goodbye!')

main()

Part 3: write playTriviaTester
Your main task today is to write the function playTriviaTester() so that your app closely (if perhaps not perfectly) matches the app in this video:

Some key points:
Starter Code:
# Be sure to first read the writeup and carefully watch the video!

import random

# This requires the file open-trivia-one-word-answers.txt,
# which is derived from the file open-trivia-all.json
# in the open-trivia-database repository on github:
# https://github.com/el-cms/Open-trivia-database

def readFile(path):
    with open(path, 'rt', encoding='utf-8') as f:
        return f.read()

def getRandomLine(multilineText):
    return random.choice(multilineText.splitlines())

def playTriviaTester():
    return 42 # replace this with your code!

def main():
    playTriviaTester()

main()

Design Requirements:
You must follow these design requirements:
  1. The categories you print must be in so-called "title case", where each first letter is capitalized and the other letters are in lowercase. You may wish to use s.title() for this. Also, the categories should print using spaces in place of underscores.
  2. The questions must end in a question mark, and without an extra space at the end of the question. As you will see, some questions already include question marks, others do not. Some questions end in an extra space, others do not. You will need to handle all these cases.
  3. When you verify answers, you should be case-insensitive, so 'Chicago' would match 'chicago', for example.
  4. While it would be more elegant to write this app using a list to store the trivia questions, do not use lists. It is possible to write this app (as we have) without them.

Important Hints:
  1. We included the function readFile(path) for you. You will need to use this function to load the contents of the file open-trivia-one-word-answers.txt into a multiline string. In particular, you will probably want a line of code like this:
    text = readFile('open-trivia-one-word-answers.txt')
  2. We also included the function getRandomLine(multilineText) for you. If you call this function with a string of multiline text, it will return a random line from that text. Very useful here!
  3. This example may be useful for you:
    info = 'CMU,5000 Forbes Ave,Pittsburgh'
    school, streetAddress, city = info.split(',')
    print(school)        # CMU
    print(streetAddress) # 5000 Forbes Ave
    print(city)          # Pittsburgh
  4. As usual, get things working without worrying too much about the output formatting. Only then get the formatting right.

Time permitting:
If you finish the creative coding task above, you and your partner(s) should work together on Additional Code Tracing Exercises from 2.4.14.