Question

In: Computer Science

In Python, your program will read in a number (no need to prompt the user), and...

In Python, your program will read in a number (no need to prompt the user), and then reads in that number of lines from the terminal. Then the program should print an array of strings formatted in a nice regular box.

So if the user inputs this:

5
Grim visaged war has smooth’d his wrinkled front
And now, instead of mounting barded steeds
To fright the souls of fearful adversaries
He capers nimbly in a lady’s chamber
To the lascivious pleasing of a lute

The program should print:

++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Grim visaged war has smooth’d his wrinkled front +
+ And now, instead of mounting barded steeds       +
+ To fright the souls of fearful adversaries       +
+ He capers nimbly in a lady’s chamber             +
+ To the lascivious pleasing of a lute             +
++++++++++++++++++++++++++++++++++++++++++++++++++++

Hints

  • Read in the number from the user with input(), then read that number of lines. Append each line that you read into a list of lines. If you don't remember how, Google "Python list append".
  • Find the length of the longest line. That determines the width of the box.
  • Then print the header row. The length of the header row depends on the length of the longest line.
  • Print each line.
  • For each line, you have to print some number of spaces at the end, depending on the difference between the length of the longest line and the length of the current line.
  • You have to print some repeated characters.
    • "x" * n  returns "x" repeated n times.
    • For instance, "x" * 4 returns "xxxx".
  • Then print the footer row. The length of the footer row depends on the length of the longest line.

Solutions

Expert Solution

Please find below code and don't forget to give a Like.

Please refer below screenshot for indentation adn output. Explanation is given in comments of the code

Code:

def result(list1):
    length=0
    for i in list1: #for getting the maximum line
        if(len(i)>length):
            length=len(i)
    print("+"*(length+10))#header
    for i in list1:
        space=length-len(i) #except the large line other require spaces
        print("+ ",i," "*space,"+")#giving spaces
    print("+" * (length+10))#footer
n=int(input("Enter number of lines"))#taking input from user
list_of_lines=[]#list of lines
while(n!=0):#to read all lines
    string1=input()
    list_of_lines.append(string1)#appending
    n-=1
result(list_of_lines)#calling function to print result

Output:


Related Solutions

I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Step by step in python please Write a program this will read a file (prompt for...
Step by step in python please Write a program this will read a file (prompt for name) containing a series of numbers (one number per line), where each number represents the radii of different circles. Have your program output a file (prompt for name) containing a table listing: the number of the circle (the order in the file) the radius of the circle the circumference the area of the circle the diameter of the circle Use different functions to calculate...
A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!! (1) Prompt the user to enter a string of...
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!! (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be...
The program will prompt the user to enter a number between [10 .. 20] (inclusive). After...
The program will prompt the user to enter a number between [10 .. 20] (inclusive). After verifying that the number is within the proper range, you will generate that many random numbers and place them into a dynamically allocated array. Your program will then display the contents of the array (with 4 numbers per line). Next, you will sort the values in descending order (from largest to smallest). Lastly, you will display the sorted numbers (again, with 4 per line)....
Write a program that asks the user to enter an unsigned number and read it. Then...
Write a program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6. COMMENT COMPLETE CODE PLEASE
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names     &
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close Do you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT