OIM3600 - Computer Science for Business Students

Session 13 (10/09)

contain

Today's Agenda

  • Welcome/News/Announcements
  • Class Review
  • How to Debug
  • Lectures:

Welcome/News/Announcements

  • Homework feedback will be provided in your GitHub issues or via PR.
    • Please respond.
  • Make sure you understand the code you use in your exercises and homework.
    • i.e. I may ask you to come to my office and explain any code that looks "interesting".
  • Communications:
    • Slack - for debugging, clarifications, real-time discussion, casual chat, etc.
    • Email - specify course # in subject title, e.g., "OIM3600: GitHub settings"
    • Meet with me in person during office hours at least once this semester.
  • Questions?

What we have learned so far

  • Code represent: 🔠🔢🈶😂🟥🟩🟦🖼 📽 🎼
  • Scratch
  • Git/GitHub
  • Programming
    • Functions: arguments, side effects, return values
    • Conditionals
    • Boolean expressions
    • Loops
    • Variables
    • Types: int, float, string, boolean, Nonetype, list, dict, ...
    • I/O

Homework Feedback & Coding Style

  • GitHub Issues
    • Reply with feedback or questions if further discussion is needed.
    • Reply with evidence (e.g., code, screenshots) and close if resolved.
  • Functionality
    • Use pseudocode for complex or unclear code/algorithms.
    • Write additional test code when necessary.
  • Documentation
    • Add docstrings for every function that you create.
    • Write clear and relevant comments.
  • Styles
    • Use lowercase and underscore for folder and file names.
    • Choose clear and meaningful variable and function names.

How to Debug

  • Format code first: Alt (⌥ option if macOS) + Shift + F in VSCode.
  • print()
  • PythonTutor.com
  • Debugging in VS Code
    1. Set breakpoints
    2. Run debugger: Press F5 to Start Debugging
      1. "Continue" ( F5 ): Keep running until you hit a stopping point.
      2. "Step Over" ( F10 ): Go through code line by line, skip functions.
      3. "Step Into" ( F11 ): Go inside functions and trace their lines.
    3. Inspect Variables: Use "VARIABLES" panel or "WATCH" panel.

Lectures

Conversions Between Data Structure Types

  1. string ⇔ list
  2. string ⇔ dictionary
  3. string ⇔ tuple
  4. string ⇔ set
  5. list ⇔ dictionary
  6. list ⇔ tuple
  7. list ⇔ set
  8. dictionary ⇔ tuple
  9. ...

API (Application Programming Interface)

API Example: OpenWeather

  • Get an API key from https://openweathermap.org/api
  • Download the sample code, weather.py, from resources/code
  • Use the urllib.request library to send HTTP requests to the OpenWeather API and retrieve data in JSON format
  • Remember to protect sensitive data (e.g., API keys, API tokens, passwords)
    • Create .env file and store your API key, e.g., OPENWEATHER_API_KEY=...
    • Install python-dotenv in Terminal: python -m pip install python-dotenv
    • import os
      from dotenv import load_dotenv
      
      load_dotenv()
      APIKEY = os.getenv("OPENWEATHER_API_KEY")
      
    • Add .env to the .gitignore to prevent committing it to a public repository.

--- # Exercises - Let's solve The New York Times' [Spelling Bee](https://www.nytimes.com/puzzles/spelling-bee). - CodeWars - [Rainfall](https://www.codewars.com/kata/56a32dd6e4f4748cc3000006/train/python) - How about solving [Wordle](https://www.nytimes.com/games/wordle/index.html)? - [Build a Wordle Clone With Python and Rich](https://realpython.com/python-wordle-clone/)