OIM3640 - Problem Solving and Software Design

2025 Spring

Session 09 (2/20)

contain

Today's Agenda

  • Welcome/News/Announcements
  • Reivew and Exercises
  • Quiz 1
  • Debugging
  • Pair programming

Welcome/News/Announcements

  • Assignment 1 is posted on Canvas
    • Please accept it now.
    • Read instructions, including code grading rubric
    • This assignment is a separate repo. Do not copy code to your oim3640 repo.
  • 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?

What we have learned so far...

  • Variables, expressions, statements
  • Types: int, float, string, boolean, Nonetype, other data structures
  • Functions
  • turtle module
  • Pseudo-code
  • Conditional Statements
  • Iterations
    • for
    • while

Quick Quiz

  1. What keyword can be used to jump out of the current loop?
    • break (What about continue?)
  2. How do you print '😀' n times in the same row using for loop?
  3. What will be the last line of output?
    n = 6
    while n > 0:
        print(n)
        n = n - 2
    
    1. What if line 2 is changed to while n >= 0:?
    2. What about while n != 0:?
  4.  n = 5
     while n != 0:
         print(n)
         n = n - 2
    

Quiz 1

Exercise Recommendations

  • Issues on GitHub - Reply and/or Reply (with evidence) and Close
  • Functionality
    • Use pseudocode if your code/algorithm is hard to understand.
    • Write your own test code.
  • Documentation
    • Add docstrings for all the functions that you create.
    • Add proper comments.
  • Styles
    • Use lowercase and underscore for folder and file names.
    • Variable and function names are sensibly chosen.
    • Create separate files for different questions.

Suggestions

Iterations can be challenging to understand and write/use, but there are effective strategies to conquer them:

  1. Practice/code a lot
  2. Think before coding
  3. Write pseudo-code before writing Python code
  4. Observe and find patterns

More Practice

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

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.

Game: Guess the Number

  • We will create the classic "Guess the Number" game.
  • Game rule:
    • The computer will think of a random number from 1 to 50, and ask you to guess it.
    • The computer will tell you if each guess is too high or too low.
    • You win if you can guess the number within 6 tries.

What the program looks like

>> Hello! What is your name?
John
>> Well, John, I am thinking of a number between 1 and 50.
>> Take a guess.
10
>> Your guess is too high.
>> Take a guess.
2
>> Your guess is too low.
>> Take a guess.
4
>> Good job, John! You guessed my number in 3 guesses!

Pair Programming

  • A collaborative practice where two individuals work together on a single programming task.
  • Driver:
    • Typing code
    • Sharing screen
  • Navigator:
    • Paying close attention to the code
    • Providing guidance and suggestions
  • Ideally, the Driver and Navigator roles should switch periodically.

Benefits of Pair Programming

  1. Constant feedback
  2. Reduced frustration
  3. Increased focus
  4. Social interaction
  5. Accountability
  6. Collaborative skills
  7. Real-world experience
  8. Mentorship

source: How Remote Pair Programming Works & Why it Can Change Your Life

Pair Programming

  1. Close your computer.
    • Start with pseudo-code on paper (10 mins)
    • Write Python code (5 mins)
      • Write Python code on the reverse side of the paper.
      • Consider how to implement random numbers, loops, and test cases.
  2. Open your computer
    • No Google/ChatGPT allowed!
    • Create session09/guess.py. Copy pseudo-code and Python code into it.
    • (Optional) Install VS Code extension "Live Share" for real-time collaboration
  3. Add more features. Be creative and enjoy!
    • See next slide for extension ideas

Extension Ideas

  1. Different difficulty Levels: easy, medium, hard mode
  2. Customizable number range
  3. Allow multiple rounds:
    1. Give the player the option to play multiple rounds of the game in a row.
    2. After each round, ask the player if they want to play again or quit.
    3. Keep track of the # of rounds and display the player's overall performance
  4. Multiplayer mode:
    1. Allow two players to take turns.
    2. Keep track of each player's score and declare the winner in each round.
    3. Or a mode where one player thinks of a number, and the other tries to guess

--- # 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](https://en.wikipedia.org/wiki/Python_(genus))* 2) Guido van Rossum's favorite *[comedy TV show](https://www.youtube.com/watch?v=JrdEMERq8MA)* 3) Guido van Rossum's favorite *[artwork](https://www.metmuseum.org/art/collection/search/398703)* 4) Guido van Rossum's favorite *[mythical creature](https://en.wikipedia.org/wiki/Python_%28mythology%29)* 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? * Consider using the [`decimal` library](https://docs.python.org/3/library/decimal.html) if precision matters to your program. * We could use `print(f'{result:.1f}')` to hide such floating point errors. * Read [more discussions on stackoverflow](https://stackoverflow.com/questions/19473770/how-to-avoid-floating-point-errors)

--- <style scoped> h1 { font-size: 1.5em; } </style> # Debugging 101 - What to Do When Coding Gets Tough 1) Do you understand **concepts** and **syntax** being used? 2) Have you attempted **example/demo code**? 3) Have you read **supplemental** materials? 4) Have you tried 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) Remember to give credits for use of code in comments.