Python Loops Practice Question
Write a Python application that generates a table displaying the results of multiplying N by 20, 200, and 2000. The table should include the values for N = 10, 20, 30, 40, and 50. Each row of the table should show the value of N, followed by the corresponding results for 20*N, 200*N, and 2000*N. Ensure that the table is formatted neatly with appropriate spacing and alignment.
This is a Python loops practice question designed for students to practice basic calculations and output formatting. To complete the chall, you can use a loop to iterate through the values of N and calculate the results for each multiplication. Consider utilizing formatted strings (f-strings) to ensure the table is displayed in a clear and organized manner.
Expected output
N | 20*N | 200*N | 2000*N
---|--------|---------|---------
10 | 200 | 2000 | 20000
20 | 400 | 4000 | 40000
30 | 600 | 6000 | 60000
40 | 800 | 8000 | 80000
50 | 1000 | 10000 | 100000
Solution
def print_multiplication_table():
print(" N | 20*N | 200*N | 2000*N ")
print("---|--------|---------|---------")
for n in [10, 20, 30, 40, 50]:
result_20n = 20 * n
result_200n = 200 * n
result_2000n = 2000 * n
print(f"{n:2} | {result_20n:6} | {result_200n:7} | {result_2000n:7}")
print_multiplication_table()
- TheÂ
"print_multiplication_table"Â function is defined to calculate and print the results for the given values of N. - A table header is printed first to show the column names.
- A loop is then used to iterate through the values of N (10, 20, 30, 40, 50).
- For each value of N, the results of 20*N, 200*N, and 2000*N are calculated and printed in a formatted manner using f-strings.