經過一連串~ brain storming, debug, paper studying, writing articles, positive of covid... hahaha jk lmao
However, I realised that my health is the piority! As a life where you have the energy to focus on everything else you want and dream of.
So, let's have a break for coding some simple python games.
Those games are my first side project, which also my self-study of Python before I start the AI and Big Data program. I learned it from the "小象學堂" which is a wechat account (kinda like wechat python learning robot ?!) that I have given from my brother. (p.s. My siblings were engineer before I be an AI RD/PM... but I'm the only one, who still alive in this field ... well life is hard... it's another story~ haha LOL, let come back to the article )
此篇-匯率轉換會讓使用者在執行程式後, 自動跑出可以輸入到terminal的input 你可以選擇想要轉換的幣, 此篇練習使用的是美元和台幣, 你可以輸入"USD" (美元) 和 "TWD" (新台幣) 在你想要換的數值後面。
Etc. 100 USD , 500 TWD (若數值和幣名之間沒有輸入空白見也是可以進行匯率轉換的計算的)。
需要輸入新台幣想要轉美元為多少錢?(數字) 不需要輸入幣值, 當然如果你想要轉換別種貨幣, 只需修改幣值匯率的參數即可(USD_VS_TWD = 31.82
), 當然幣值的變數名稱和輸出名稱, 也可以改成你想轉換的貨幣名稱。
#匯率
USD_VS_TWD = 31.82
#人民幣的輸入
twd_str_value = input('請輸入新台幣(TWD)金額: ')
#將字符串轉為數字
twd_value = eval(twd_str_value)
#匯率計算
usd_value = twd_value / USD_VS_TWD
#輸出結果
print('美元(USD)金額是: ', usd_value)
1. RUN 上面的程式, 會出現 "請輸入新台幣(TWD)金額:" 在terminal 中
2. 輸入想轉換的新台幣轉美元的數值 >>> 1000
3. 程式成功輸出轉換成美元的金額為 31.426775612822123
#匯率
USD_VS_TWD = 31.82
#帶單位的貨幣輸入
currency_str_value = input('請輸入帶單位的貨幣金額(退出程序請按Q): ')
i = 0
while currency_str_value != 'Q':
i = i + 1
#print('循環次數', i)
#獲取貨幣單位
unit = currency_str_value[-3:]
print(unit)
if unit == 'TWD':
#輸入的是新台幣
twd_str_value = currency_str_value[:-3]
# 將字符串轉換為數字
twd_value = eval(twd_str_value)
# 匯率計算
usd_value = twd_value / USD_VS_TWD
# 輸出結果
print('美元(USD)金額是: ', usd_value)
elif unit == 'USD':
# 輸入的是美元
usd_str_value = currency_str_value[:-3]
#將字符串轉換為數字
usd_value = eval(usd_str_value)
#匯率計算
twd_value = usd_value * USD_VS_TWD
# 輸出結果
print('新台幣(TWD)金額是: ', twd_value)
else:
#其他情況
print('目前版本尚不支持該種貨幣')
print('**********************************************************')
#帶單位的貨幣輸入
currency_str_value = input('請輸入帶單位的貨幣金額(退出程序請按Q): ')
print ('程式已退出!')
def main():
"""
主函數
"""
# 匯率
USD_VS_TWD = 31.82
# 帶單位的貨幣輸入
currency_str_value = input('請輸入帶單位的貨幣金額: ')
unit = currency_str_value[-3:]
if unit == 'TWD':
exchange_rate = 1 / USD_VS_TWD
elif unit == 'USD':
exchange_rate = USD_VS_TWD
else:
exchange_rate = -1
if exchange_rate != -1:
in_money = eval(currency_str_value[:-3])
#使用lambda定義函數
convert_currency2 = lambda x: x * exchange_rate
# # 調用函數
# out_money = convert_currency(in_money, exchange_rate)
# 調用Lambda函數
out_money = convert_currency2(in_money)
print('轉換後的金額: ', out_money)
else:
print('不支持該種貨幣!')
if __name__ == '__main__':
main()