In: Computer Science
do not use these: {,},{}
Python 3
APPL
276.75
15
A file named asset.txt consists of only the three lines above. They describe the unit cost and quantity of shares of Apple stock. Write statement that open the file, read the data and use it to print this exact output:
My APPL stock is worth $4,151.25.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required. Also note that since you mentioned to not use '{}', I have formatted the currency using locale module. Otherwise it was very easy to format the currency using '${:,.2f}'
#code
# these two lines are needed for currency formatting (since you mentioned that # you dont want to use {}, otherwise it would have been very simple) import locale locale.setlocale(locale.LC_ALL, '') # opening asset.txt in read mode file = open('asset.txt', 'r') # reading first line as stock name name = file.readline().strip() # next line as unit cost, float unit_cost = float(file.readline()) # next line as quantity, integer quantity = int(file.readline()) # displaying the result in proper format print("My " + name + " is worth " + str(locale.currency(unit_cost * quantity, grouping=True)) + ".") # closing file file.close()