Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用函数(def)消除冗余?_Python_Python 3.x - Fatal编程技术网

Python 如何使用函数(def)消除冗余?

Python 如何使用函数(def)消除冗余?,python,python-3.x,Python,Python 3.x,我正在构建一个货币转换器,我已经基本完成了这个程序。然而,我试图通过实现一个函数或一个define块来消除冗余。我试过很多方法,但似乎都不管用 我当前的代码如下: EUR = 0.83 EGY = 16.22 def currency_converter(): money = total_value new_value = 0 if currency == 1: new_value = money*EUR_CON print("Your total is

我正在构建一个货币转换器,我已经基本完成了这个程序。然而,我试图通过实现一个函数或一个define块来消除冗余。我试过很多方法,但似乎都不管用

我当前的代码如下:

EUR = 0.83

EGY = 16.22

def currency_converter():
  money = total_value
  new_value = 0
  if currency == 1:
    new_value = money*EUR_CON
    print("Your total is " + "$" + str(money) + " US Dollars  which is " + "e£ " + str(new_value) + " European Euros.")
  elif currency == 2:
    new_value = money*EGY_CON
    print("Your total is " + "$" + str(money) + " US Dollars  which is " + "e£ " + str(new_value) + " Egyptian Pounds.")
我想在
if
/
elif
块下创建一个函数。我试过这样做:

def conversion(EUR_CON,GDP_CON, BRL_CON, EGY_CON, YEN_CON):
  new_value = money*conversion()
  print("Your total is " + "$" + str(money) + " US Dollars  which is " + str(new_value)+ str(conversion)

if currency == 1:
    conversion(EURO_CON)

但它不起作用。有人能帮忙吗?

把你所有的转换率都放到一个列表或字典里,这样你就不需要所有那些
if
语句了

函数不需要太多参数。只是你要兑换的货币和金额。然后,该函数可以查找与currency参数相关的所有信息

conversion_data = {
    'EUR': {'rate': 0.83, 'symbol': '€', 'name': 'European Euros'},
    'EGY': {'rate': 16.22, 'symbol': '£', 'name': 'Egyptian Pounds'},
    ...
}

def conversion(currency, dollars):
    new_value = dollars * conversion_data[currency]['rate']
    return f"Your total is ${dollars} US dollars which is {conversion_data[currency]['symbol']}{new_value} {conversion_data[currency]['name']}."

print(conversion('EUR', 5))

把你所有的转换率放到一个列表或字典中,这样你就不需要所有那些
if
语句了

函数不需要太多参数。只是你要兑换的货币和金额。然后,该函数可以查找与currency参数相关的所有信息

conversion_data = {
    'EUR': {'rate': 0.83, 'symbol': '€', 'name': 'European Euros'},
    'EGY': {'rate': 16.22, 'symbol': '£', 'name': 'Egyptian Pounds'},
    ...
}

def conversion(currency, dollars):
    new_value = dollars * conversion_data[currency]['rate']
    return f"Your total is ${dollars} US dollars which is {conversion_data[currency]['symbol']}{new_value} {conversion_data[currency]['name']}."

print(conversion('EUR', 5))

正确的方法是制作一个映射或
enum
,将转换类型与相关参数(在本例中为目标货币的乘数和字符串名称)联系起来。例如,使用
enum

from enum import Enum

class Currency(Enum):
    EUR = 0.83, "European Euros"
    EGY = 16.22, "Egyptian Pounds"

def currency_converter(target_currency):
    multiplier, name = target_currency.value  # Retrieve value of enum and unpack parts for use
    new_value = money * multiplier
    print(f"Your total is ${money} US Dollars which is {new_value} {name}")
这样,您就可以使用它,只需:

currency_converter(Currency.EUR)  # Convert to euros
需要明确的是:将
dict
用于类似的目的也很好。枚举主要强调存在一组固定的、已知的可能转换,其中,
dict
s没有充分地考虑到这一点(添加和删除键始终是可能的)

我会注意到,在实际代码中,函数通常不应该依赖于从全局函数接收非常量信息,也不应该打印结果(
返回
允许调用方打印
,或者不打印
打印
,如他们所选择),因此更好的设计应该是:

def convert_usd_to_currency(target_currency, money):
    multiplier, _ = target_currency.value  # Retrieve value of enum and unpack parts for use
    return money * multiplier
可能需要一个执行
打印的助手(如果您确实有许多地方需要以相同的方式格式化):


我承认我在这里缺乏想象力;我几乎从来没有看到需要考虑输出代码本身(每个位置根据需要打印不同的内容),因此我可能只会将工作内联到一个实际打印它的位置(而不是可能需要执行转换的许多位置).

正确的方法是制作一个映射或
enum
,将转换类型与相关参数(在本例中为目标货币的乘数和字符串名称)联系起来。例如,使用
enum

from enum import Enum

class Currency(Enum):
    EUR = 0.83, "European Euros"
    EGY = 16.22, "Egyptian Pounds"

def currency_converter(target_currency):
    multiplier, name = target_currency.value  # Retrieve value of enum and unpack parts for use
    new_value = money * multiplier
    print(f"Your total is ${money} US Dollars which is {new_value} {name}")
这样,您就可以使用它,只需:

currency_converter(Currency.EUR)  # Convert to euros
需要明确的是:将
dict
用于类似的目的也很好。枚举主要强调存在一组固定的、已知的可能转换,其中,
dict
s没有充分地考虑到这一点(添加和删除键始终是可能的)

我会注意到,在实际代码中,函数通常不应该依赖于从全局函数接收非常量信息,也不应该打印结果(
返回
允许调用方打印
,或者不打印
打印
,如他们所选择),因此更好的设计应该是:

def convert_usd_to_currency(target_currency, money):
    multiplier, _ = target_currency.value  # Retrieve value of enum and unpack parts for use
    return money * multiplier
可能需要一个执行
打印的助手(如果您确实有许多地方需要以相同的方式格式化):


我承认我在这里缺乏想象力;我几乎从来没有看到需要考虑输出代码本身(每个位置根据需要打印不同的内容),因此我可能只会将工作内联到一个实际打印它的位置(而不是可能需要执行转换的许多位置).

请修复缩进。Ok@Barmar对此表示抱歉。
转换函数1)是递归的,没有转义路径,2)有许多参数,这两个调用方都没有提供。假设您总是从同一货币开始,只希望将其转换为单个其他货币,我会为一个函数设置一个参数,基于用户的响应,他想使用这个响应。然后根据该响应使用适当的值。这里您需要一个函数的多个参数,但在调用它时只声明了一个。请修复缩进。Ok@Barmar对此表示抱歉。
转换(函数1)是递归的,没有转义路径,2)有许多参数,这两个调用方都没有提供。假设您总是从同一种货币开始,并且只希望将其转换为单个其他货币,我会根据用户希望使用的响应为函数设置一个参数。然后根据该响应使用适当的值。在这里,一个函数需要多个参数,但在调用它时只声明了一个参数。当我调用函数conversion()时,括号中应该放什么?我输入了EUR,0.83,但它给了我一条错误消息。我展示了一个示例第一个参数必须是引号中的字符串。第二个是美元的数量,当我调用函数conversion()时,括号里应该放什么?我输入了EUR,0.83,但它给了我一条错误消息。我展示了一个示例第一个参数必须是引号中的字符串。第二个是美元的数额,