This is a lab assignment and so you will not be submitting it. However, the concepts and practice will help you on both the homework and quizzes so I encourage you to make a serious effort on it during class and consider finishing it outside of class.
I recommend making a folder for today’s lab in COURSES as you usually do.
Remember you can refer to the textbook readings if you forget the syntax for something.
For each snippet, what would it print out to the screen? If you think there will be an error, indicate the line with the error and explain why. After you’ve made a prediction, test out the code.
Create a new file and write short program to compute the factorial of a positive integer given by the user. The factorial of a positive integer is the product of all of the numbers from 1 up to that integer. For example, 4 factorial (written “4!”) is equal to 1 * 2 * 3 * 4 = 24.
Let’s write a program that receives a phrase from the user, reverses the order of each word, and then prints the result back out to the screen. The order of the words relative to one another should remain the same, but each word’s letters should be reversed, as in the following example:
Input a phrase: stay warm out there yats mraw tuo ereht
As with any program, we should start out by laying out our algorithm, which at a high level might look something like this:
- Get phrase from user.
- Split phrase into words
- For each word in the phrase
- Reverse the letters
- Combine the letters back together
- Print the reversed word
Write a program called
reverse.py to carry out the above algorithm.
Here is a link to the sequences reading if you need to refresh on working with strings.
If you finish all of the above, try out some of the end of chapter exercises for more practice.