#Future Value #It is difficult to make a budget that spans several years, #because prices are not stable. If your company needs 200 pencils #per year, you cannot simply use this year's price as the cost of #pencils two years from now. Because of inflation the cost is likely #to be higher than it is today. # #Write a program to gauge the expected cost of an item in a specified #number of years. The program asks for the cost of the item, #the number of years from now that the item will be purchased, # and the rate of inflation. The program then outputs the estimated #cost of the item after the specified period. #Have the user enter the inflation rate as a percentage, like 5.6 (%). #Your program should then convert the percent to a fraction, like 0.056, #and should use a loop to estimate the price adjusted for inflation. from graphics import * def main(): print("This program calculates the expected price of an item") price = eval(input("Please, input the today's price of an item: ")) years = eval(input("Please, input the number of years from now that the item will be purchased: ")) inflation = eval(input("Please, input the inflation rate: ")) starting_price = price # preserving the starting price # calculating the price in ... years inflation = inflation/100.0 for i in range(years): price=price+price*inflation #print("price = ", price) phrase = "The starting price is $"+str(starting_price)+",\n In "+\ str(years)+" years the price will be $"+str(round(price,2)) print(phrase) print("\n \t Now let's do it in graphics!") window = GraphWin('Predicting item\'s price.',800,600) window.setBackground("white") #Displaying the result in the graphics window PriceText = Text(Point(400,450),phrase) PriceText.draw(window) # let's draw the scale consisting of five marks(you may skip this part till drawing bars): print("calculations:") diff = price-starting_price # calculating the difference in starting price and final price scale=diff/4.# dividing by four, since we'll have 5 marks; print("difference = ",diff) print("scale = ",scale) l1='$'+str(round(starting_price,2)) # converting numerical mark to string print("label 1=",l1) label2=starting_price+scale l2='$'+str(round(label2,2)) # converting numerical mark to string, rounding off to two decimal places print("label 2=",l2) label3=starting_price+2*scale l3='$'+str(round(label3,2)) # converting numerical mark to string, rounding off to two decimal places print("label 3=",l3) label4=starting_price+3*scale l4='$'+str(round(label4,2)) # converting numerical mark to string, rounding off to two decimal places print("label 4=",l4) label5=starting_price+4*scale l5='$'+str(round(label5,2)) # converting numerical mark to string, rounding off to two decimal places print("label 5",l5) # displaying labels and lines Text(Point(40,30), l5).draw(window) Line(Point(70,30),Point(580,30)).draw(window) Text(Point(40,80), l4).draw(window) Line(Point(70,80),Point(580,80)).draw(window) Text(Point(40,130),l3).draw(window) Line(Point(70,130),Point(580,130)).draw(window) Text(Point(40,180),l2).draw(window) Line(Point(70,180),Point(580,180)).draw(window) Text(Point(40,230),l1).draw(window) Line(Point(70,230),Point(580,230)).draw(window) # draw bars for successive years price = starting_price # re-assigning price to initial one # need variable pixels to find the bar height pixels=(50.)/scale # calculating how many pixels should be used to 1$ print("pixels=" ,pixels) bar_width = 40 for i in range(years): price = price + price*inflation # calculating price in a year print("price=", price) # draw bar for this value x = i * bar_width + 180 # x-coordinate of the bottom left #corner of the bar height=(price-starting_price)*pixels # calculating the height of the # bar so that it corresponds to the difference between # previous year's price and current year's price print("height=",height) bar=Rectangle(Point(x,230),Point(x+bar_width,230-height)) bar.setFill('green') bar.setWidth(1) bar.draw(window) # putting label under the bar with the corresponind price T = Text(Point(x+bar_width/2,260),str(round(price,1))) T.draw(window) T = Text(Point(400,500),"Click on EXIt button to terminate the program.") T.draw(window) # drawing the text # Building exit button - rectangle + text exitButton = Rectangle(Point(680,520),Point(770,570)) exitButton.setFill("green") exitText = Text(Point(725,545),"EXIT") exitText.setSize(22) exitButton.draw(window) exitText.draw(window) while True: x = window.getMouse() # waiting for a mouse click if 680<=x.getX()<=770 and 520 <= x.getY() <= 570: break window.close() main()