0% found this document useful (0 votes)
14 views

Lab 03

This document provides instructions for a lab exercise on string manipulation and debugging in Python. Students will practice debugging a string program, then write a program to convert words to Pig Latin by modifying the string based on vowel/consonant patterns.

Uploaded by

ksiddhesh647
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lab 03

This document provides instructions for a lab exercise on string manipulation and debugging in Python. Students will practice debugging a string program, then write a program to convert words to Pig Latin by modifying the string based on vowel/consonant patterns.

Uploaded by

ksiddhesh647
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab Exercise #3

Assignment Overview

This lab exercise provides practice with strings and methods in Python.

 Partnering on this Exercise


• Online students: you can do it on your own or with a partner. The best place to find a partner is Piazza.
• For in-person section students, you will work with a partner on this exercise during your lab session. Share your
computer screen with your group. To allow for all partners to practice, occasionally switch the person who is
typing. The person typing should take ownership of what they write while the people without the keyboard
double-check their work. If there is disagreement or uncertainty on how to proceed, talk to each other about what
you are doing and why that is the best choice.

PART A: DEBUGGING EXERCISE


This part of the lab exercise provides practice with debugging, meaning the process of identifying and removing
errors from your code. The art of debugging is an integral part of programming. The following exercise is
designed to help you understand string manipulation using a debugger.

As a reminder as to how to best make use of the debugger in PyCharm:


• Set breakpoints on lines by clicking on the line numbers of the PyCharm. Try it and you will see a red
circle; -click again to make it disappear.
• Run the program in debug mode by pressing the bug icon (CTRL + F9) button. Execution “freezes”
when a breakpoint is encountered. You can then progress the program one statement at a time by
pressing the Step Over (F8) button.
• The next statement to be executed will be highlighted. This console output will change every time you
progress your program further (using F8). You can use this along with F8 to understand which lines of
your code are executed during conditional statements.
• Notice the values stored in variables in the Threads & Variables window (the tab next to the console).

Download the Lab03 folder and unzip it to your computer. As before, we open the new project Lab03. Open
the file debug3.py in PyCharm by double-clicking.

Set a breakpoint at the first executable line of the program (string1) and start the debugger by pressing the
“bug” button (leftmost green button). Answer the following questions:

1) When line 14 is highlighted, which variables are held in memory and what are their values?
ANSWER: Variables  values

string1  noitalupinam gnirtS


string2  NOITALUPINAM GNIRTS
string3  RTS
string4  <empty>

2) List all values of i and ch observed until the completion of the for loop.
ANSWER:
i ch
0 R
1 T
2 S

Note: Please do not use print() statements during the debug exercise.
Additional Resources
https://www.jetbrains.com/help/pycharm/debugging-your-first-python-application.html#where-is-the-problem

PART B: PROGRAMMING WITH STRINGS

Develop a Python program which will convert English words into their Pig Latin form, as described below.

 You will work with partners on this exercise during your lab session. On-line students: you can do
it on your own or with a partner. Best place to find a partner is Piazza.

The program will repeatedly prompt the user to enter a word. First convert the word to lower case. The word
will be converted to Pig Latin using the following rules:

a) If the word begins with a vowel, append “way” to the end of the word.
b) If the word begins with a consonant, remove all consonants from the beginning of the word and
append them to the end of the word. Then, append “ay” to the end of the word.

For example:

"dog" becomes "ogday"


"scratch" becomes "atchscray"
"is" becomes "isway"
"apple" becomes "appleway"
"Hello" becomes "ellohay"
"a" becomes "away"

The program will halt when the user enters “quit” (any combination of lower and upper case letters, such as
“QUIT”, “Quit” or “qUIt”).

Suggestions:

a) Use .lower() to change the word to lower case.


b) How do you find the position of the first vowel? I like using enumerate(word) as in
for i,ch in enumerate(word)
where ch is each character in the word and i is the character’s index (position).
c) Use slicing to isolate the first letter of each word.
d) Use slicing and concatenation to form the equivalent Pig Latin words.
e) Use the in operator and the string "aeiou" to test for vowels.
Good practice: define a constant VOWELS = "aeiou"

 Demonstrate your completed program to your TA. On-line (Section 730) students should submit
the completed program (named “lab03.py”) for grading via the Codio system. You can also copy and
paste your code into the coding window.

You might also like