In: Computer Science
Python
Write a for loop with a range function and format output as
currency
Use an input statement to ask the user for # of iterations using
the prompt: #? [space after the ?] & assign to a variable
Convert the variable to an integer
Use a for loop and a range function to display all whole numbers
from 1 to the user entered number
Display the value of the item variable on screen in each iteration
in the following format:
The price is [item variable in currency format]!
(hint: use the format specifier $%.2f)
After the for loop ends, display the user entered number of
iterations
This program was done using 'Spyder 4.0.1' (Python version: 3.7.6)
PYTHON CODE:
import babel.numbers # its is used to display numbers in currency format
# input statement to ask the user for # of iterations using the prompt
# the input value is assigned to the variable number_input
# the variable is coverted to an integer
number_input = int(input("Input the number of iterations: "))
#for loop with range function
for item in range(1, number_input + 1):
print(item); # display whole numbers from 1 to user entered number
# formatting output as currency(USD)
# price variable has the integer variable rounded off to two decimal places using format specifier and it is in currency format (USD)
price = babel.numbers.format_currency("{:.2f}".format(item), "USD" );
print("The value of of the item variable is " + price);
# displays the user entered number of iterations after the for loop ends
print("\nThe value entered by the user is " + str(number_input));
OUTPUT: