Input and Output Exercises#

Question-1: Multi-Line Print#
Write a single print statement that produces the following output:
A
B
CD
E
Question-2: Dashed Output#
Use two print() functions, and the strings ‘A’, ‘B’, ‘C’, ‘D’ to generate the following output. A—B—C—D
Question-3: String Mixing#
Use a single print() function and the strings ‘ABCDE’ and ‘FGH’ to generate the following output: FGCDE
Question-4: Harmonic Mean#
Write a program that prompts the user for 3 numbers using 3 input() functions.
Find the harmonic mean of these numbers using the formula: \(\displaystyle H(x, y, z) = \frac{3}{\frac{1}{x}+\frac{1}{y}+\frac{1}{z}}\)
Round the harmonic mean to the nearest hundredth and print it in the following format: \(H(x,y,z)=\) rounded harmonic mean.
Sample Output:
Enter x: 2
Enter y: 3
Enter z: 4
H(2.0,3.0,4.0) = 2.77
Question-5: Number Pattern#
Write a program that prompts the user for a positive integer.
Print that integer.
On the second line, print one more than the given number two times separated by a dash.
On the third line, print two more than the given number three times separated by dashes.
On the fourth line, print three more than the given number four times separated by dashes.
On the fifth line, print four more than the given number five times separated by dashes.
Sample Output:
Please enter an integer: 5
5
6-6
7-7-7
8-8-8-8
9-9-9-9-9
Question-6: Word Repeater#
Write a program that prompts the user for a number and a word using two input() functions.
Print the given word as many times as the given number.
Question-7: Standard Deviation#
Write a program that prompts the user for 3 numbers using 3 input() functions.
Find the mean of these three numbers.
Subtract the mean from each number.
Square each of these differences.
Find the mean of these squares.
Take the square root of this mean and round it to the nearest hundredth.
Remark:This is the standard deviation of the given numbers, which provides information about how the given numbers are spread out around the mean.
Question-8: Countdown#
Write a program that prints the numbers starting from 5 down to 1 and displays ‘START’ after that.
Use the sleep() function from the time module to add a 1-second waiting time between each number for observation.
After printing a number, pause for 1 second, then delete it before printing the next number.
The output of your program should resemble a countdown.
Question-9: Arrow#
Write a program that prints the moving arrow ‘—>’.
Print the arrow, then add a one-second waiting time, then delete it.
Print the arrow again with a single space in front of it, then add a one-second waiting time, then delete it.
Repeat this procedure 10 times by increasing the space one more in each step.
The output of your program should resemble a moving arrow to the right.
Question-10: Standard Scaling#
Write a program that prompts the user for 5 numbers using 5 input() functions.
Find the mean and standard deviation of these three numbers.
Subtract the mean from each number.
Divide each of these differences by the standard deviation.
Remark: This is the standard scaling of a data set. This means that the mean and standard deviation of the data becomes zero and one, respectively.
Question-11: MinMax Scaling#
Write a program that prompts the user for 5 numbers using 5 input() functions.
Find the minimum and maximum of these numbers.
Subtract the minimum from each number, then divide each of these differences by the difference between the maximum and minimum.
Remark: This is the MinMax scaling of a dataset, meaning that the data becomes scaled between 0 and 1.
Question-12: Square#
Print a square using the character *.
Each side consists of number many ‘*’ characters with a space between them.
length = 3
Examples

Question-13: User Info#
Write a Python program that does the following:
Prompts the user to enter their first name.
Prompts the user to enter their last name.
Prompts the user to enter their age.
Prompts the user to enter their country.
Prompts the user to enter their city.
Stores each of these values in separate variables.
Using these variables display the following message:
The student Jackson, Amy is 35 years old and from Madrid, Spain.
Question-14: Line Printer#
Write a program that asks the user to enter an integer and uses a single print() function to display that number of lines, each containing a single < character. Hint: Use the newline character ‘\n’.
Sample Output:
Enter an integer: 4
<
<
<
<
Question-15: Evaluating a Formula#
Write a program that asks the user to enter four numbers: x, y, z, and t.
Then compute the value: \(F = \displaystyle \frac{x^2+5y-z}{\sqrt{t-3}}\)
Finally, print the value of F rounded to two decimal places. (You may assume that t>3.)
Hint:You may use the sqrt function from the math module.
Question-16: Time Conversion#
Write a program that asks the user to enter the total number of seconds.
Then convert the time into hours, minutes, and seconds using the format:
H hours M minutes S seconds
Hint: Use integer division (//) and the modulo operator (%) to compute hours, minutes, and seconds.
Sample Input/Output
Input: Enter total seconds: 7453
Output: 2 hours 4 minutes 13 seconds
Question-17: Price Formatting#
Write a program that asks the user to enter three prices: price_1, price_2, and price_3.
Then display the prices in the following format:
Product-1: $0.00
Product-2: $0.00
Product-3: $0.00
Each price should be rounded to two decimal places.
Hint: Use the built-in format() method to control the number of decimal places.
Sample Input/Output
Input:
Enter price 1: 2.5
Enter price 2: 10
Enter price 3: 4.789
Output:
Enter price 1: 2.50
Enter price 2: 10.00
Enter price 3: 4.79
Question-18: Digit Extraction#
Write a program that asks the user to enter a four-digit integer.
Then print each digit separately in the following format:
Thousands: 0
Hundreds: 0
Tens: 0
Ones: 0
Hint: Use integer division (//) and the modulo operator (%) to extract each digit.
Question-19: Loan Payment#
Write a program that asks the user to enter:
the loan amount: P
the annual interest rate: r (in percent)
the number of months: n
First, compute the total amount using simple interest: \(\displaystyle A = P\left(1+\frac{r}{100}\right)\)
Then compute the monthly payment: \(M = \frac{A}{n}\)
Finally, print both A and M rounded to two decimal places.
Sample Input/Output
Input:
Enter loan amount: 1000
Enter annual rate (%): 5
Enter months: 10
Output:
Total amount: $1050.00
Monthly payment: $105.00