python - 不使用 if else 的货币转换器
问题描述
我正在使用 tkinter 上的 if-else 语句编写货币转换器程序。我只为其中六种货币编写了它,而且花费的时间太长。
如果我想为数百种货币写作怎么办?我也使用 if-else 吗?有没有更有效的方法来编程货币转换器而不使用 If-Else 语句?
from tkinter import ttk
from tkinter import *
import tkinter as tk
class CurrencyConverter(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title('Currency Converter')
self.geometry('500x300')
self.currencies_list = ['United States Dollar', 'Pound Sterling', 'Euro', 'Pakistani Rupee', 'Swiss Franc',
'Japanese Yen']
self.usd_to_other_list = [1, 0.72, 0.85, 153.50, 0.94, 110.70]
self.ps_to_other_list = [1.38, 1, 1.18, 212.15, 1.3, 153]
self.euro_to_other_list = [1.18, 0.85, 1, 180.55, 1.11, 130.19]
self.pkr_to_other_list = [0.0065, 0.0047, 0.0055, 1, 0.0061, 0.72]
self.sf_to_other_list = [1.06, 0.77, 0.9, 162.79, 1, 117.46]
self.jy_to_other_list = [0.009, 0.0065, 0.0077, 1.39, 0.0085, 1]
self.from_currency = tk.StringVar()
self.to_currency = tk.StringVar()
self.from_entry_variable = tk.IntVar()
self.to_entry_variable = tk.IntVar()
self.from_entry = ttk.Entry(
self,
textvariable=self.from_entry_variable,
width=40
)
self.from_combobox = ttk.Combobox(
self,
textvariable=self.from_currency,
values=tuple(self.currencies_list),
state='readonly'
)
self.from_combobox.set(self.currencies_list[0])
self.to_entry = ttk.Entry(
self,
textvariable=self.to_entry_variable,
width=40,
state='readonly'
)
self.to_combobox = ttk.Combobox(
self,
textvariable=self.to_currency,
values=tuple(self.currencies_list),
state='readonly'
)
self.to_combobox.set(self.currencies_list[4])
self.from_entry.pack()
self.from_combobox.pack()
self.to_entry.pack()
self.to_combobox.pack()
self.from_entry.bind('<KeyPress>', self.currency_converter)
self.from_entry.bind('<KeyRelease>', self.currency_converter)
def currency_converter(self, event):
try:
if self.from_currency.get() == 'United States Dollar':
if self.to_currency.get() == 'United States Dollar':
converted = int(self.from_entry_variable.get()) * self.usd_to_other_list[0]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pound Sterling':
converted = int(self.from_entry_variable.get()) * self.usd_to_other_list[1]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Euro':
converted = int(self.from_entry_variable.get()) * self.usd_to_other_list[2]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pakistani Rupee':
converted = int(self.from_entry_variable.get()) * self.usd_to_other_list[3]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Swiss Franc':
converted = int(self.from_entry_variable.get()) * self.usd_to_other_list[4]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Japanese Yen':
converted = int(self.from_entry_variable.get()) * self.usd_to_other_list[5]
self.to_entry_variable.set(value=f'{converted}')
elif self.from_currency.get() == 'Pound Sterling':
if self.to_currency.get() == 'United States Dollar':
converted = int(self.from_entry_variable.get()) * self.ps_to_other_list[0]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pound Sterling':
converted = int(self.from_entry_variable.get()) * self.ps_to_other_list[1]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Euro':
converted = int(self.from_entry_variable.get()) * self.ps_to_other_list[2]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pakistani Rupee':
converted = int(self.from_entry_variable.get()) * self.ps_to_other_list[3]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Swiss Franc':
converted = int(self.from_entry_variable.get()) * self.ps_to_other_list[4]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Japanese Yen':
converted = int(self.from_entry_variable.get()) * self.ps_to_other_list[5]
self.to_entry_variable.set(value=f'{converted}')
elif self.from_currency.get() == 'Euro':
if self.to_currency.get() == 'United States Dollar':
converted = int(self.from_entry_variable.get()) * self.euro_to_other_list[0]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pound Sterling':
converted = int(self.from_entry_variable.get()) * self.euro_to_other_list[1]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Euro':
converted = int(self.from_entry_variable.get()) * self.euro_to_other_list[2]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pakistani Rupee':
converted = int(self.from_entry_variable.get()) * self.euro_to_other_list[3]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Swiss Franc':
converted = int(self.from_entry_variable.get()) * self.euro_to_other_list[4]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Japanese Yen':
converted = int(self.from_entry_variable.get()) * self.euro_to_other_list[5]
self.to_entry_variable.set(value=f'{converted}')
elif self.from_currency.get() == 'Pakistani Rupee':
if self.to_currency.get() == 'United States Dollar':
converted = int(self.from_entry_variable.get()) * self.pkr_to_other_list[0]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pound Sterling':
converted = int(self.from_entry_variable.get()) * self.pkr_to_other_list[1]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Euro':
converted = int(self.from_entry_variable.get()) * self.pkr_to_other_list[2]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pakistani Rupee':
converted = int(self.from_entry_variable.get()) * self.pkr_to_other_list[3]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Swiss Franc':
converted = int(self.from_entry_variable.get()) * self.pkr_to_other_list[4]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Japanese Yen':
converted = int(self.from_entry_variable.get()) * self.pkr_to_other_list[5]
self.to_entry_variable.set(value=f'{converted}')
elif self.from_currency.get() == 'Swiss Franc':
if self.to_currency.get() == 'United States Dollar':
converted = int(self.from_entry_variable.get()) * self.sf_to_other_list[0]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pound Sterling':
converted = int(self.from_entry_variable.get()) * self.sf_to_other_list[1]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Euro':
converted = int(self.from_entry_variable.get()) * self.sf_to_other_list[2]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pakistani Rupee':
converted = int(self.from_entry_variable.get()) * self.sf_to_other_list[3]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Swiss Franc':
converted = int(self.from_entry_variable.get()) * self.sf_to_other_list[4]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Japanese Yen':
converted = int(self.from_entry_variable.get()) * self.sf_to_other_list[5]
self.to_entry_variable.set(value=f'{converted}')
elif self.from_currency.get() == 'Japanese Yen':
if self.to_currency.get() == 'United States Dollar':
converted = int(self.from_entry_variable.get()) * self.jy_to_other_list[0]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pound Sterling':
converted = int(self.from_entry_variable.get()) * self.jy_to_other_list[1]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Euro':
converted = int(self.from_entry_variable.get()) * self.jy_to_other_list[2]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pakistani Rupee':
converted = int(self.from_entry_variable.get()) * self.jy_to_other_list[3]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Swiss Franc':
converted = int(self.from_entry_variable.get()) * self.jy_to_other_list[4]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Japanese Yen':
converted = int(self.from_entry_variable.get()) * self.jy_to_other_list[5]
self.to_entry_variable.set(value=f'{converted}')
except TclError:
pass
root = CurrencyConverter()
root.mainloop()
解决方案
像这样的一些功能(我没有 Python 编译器 - 可能存在小错误)?
currencies_list = ['United States Dollar', 'Pound Sterling', 'Euro', 'Pakistani Rupee', 'Swiss Franc', 'Japanese Yen']
usd_to_other_list = [1, 0.72, 0.85, 153.5, 0.94, 110.7]
def GetParity(ValueToConvert, FromCurrency, ToCurrency):
SourceIndex = currencies_list.Index(FromCurrency)
DestIndex = currencies_list.Index(ToCurrency)
SourceR = usd_to_other_list(SourceIndex)
DestR = usd_to_other_list(DestIndex)
Result = ValueToConvert * DestR / SourceR
Return(Result)
推荐阅读
- python - 当字符串列表包含“inf”时,将其转换为列表时出错
- python-3.x - Django 管理字段。在关系字段中获取值
- string - 在半列之前和之后拆分字符串 [Kotlin]
- docker - Dockerfile:如何正确配置redis
- azure-pipelines-yaml - 从 Azure DevOps 管道模板访问调用文件夹的文件
- spring-boot - 使用 Oracle 的 Spring Boot 可分页计数查询错误
- python - Xlwings:如何将单个单元格中的两个逗号分隔值作为列表而不是浮点数读取?
- entity-framework - 如何通知用户在他缺席期间所做的所有更改
- bash - 如何编写一个 bash 脚本来退出程序?
- python - 使用matplotlib(python)在boxplot中添加补丁