Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(PYTHON) WARM-UP: Write a COUNTING LOOP that prints out the numbers 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, with each number

(PYTHON)

WARM-UP: Write a COUNTING LOOP that prints out the numbers 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, with each number on a separate line. You can do this using a 'while' loop or a 'for' loop, but do this without typing all these numbers directly into your program. Being able to write a counting loop is a prerequisite for doing today's lab exercise.

For this lab exercise, you will manipulate a string in several ways. You should create and upload one program that does all these things. Develop your program progressively, adding one operation at a time and testing as you go. Remember that your program needs to work with any phrase the user enters, not just the example shown.

  1. Start by writing code that asks the user to enter a short phrase. (Examples below assume the user enters \"happy birthday\", but your program should work with any phrase entered by the user.)
  2. Use a loopto print the user's phrase, one character per line. Test this to make sure it works.
  3. Modify your program so that it also prints the phrase all on one line in reverse order. Our test phrase above would be printed like this: yadhtrib yppah Do this by using asliceto get the reversed string, and print that.
  4. Print the original phrase in reverse order, all on one line, again, but this time do sowithoutusing a slice or the string object's 'reverse()' method (or the built-in function named \"reversed()\"). Instead, use aloopto print one character at a time, all on the same line, but in reverse order. (Hint: try using a COUNTING loop that counts through all the legal index values in reverse order. If you are using a slice, you are missing the point.) Remember that you can use the 'end' parameter when you call the 'print' function to keep each the characters all on the same line. If you prefer, you could use a string accumulator -- see Note 1 below.
  5. Modify your program so that it also prints the phrase one word per line, like this: happy birthday Use an if statement inside a loop. Test to see if the character is a space (just a string comparison to ' '), and when it is, start printing on a new line.
  6. Optional: Add code to your program that asks the user to enter the name of a file. Then, print the contents of the file onewordper line, but do not print any vowels ('aeiouAEIOU'). Assume that the file may have more than one word on each line, but your output should have just one word per line.
  7. Optional: Working once again with the phrase the user entered in Part 1, add code that replaces any word beginning with the letter 'b' with the word \"bee\". To do complete word replacement like this, you will need to use a string accumulator to hold the word, as described in Note 1 below. You know a word is completed if you encounter a space OR the end of the message. (Tip: add one space at the end of the user's phrase to ensure that every word is followed by a space.) Here are a few examples: -- \"happy birthday\" would be printed out as \"happy bee\" -- \"a bear likes honey\" would be printed out as \"a bee likes honey\" -- \"bake better bread\" would be printed out as \"bee bee bee\"

Write all your code in one .py file and upload the file before the deadline to get lab credit.

Note:

1. Here's how to use a string variable as an accumulator to complete Part 6. First, be sure to inititalze the accumulator before the loop to an empty string (two quote marks with nothing between them, not even a space). Then, instead of printing each character one at a time, add each character to the accumulator. When you reach the end of a word, print out the word (or a substitute), and then reset the accumulator to an empty string. (In other situations, you may choose to add a newline character (' ') to the accumulator to go to a new line, and then just print out the string accumulator's value one time after the loop is finished.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

=+b. Calculate SSResid and SSTo.

Answered: 1 week ago