为什么Python打印括号?

时间:2014-06-28 17:51:50

标签: python string formatting easygui

当我运行下面的代码时,会弹出以下消息框。为什么要打印那些我不想要它们的烦人牙箍呢?我使用的是Python 2.7和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()

1 个答案:

答案 0 :(得分: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)

出于这个原因,我通常将格式字符串作为全局常量。