2

When I run the code below, the following message box pops up. Why is it printing those annoying braces where I don't want them to? I am using Python 2.7 and EasyGui.

enter image description here

from __future__ import division
from easygui import *
import ystockquote

def main():
    PRHSX_message()

def PRHSX_message():
    prhsx_share_price = float(ystockquote.get_last_trade_price('PRHSX'))
    prhsx_daily_change = ystockquote.get_change('PRHSX')
    prhsx_total_shares = 47.527
    prhsx_starting_value = 2500
    prhsx_percent_change = (((prhsx_share_price * prhsx_total_shares) / prhsx_starting_value) - 1) * 100
    prhsx_percent_change = str("%.2f" % prhsx_percent_change) + "%"
    prhsx_current_value = prhsx_share_price * prhsx_total_shares
    prhsx_profit = prhsx_current_value - prhsx_starting_value
    prhsx_current_value =  "$" + str("%.2f" % prhsx_current_value)
    prhsx_profit = "$" + str("%.2f" % prhsx_profit)
    prhsx_string = "T. Rowe Price Roth IRA Account\n____________________________\n   \
                \nPercent Change:", str(prhsx_daily_change), \
                "\nShare Price:", prhsx_share_price, \
                "\nCurrent Value:", prhsx_current_value, \
                "\nPercent Growth:", prhsx_percent_change, \
                "\nProfit:", prhsx_profit
    return msgbox(prhsx_string)

if __name__ == '__main__':
    main()
paisanco
  • 4,098
  • 6
  • 27
  • 33
Shankar Kumar
  • 2,197
  • 6
  • 25
  • 32

1 Answers1

5

You are passing in a tuple, not a string. Use string formatting here to ease creating a string:

def PRHSX_message():
    prhsx_share_price = float(ystockquote.get_last_trade_price('PRHSX'))
    prhsx_daily_change = ystockquote.get_change('PRHSX')
    prhsx_total_shares = 47.527
    prhsx_starting_value = 2500
    prhsx_percent_change = (((prhsx_share_price * prhsx_total_shares) / prhsx_starting_value) - 1) * 100
    prhsx_current_value = prhsx_share_price * prhsx_total_shares
    prhsx_profit = prhsx_current_value - prhsx_starting_value

    prhsx_string = (
        "T. Rowe Price Roth IRA Account\n"
        "____________________________\n\n"
        "Percent Change: {}\n"
        "Share Price: {}\n"
        "Current Value: ${:.2f}\n"
        "Percent Growth: {:.2f}%\n"
        "Profit: ${:.2f}").format(
        prhsx_daily_change, prhsx_share_price, prhsx_current_value,
        prhsx_percent_change, prhsx_profit)

The float value formatting has been moved into the string format.

The main string is concatenated by the Python compiler, as there are no other statements between the various strings.

You could also use a multi-line string with triple-quoting, but then your indentation might look a little funny:

def PRHSX_message():
    prhsx_share_price = float(ystockquote.get_last_trade_price('PRHSX'))
    prhsx_daily_change = ystockquote.get_change('PRHSX')
    prhsx_total_shares = 47.527
    prhsx_starting_value = 2500
    prhsx_percent_change = (((prhsx_share_price * prhsx_total_shares) / prhsx_starting_value) - 1) * 100
    prhsx_current_value = prhsx_share_price * prhsx_total_shares
    prhsx_profit = prhsx_current_value - prhsx_starting_value

    prhsx_string = """\
T. Rowe Price Roth IRA Account
____________________________

Percent Change: {}
Share Price: {}
Current Value: ${:.2f}
Percent Growth: {:.2f}%
Profit: ${:.2f}""".format(
        prhsx_daily_change, prhsx_share_price, prhsx_current_value,
        prhsx_percent_change, prhsx_profit)

For that reason I usually place a format string as a global constant.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343