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.
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"))