Week 6 Lab: while loops

Part 1: Investigating the Collatz Sequence

Step 1: Make the lab6 project

Open up Eclipse. Make sure you are using your correct workspace: it should be S:\workspace. If it isn't, use File --> Switch Workspace ... to fix that.

Create a new Folder under CS104, called lab6. In this folder, create a new file called collatz.py.

Add this to the file:


# constant definitions here


# function definitions here



# --------- main program -------------

Step 2: Do the work

NOTE NOTE NOTE: follow the directions ssssslllllooooowwwwwlllllyyyyy. Follow every instruction carefully. This will help you finish the lab more quickly.

Open up the online textbook and scroll down to the "More about Iteration" chapter. In that chapter, click on the "The 3n + 1 Sequence" section. In that section, click on the "Experimenting with the 3n+1 Sequence" online lab. Do that lab, skipping step 3. Do your work in the collatz.py file.

---->> When you are done with the code, add a line of code at the end to print your maxSoFar value. You could make it nice by printing out something like

Longest collatz sequence found has 33 steps.

(Where 33 is replaced with the actual value from maxSoFar, whatever that is.)

Step 3: Submit your code

Add your standard comment to the top of the file, describing who you are, what the date is, and what the assignment is.

To submit your final version of collatz.py, you’ll need to use Windows Explorer.


This lab is worth 5 points. 
3 points: program that runs correctly. 
2 points: code is clean and neat and uses good comments and variable names. I.e., the code is hospitable. You should have good use of spacing around operators. I don't want to see this kind of stuff anymore: if x==y: It should be if x == y:


Part 2: Interactive Control


In this lab, you will ...

For this assignment, your main program will take commands from the user, and then tell the robot to do what the user commands.

Step 1: Make the lab6 project

AGAIN: Follow the directions ssssslllllooooowwwwwlllllyyyyy. Follow every instruction carefully. E.g., below I tell you to create a function that ***returns*** a value. Your function needs to return that value, *not* print it. Going slowly and carefully will help you finish the lab more quickly.

(If you didn't do this above...) Open up Eclipse and create a new folder under CS104, called lab6.

In this folder, create a new file called lab6.py.

Step 1.5: Make move.py file

In that same folder, create another new file called move.py. At the top of this file, add a comment that says that this file contains functions to enhance scribbler movement.

Go to last week's lab and copy-n-paste the code for the following functions into move.py:

travelForw(), travelBack(), turnLeftDegrees(), turnRightDegrees(), turnDegrees().

Also, copy the the constant definitions and the import stuff at the top.

In other words, move.py should contain all the code from lab5.py except drawNgon() and the main code.

Step 2: Add the main loop

Step 3: Create a function to get user input

Above your init() call, in the functions area, define a new function called getCommand().

getCommand() description
Function Name getCommand
Input parameters none
Return value single character string representing the command the user entered
Description

Print out a nice message to the user, like this:

Commands:
f or F for forward,
b or B for backward,
l or L for left turn of 90 degrees,
r or R for a right turn of 90 degrees,
t or T for a turn, and
q or Q for quit.  

Enter a command:

The code then gets a single-character string command from the user, and returns that command. The function does NOT have to check if the user enters good input. We assume the user does.

Test your new function by putting code in the while loop (in the main part of the file) to call your new function, storing the result in a variable, and print out that result. Give the variable a good, descriptive name. Put this code where pass is currently. Run your code and make sure you are printing out the nice message for the user, and getting the result back. Note that your code will run forever as there is currently no way for the while loop to exit. You'll have to stop the run by clicking the red box in Eclipse.

Step 4: Add code to exit the loop

We are going to do incremental development here, adding just a few lines of code to the program before testing it. First, let's handle the q/Q command. When the user enters q or Q, the while loop in the main code should exit. This is done by setting the variable done to True.

Add code to your while loop body that tests if the command gotten from the user is "q" or "Q". If it is, then set done to True. If it isn't "q" or "Q" just print out a message saying "Command not implemented yet." NOTE: the following is legal python syntax, but does not do what you want it to:

if command == "q" or "Q": # probably not what you want because the bool expr is wrong...

Test your code to make sure the loop (and program) exits when you type q or Q (and only q or Q).

Step 5: Handle other commands

Uncomment out your init call. You'll need a scribbler from now on.

Enhance your if-elif-else conditional so that you now handle the case where the user enters f or F. (For all of these movement commands, call the functions you have in move.py that are being imported into lab6.py.) Define a constant in lab6.py that stores how far your scribbler should go when the user enters f/F or b/B. I had my scribbler go 10 centimeters. Test your code for going forward.

Now, add code to handle b or B. Test your code. Then, add code for left and right 90 degree turns. Test your code.

Step 6: Handle the turn command

For this command (t or T), you will need to ask the user for the degrees to turn. Make a function for this (in lab6.py):

getCommand() description
Function Name getTurnAmount
Input parameters none
Return value a float, which is the amount the scribbler should turn
Description

Use input() to print out a nice message to the user, like this:

Enter amount to turn:

Return the value the user enters, as a float.

Call this function from in your if-elif-elif-else inside your while loop, and call turnDegrees() with the result.

Step 7: Test your code


Step 8: Submit your code

Add your standard comment to the top of the file, describing who you are, what the date is, and what the assignment is.

To submit your final version of lab6.py and move.py, you’ll need to use Windows Explorer.


This lab is worth 10 points. 
7 points: program that runs correctly. 
1 points: correct comments at the top of the file.
2 points: code is clean and neat and uses good comments and variable names. I.e., the code is hospitable. You should have good use of spacing around operators. I don't want to see this kind of stuff anymore: if x==y: It should be if x == y: