OIM3640 - Problem Solving and Software Design

2025 Spring

Session 10 (2/25)

contain

Today's Agenda

  • Welcome/News/Announcements
  • Class Review
    • Revisit Quiz 1
  • Exercise feedback
  • Lecture:

Welcome/News/Announcements

  • Assignment 1: Due Friday, 2/28
    • You should have accepted it.
    • Read instructions, including code grading rubric
    • This assignment is a separate repo. Do not copy code to your oim3640 repo.
  • Quiz 1 grades are posted.
  • Communications
    • Meet with me in person during office hours at least once this semester.
    • Email - specify course # in subject title, e.g., "OIM3640: GitHub settings"
    • Use Slack/GitHub when asking code-related questions
  • Questions?

Quick Quiz - Python Trivia Facts

  1. Was Python created as a hobby project?
  2. Where does the name "Python" for the programming language come from?
    1. Guido van Rossum's favorite pet animal
    2. Guido van Rossum's favorite comedy TV show
    3. Guido van Rossum's favorite artwork
    4. Guido van Rossum's favorite mythical creature
  3. Is Python open source?

One More Python Programming Fun Fact

  • Try 0.1 + 0.2 in Python interactive shell
    • Do not worry! It is NORMAL!
    • This happens in all programming languages that use floating point decimals, not just in Python.
    • Because computers represent decimal numbers in binary (Base-2) format.
    • Check out https://0.30000000000000004.com
  • How to avoid?

What we have learned so far...

  • Variables, Expressions, Statements
  • Types: int, float, string, boolean, Nonetype, other data structures
  • Functions
  • Conditional Statements
    • if...elif...else
    • recursion
  • Iterations
    • for
    • while
  • Pseudo-code

Shoud I use for or while?

  • Use a for loop
    • when the number of iterations is known in advance.
    • when iterating over a specific range of integers.
    • when looping through elements in a collection or sequence (e.g., string, list, tuple, dictionary, set)
  • Use a while loop
    • when creating an infinite loop.
    • when the number of iterations is unknown and should continue until:
      • a specific condition becomes False.
      • or a break statement is encountered.

Revisit Quiz 1

Coding Practice

Create function(s) that execute a simulation 10 times. Within each simulation, roll 100 dice (🎲🎲🎲🎲🎲🎲🎲🎲🎲🎲...) and display the resulting sum.

Suggestions

Iterations can be challenging to understand and write, but these strategies can help:

  1. Observe and find patterns in the problem
  2. Think before coding to plan your approach
  3. Write pseudo-code to organize your thoughts
  4. Practice a lot to enhance your understanding

Practice

  1. ⭐ Codingbat (OIM3640/codingbat)
  2. Python Challange
  3. More learning resources

Issues from Exercises

  1. Need to wrap code into function guess()
  2. if __name__ == "__main__":
  3. Use f-string

After (semi-)finishing your exercises

  • Review the solution code in OIM3640/oim3640 repo.
    • Don’t assume my code is the only or best solution.
    • Reflect on it, think critically, and ask questions.
  • Don't directly delete your current code.
    • Instead, comment it out and
    • make changes based on it.
  • Remember More on Learning to Code?

Debugging 101 - What to Do When Coding Gets Tough

  1. Do you understand the concepts and syntax being used?
  2. Have you tried running example/demo code?
  3. Have you reviewed supplemental materials?
  4. Have you applied any debugging approaches?
  5. Have you had enough rest? Are you feeling tired or stressed?
  6. Have you asked for help?
    1. Reach out to your peers, professors, or online communities.
    2. Use AI coding assistants or tools for suggestions.
    3. Don’t forget to give credit for external code in comments.

How to Debug

  • Format your code first: Alt (βŒ₯ option if macOS) + Shift + F in VS Code.
  • print()
  • PythonTutor.com
  • Debugging in VS Code
    1. Set breakpoints to pause execution at specific lines.
    2. Run debugger: Press F5 to Start Debugging
      1. "Continue" ( F5 ): Keep running until next breakpoint or stopping point.
      2. "Step Over" ( F10 ): Go through code line by line, skip over functions.
      3. "Step Into" ( F11 ): Step into functions and trace each line inside them.
    3. Inspect Variables: Use "VARIABLES" panel or "WATCH" panel.

Session 10

full

--- # Debugging Practice ```python import random def guess(): random = random.randint(1, 50) for i in range(6): user_num = int(input("Take a guess: ")) if user_num > random: print("Your guess is too high.") elif user_num < random: print("Your guess is too low.") else: if user_num == random: print(f'Good job! You guessed my number in {i+1} times.') else: print("you lose!") ```