Question

In: Computer Science

Python Write a for loop with a range function and format output as currency Use an...

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

Solutions

Expert Solution

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:


Related Solutions

must use python Write a nested for loop that displays the following output using the same...
must use python Write a nested for loop that displays the following output using the same integer from part a:                                                     1                                                 1   2   1                                             1   2   4   2   1                                         1   2   4   8   4   2   1                                     1   2   4   8 16   8   4   2   1                                 1   2   4   8 16 32 16   8   4   2   1                             1   2   4   8 16 32 64 32 16   8   4   2   1                         1   2   4  ...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name it addToDictionary(s,r) that take a string and add it to a dictionary if the string exist increment its frequenc 2) function named freq(s,r) that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.
2) create a python program that uses a for loop and range to print out the...
2) create a python program that uses a for loop and range to print out the values 10 8 6 4 2 3) Create a python program that yses a for loop to print out ["bob","al","bert"]
#Python program using loop instead of numpy or pandas. Write a function to enter n student...
#Python program using loop instead of numpy or pandas. Write a function to enter n student scores from 0 to 100 to represent student score, Count how many A (100 - 90), B(89-80), C(79-70), D(69-60), F(<60) We will use numpy array OR pandas later for this problem, for now use loop Invoke the function with 10 student scores, show the result as follow: Test Enter 10 scores Enter a student score: 99 Enter a student score: 88 Enter a student...
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop invariant for loop 1: “the contents ofnewlistare in sorted order,newlistcontainsaelements oflistAandbelements oflistB” def mergeSortedLists(listA, listB):  newlist = [ ]  a = 0  b = 0  # loop 1  while a < len(listA) and b < len(listB):   if listA[a] < listB[b]:    newlist.append(listA[a])    a +=1   else:    newlist.append(listB[b])    b +=1  if a < len(listA):   newlist.extend(listA[a:])  if b < len(listB):   newlist.extend(listB[b:])  return newlist (a) Write down (in regular...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end Use the readInput() method for the following input data Oranges: 10 for...
Java Input, Output and for-loop function. Practice01) Write-an average SCORE application called TestScores01 This project reads...
Java Input, Output and for-loop function. Practice01) Write-an average SCORE application called TestScores01 This project reads in a list of integers as SCOREs, one per line, until a sentinel value of -1. After user type in -1, the application should print out how many SCOREs are typed in, what is the max SCORE, the 2nd max SCORE, and the min SCORE, the average SCORE after removing the max and min SCORE. When SCORE >= 90, the school will give this...
Solve by using python function. use loop Input; Crossing Times = { 10,15,20,25} 1.Firstly person '1'...
Solve by using python function. use loop Input; Crossing Times = { 10,15,20,25} 1.Firstly person '1' and '2' cross the path with total time about 15 min(maximum of 10, 15) 2.Now the person '1' will come back with total time of '10' minutes. 3. Now, person 1 and person 3 cross the path with total of about 20 minutes .(maximum of 10,20) 4. Now the person 1 will come back in 10 mintues 5. Lastly person 1 and person 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT