2014-06-28 46 views
2

當我運行下面的代碼時,彈出以下消息框。爲什麼它在我不想要的地方打印那些惱人的大括號?我正在使用Python 2.7和EasyGui。爲什麼Python在這裏打印大括號?

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() 

回答

5

你傳遞一個元組,而不是一個字符串。使用string formatting這裏減輕創建一個字符串:

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) 

的浮點值的格式已被移動到字符串格式。

主要字符串由Python編譯器連接,因爲在各種字符串之間沒有其他語句。

你也可以使用多線串三報價,但隨後你的壓痕可能看起來有點滑稽:

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) 

出於這個原因,我通常把格式字符串作爲全局常量。

+0

你忘了.02的精度;) – ThiefMaster

+0

@ThiefMaster:我沒有改變原來的任務;我現在已經做到了。 –

+0

啊。無論如何,現在你有'prhsx_current_value = prhsx_current_value'這似乎沒有多大意義:p – ThiefMaster