【问题标题】:How to get a function to print the result from a separate function, but the separate function not print a result.如何获得一个函数来打印来自单独函数的结果,但单独的函数不打印结果。
【发布时间】:2017-05-24 05:04:37
【问题描述】:

很抱歉这篇措辞糟糕的帖子,我已经尝试做这个练习测试题好几个小时了,我的大脑已经变成糊状了。 因此,在 uni 的练习测验中,我们必须调用以前的函数,不允许使用全局常量或变量。测验服务器然后测试原始函数以确保它有效,因此它输入:

cost = fun_function(5, 31, 15, 10)
print('5 items cost ${:.2f}'.format(cost))

并期待结果:

5 items cost $155.00

我的代码的问题是它会自动打印项目成本而没有添加

print('5 items cost ${:.2f}'.format(cost))

但如果我删除代码的打印元素,我不会通过第一个问题。第一个问题只是代码自动运行,输入为 25,代码必须自动输出所需的结果,不添加任何输入。代码的期望输出是:

25 items cost $607.50

那么我如何让代码自动打印主函数的结果,而不是自动打印任何额外的测试。 我的代码:

"""Prints out cost of a group of items with discount if it meets item 
required thresh hold"""

def fun_function(
    n_items, cost_per_item, discount_percent, discount_threshold
    ):
    """Prints out cost of a group of items with discount if it meets item 
    required thresh hold"""
    if n_items > discount_threshold:
        final_cost = (n_items * cost_per_item) * (1 - discount_percent/100)
    elif n_items < discount_threshold:
        final_cost = n_items * cost_per_item
    print('{} items cost ${:.2f}'.format(n_items, final_cost))

def main():
    """Deminstrates that the function works as desired"""
    fun_function(int(input("How many items? ")), 27, 10, 20)

main()

所以我想我是在问如何让 main() 打印结果,但不让 fun_function() 打印结果

当我有 fun_function() 返回时 main() 打印如下:

return '{} items cost ${:.2f}'.format(n_items, final_cost)

print(fun_function(int(input("How many items? ")), 27, 10, 20))

我收到此错误:

print('5 items cost ${:.2f}'.format(cost))
Traceback (most recent call last):
  Python Shell, prompt 4, line 1
builtins.ValueError: Unknown format code 'f' for object of type 
'str'

当我输入时

print('5 items cost ${:.2f}'.format(cost))

这只是我从字面上复制粘贴测验服务器输入的内容,

【问题讨论】:

  • 错误信息提示您返回的值是字符串而不是整数或浮点数,因此请在return 之前将其转换为float(my_value)int(my_value)您甚至可以在同一行进行转换,例如return float(my_result) 假设您的结果可以安全地转换为浮点数。
  • 您不应在func_function 中使用打印语句。相反,返回结果字符串 ('{} items cost ${:.2f}'.format(n_items, final_cost))。在main,你可以用也可以不用。
  • 另外,就像@hallazzang 所说,你不应该在fun_function 中使用print,而应该使用return 一个值。这就是我首先要推荐的,直到我阅读了更多你的帖子,当你说“当我有 fun_function 返回时……”时被误解了
  • 当我有 main() 返回而不是打印时,我仍然有它自动打印答案的问题,而不是等待服务器输入print('5 items cost ${:.2f}'.format(cost))
  • @GH05T 我如何让fun_function() 将其作为浮点数或整数返回,因为我仍然需要来自测验服务器的测试答案,以便在输入数字 25 后自动打印25 items cost $607.50 . 要做到这一点,我不需要('{} items cost ${:.2f}'.format(n_items, final_cost))

标签: python function callback


【解决方案1】:

@JaredRendell

假设您获取费用的程序是正确的(我没有检查它 - 我只是想让您更清楚:

def fun_function(
    n_items, cost_per_item, discount_percent, discount_threshold):
    """Prints out cost of a group of items with discount if it meets item 
    required thresh hold
    """
    if n_items > discount_threshold:
        final_cost = (n_items * cost_per_item) * (1 - discount_percent/100)
    elif n_items < discount_threshold:
        final_cost = n_items * cost_per_item
    return n_items, final_cost


def main():
    """Deminstrates that the function works as desired"""
    n_items, final_cost = fun_function(int(input("How many items? ")), 27, 10, 20)

    print('{} items cost ${:.2f}'.format(float(n_items), float(final_cost)))


main()

注意,在mainprint 调用中,我根据您的错误消息添加了对float 的调用。这将确保值将作为浮点数返回,如果它们可以转换为浮点数(例如,如果它们是整数,它们将能够被转换,例如 1 或 2,或者可以是字符串转换为整数,例如“1”或“2”)。

以后,当你觉得你的大脑变得糊状时,休息一下,做点别的事情。说真的,你会惊讶地发现,你想出一个解决方案来解决你在前一天和第二天都被卡住的问题,甚至不去想它。

我还建议您从头开始,重写所有代码。很容易让自己陷入一种思维方式,因为你最终会变得依赖于你已经完成的工作,即使它可能有缺陷。当您继续盯着它时,几乎不可能跳出这个框框思考。

【讨论】:

    猜你喜欢
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 2016-11-22
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多