![]() |
Python Formula - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python Formula (/thread-17578.html) |
Python Formula - prophet11 - Apr-16-2019 Dear Collaborates, Firs of all thank you in advance for replying to my inquiry: Problem: I have written a python program that works fine, but I would like to reduce the amount of code and use a looping mechanism. I'm new to python so please excuse my inexperience. from tabulate import tabulate mydata = [("GOOGL", 125, 772.88, 941.53), ("MSFT", 85, 56.60, 73.04), ("RDS-A", 400, 49.58, 55.74), ("AIG", 235, 54.21, 65.27), ("FB", 150, 124.31, 172.45)] headers = ["Stock Symbol", "No Shares", "Purchase Price", "Current Value"] headers1 = ["Stock Symbol", "No Shares", "Earnings/Loss"] x = (mydata[0][2]) y = (mydata[0][3]) s =(mydata[0][1]) a = (y-x) * s x1 = (mydata[1][2]) y1 = (mydata[1][3]) s1 =(mydata[1][1]) b = (y1-x1) * s1 x2 = (mydata[2][2]) y2 = (mydata[2][3]) s2 =(mydata[2][1]) c = (y2-x2) * s2 x3 = (mydata[3][2]) y3 = (mydata[3][3]) s3 =(mydata[3][1]) d = (y3-x3) * s3 x4 = (mydata[4][2]) y4 = (mydata[4][3]) s4 =(mydata[4][1]) e = (y4-x4) * s4 results = [(mydata[0][0], mydata[0][1],a), (mydata[1][0], mydata[1][1],b), (mydata[2][0], mydata[2][1],c), (mydata[3][0], mydata[3][1],d), (mydata[4][0], mydata[4][1],e)] print("\nStock Ownership") print("..............................................................") print(tabulate(mydata, headers=headers, tablefmt="grid")) print("\nStock Earnings/Losses ") print("..............................................................") print(tabulate(results, headers=headers1, tablefmt="grid")) RE: Python Formula - Larz60+ - Apr-17-2019 mydata = [("GOOGL", 125, 772.88, 941.53), ("MSFT", 85, 56.60, 73.04), ("RDS-A", 400, 49.58, 55.74), ("AIG", 235, 54.21, 65.27), ("FB", 150, 124.31, 172.45)] headers = ["Stock Symbol", "No Shares", "Purchase Price", "Current Value"] headers1 = ["Stock Symbol", "No Shares", "Earnings/Loss"] separator1 = '+----------------+-------------+------------------+-----------------+' separator2 = '+================+=============+==================+=================+' separator3 = '+----------------+-------------+-----------------+' separator4 = '+================+=============+=================+' print("\nStock Ownership") print(separator1) print('| {:14} | {:11} | {:16} | {:15} |'.format(*headers)) print(separator2) for element in mydata: print('| {:14} | {:11} | {:16} | {:15} |'.format(*element)) print(separator1) print("\nStock Earnings/Losses ") print(separator3) print('| {:14} | {:11} | {:15} |'.format(*headers1)) print(separator4) for element in mydata: s = (element[3] - element[2]) * element[1] print('| {:14} | {:11} | ${:14,.2f} |'.format(element[0], element[1], s)) print(separator3)output:
RE: Python Formula - prophet11 - Apr-17-2019 I really appreciate it!!! Thank you so much for taking time out of your day to respond. Have a wonderful rest of your week. RE: Python Formula - Skaperen - Apr-17-2019 this make me think it would be nice to have a "table" module. it could take all the contents at one time in a list of lists or work an abstract data type letting you put data in cells one at a time. one version would output text like Larz60+'s code does. another could output or return HTML code. yet another could display the table graphically for a GUI app. RE: Python Formula - prophet11 - Apr-17-2019 Very good idea Skaperen. The other part of your idea could be to allow the program to receive a csv file upload, and then do the calculation. Let me know if you come up with a concept. Thank you RE: Python Formula - perfringo - Apr-17-2019 (Apr-17-2019, 04:03 AM)Skaperen Wrote: another could output or return HTML code tabulate has this feature, from their help: Quote:"html" produces HTML markup: |