Input and Output Exercises#
Warm Up Question#
Write a program that prompts the user for a number and displays the double of that number.
Warm Up Question#
Write a program that prompts the user for a name of a country and displays the number of chareacters in that country’s name.
Warm Up Question#
Use the variables \(x,y,z\) and a single print() function to display 3 <= 7 <=9.
Hint: Change the value of the sep parameter.
x, y, z = 3, 7, 9
Warm Up Question#
Modify only the end parameter of the given print statements to produce the output A,B??C===D.
print('A')
print('B')
print('C')
print('D')
Question-1#
Write a single print statement that produces the following output:
A
B
CD
E
Question-2#
Use two print() functions, and the strings ‘A’, ‘B’, ‘C’, ‘D’ to generate the following output. A—B—C—D
Question-3#
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#
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#
Write a program that prompts the user for a number and a word using three 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
Business Applications#
Question: Production Data Analysis#
Write a program that prompts the user for 5 numbers using five input() functions, each representing the annual production for the last five years.
Calculate the total, average, and highest production using built-in functions.
Question: Revenue#
Revenue (R) is the product of the number (n) of items sold and the price (p) of the item.
\(R = p\cdot n\)
Write a program that ask the user for the price of an item and calculate the revenue using the following formula for the number of item sold.
\( n = -1.5p+30 \)
Question: Simple Interest#
Simple Interest Formulas: \(\displaystyle I=P r t\) and \(A = P + I\) where
A is the final amount
I is the interest amount
P is the initial (principal) amount
r is the annual interest rate (decimal form)
t is the number of years
Write a program that asks the user for the principal, annual interest rate, and number of years using three input() functions one by one, and then returns the interest and final amount.
Sample Output:
Please enter the principal: 1500
Please enter the annual interest rate: 0.025
Please enter the year: 20
I = 750.0
A = 2250.0