Reputation: 21
SharesPurchased = 2000
PricePaid = 40
Commission = .03
SellPrice = 42.75
TotalPricePaid = SharesPurchased * PricePaid
CommissionPaid = TotalPricePaid * Commission
TotalSellPrice = SellPrice * SharesPurchased
CommissionSellPaid = TotalSellPrice * Commission
ProfitIfAny = TotalSellPrice - TotalPricePaid - CommissionPaid -
CommissionSellPaid
print ("""
Joe paid {} for his shares.
Joe paid () to his broker for purchases.
Joe sold his shares for {}
Joe paid {} to his broker for selling his shares.
Joe made a total of {}
"""
).format([TotalPricePaid, CommissionPaid, TotalSellPrice, CommissionSellPaid, ProfitIfAny])
Here's my spaghetti of code. I'm trying to figure this out for my first actual assignment in python. I can't seem to figure out how to get the assigned variables to pop into text without having to make 5 seperate print("").f lines in a row.
This is what I've produced for a working version :
print("Joe paid {} for his shares.").format(TotalPricePaid)
print("Joe paid {} to his broker for purchases.").format(CommissionPaid)
print("Joe sold his shares for {}").format(TotalSellPrice)
print("Joe paid {} to his broker for selling his
shares.").format(CommissionSellPaid)
print("Joe made a total of {}.").format(ProfitIfAny)
Upvotes: 0
Views: 381
Reputation: 1122222
You are passing in one argument to str.format()
, a list:
....format([TotalPricePaid, CommissionPaid, TotalSellPrice, CommissionSellPaid, ProfitIfAny])
You need to pass in separate arguments instead. Remove the [...]
list brackets.
You are also using ()
in one location where you probably wanted CommisionPaid
to be interpolated:
Joe paid () to his broker for purchases.
Replace those ()
with {}
Next, you are applying the .format()
method to whatever print()
returns, which is always None
. Use the .format()
call on the string, not the print()
call, and pass the result to print()
:
print ("""
...""".format(...))
The following works:
print ("""
Joe paid {} for his shares.
Joe paid {} to his broker for purchases.
Joe sold his shares for {}
Joe paid {} to his broker for selling his shares.
Joe made a total of {}
""".format(TotalPricePaid, CommissionPaid, TotalSellPrice, CommissionSellPaid, ProfitIfAny))
or, slightly better formatted:
print("""\
Joe paid {} for his shares.
Joe paid {} to his broker for purchases.
Joe sold his shares for {}
Joe paid {} to his broker for selling his shares.
Joe made a total of {}
""".format(
TotalPricePaid, CommissionPaid, TotalSellPrice,
CommissionSellPaid, ProfitIfAny))
Upvotes: 3