汇率兑换的函数问题

def main_program():
    # exchange rate
    USD_VS_CNY = 6.638
    # currency is input
    currency_str_value = input('''please input currency with unit
    (inputting Q will be exited): ''')
    tabix = 0
    while currency_str_value != 'Q':
        # loop of times
        tabix = 1 + tabix
        # capture the unit of inputting currency
        unit = currency_str_value[-3:]
        if unit == 'CNY':
            exchange_rate = 1 / USD_VS_CNY
        elif unit == 'USD':
            exchange_rate = USD_VS_CNY
        else:
            exchange_rate = -1

        if exchange_rate != -1:
            # make string into number
            in_money = eval(currency_str_value[:-3])
            # define lambda function
            curreny_converted = lambda x: x * exchange_rate
            # submit lambda function
            out_money = curreny_converted(in_money)
            print ("exchanged amount:", out_money)
        else:
            print ("inputting currency isn't support at present")

        print ("----------------------------")
        # currency is input
        currency_str_value = input('''please input currency with unit
        (inputting Q will be exited ):''')
    print("Program has been existed")


if __name__ == '__main_program__':
    main_program()

 

Robin_TY - PhD@CSU,研究方向:计算机视觉,模式识别

赞同来自: 子玄 fish

if 语句必须这么写:
if __name__ == '__main__':
    ...
因为当该模块(.py文件)作为整体执行时,__name__ 的值就是 '__main__'。所以上面的if为True,而你在判断的是 __nam__ == '__main_program__':这个语句为False,所以不会执行if代码块中的内容。

要回复问题请先登录注册