【问题标题】:How to assign an if statement's result into an equation?如何将 if 语句的结果分配给等式?
【发布时间】:2019-08-10 12:07:33
【问题描述】:

我是 python 新手,我必须运行一个程序来根据用户输入获取正确的利率,并使用获得的利率来计算每月赚取的利息。

为了计算所赚取的利息,我正在尝试使用打印结果来创建计算每月所赚取利息的公式。但是,我已经尝试了很多东西,但我不知道如何纠正这个问题。


transaction_category = [2000, 2500, 5000, 15000, 30000]
first_50k_1_category_rates = [0.05, 1.55, 1.85, 1.90, 2.00, 2.08]

if (count == 1) and (account_balance <= 50000) and (total_eligible_monthly_transactions < transaction_category[0]):
    print(f'Interest rate applicable is: {first_50k_1_category_rates[0]: .2f}%')

if (count == 1) and (account_balance <= 50000) and (transaction_category[0] <= total_eligible_monthly_transactions < transaction_category[1]):
    print(f'Interest rate applicable is: {first_50k_1_category_rates[1]: .2f}%')

【问题讨论】:

  • 也许重新思考一下逻辑,if 语句应该选择一个计算,对吧? print 的调用只是为您提供有关正在发生的事情的信息 - 它应该与我建议的逻辑或数学无关。
  • 是的,if 语句将给出我应该用来计算每月赚取利息的利率。只是不确定如何使用公式中的输出来计算所赚取的利息。
  • 输出是什么意思? printif 语句都没有输出(尽管我认为 print 实际上返回 none)。
  • 获取费率时的输出,即 first_50k_1_category_rates 或 first_50k_2_categories_or_more_rates
  • if 没有result(或者更确切地说是value),因为它是一个流控制statement ,也就是自成一头的野兽。 ፨ ፨所以不,您不能将ifresult 用于equation(我会说,assignment statement)。另一方面,if 后面的代码块可以包含任意数量的赋值,并且您可以稍后使用与名称绑定的值。有关示例,请参见 tripleee's answer

标签: python


【解决方案1】:

你的问题不太清楚,但我猜你正在寻找类似的东西

if (count == 1) and (account_balance <= 50000) and (transaction_category[3] <= total_eligible_monthly_transactions < transaction_category[4]):
    applicable_interest_rate = first_50k_1_category_rates[4]

elif (count == 1) and (account_balance <= 50000) and (total_eligible_monthly_transactions >= transaction_category[4]):
    applicable_interest_rate = first_50k_1_category_rates[5]

print(f'Interest rate applicable is: {applicable_interest_rate: .2f}%')

这只是一个草图;您必须确保始终定义新变量,然后在最终方程中使用它。

可能重复的条件也应该重构,所以你不要一遍又一遍地比较相同的东西。

if (count == 1) and (account_balance <= 50000):
    if transaction_category[3] <= total_eligible_monthly_transactions < transaction_category[4]:
        applicable_interest_rate = first_50k_1_category_rates[4]
    elif total_eligible_monthly_transactions >= transaction_category[4]:
        applicable_interest_rate = first_50k_1_category_rates[5]

但同样,没有看到完整的脚本,不清楚具体如何重构。这只是说明这一想法的一个例子。

【讨论】:

  • 嗨,我输入了整个代码。希望能帮助到你!我试图按照你说的去做,但 python 说 apply_interest_rate 没有定义。我该怎么办?
  • 哦,你或多或少早了 10 分钟就给出了我的答案,我觉得我会 +1 并删除我的...
  • 这个答案的要点是仅仅打印决定并不能告诉 Python 使用该数字。您还需要将其分配给变量或在函数调用或其他任何内容中使用它。我的建议是定义一个新变量并在最终方程中使用它。就像答案已经解释的那样,即使这些if 条件都不为真,您也需要确保定义了这个变量。一种常见的安排是在​​代码顶部使用默认值定义变量,然后在默认值不正确的情况下用不同的值覆盖默认值。
  • 另一种方法是将其设置为None,然后确保每个代码路径都用有效数字覆盖它。如果某些代码路径无法做到这一点,这将导致回溯,这对最终用户来说很烦人,但比由于您忘记完成一组代码更改而产生错误结果而没有任何警告的错误代码要好得多。跨度>
  • @tripleee 非常感谢!你的方法帮助我解决了我的问题。感谢您非常耐心!
【解决方案2】:

因此,您可以在一个代码块中执行 if/else,或者将这些打印语句转换为变量并返回它们。两者都会说结果为 var name。

def foo(condition1, condition2):

    if condition1 < condition2:
        result = (1 + 1)

    if 1 == False:
        result = (1 - 1)

    return result

print(foo(1, 2))
  • 在函数式编程方面有更好的方法来做到这一点,例如:(lambda :f"b:{b}",lambda :f"a:{a}")[a&gt;b](),但似乎这种代码风格是必要的,所以坚持下去。

【讨论】:

  • @tripleee 我刚起床,从我的手机转到笔记本电脑,感谢您指出,我已修复;)
【解决方案3】:

如果您在使用范围时遇到问题,可以试试这个(安全的)hack:

_scope = {
    "applicable_interest_rate1": first_50k_1_category_rates[4],
    "applicable_interest_rate2": first_50k_1_category_rates[5],
}

def foo(condition1, condition2):

    if condition1 < condition2:
        result = _scope["applicable_interest_rate1"]

    if 1 == False:
        result = _scope["applicable_interest_rate2"]

    return result


print(foo(1, 2))

【讨论】:

    猜你喜欢
    • 2012-05-09
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 2017-01-07
    • 2021-02-02
    • 2019-08-08
    • 2018-04-02
    • 1970-01-01
    相关资源
    最近更新 更多