Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/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中调用的函数不返回值_Python - Fatal编程技术网

在python中调用的函数不返回值

在python中调用的函数不返回值,python,Python,一般来说,我对python和编码比较陌生。 我已经搜索了其他一些类似的问题,似乎已经找不到我所寻找的答案。 我正在开发一个小程序,它将计算篮球运动员的进攻效率,但当我定义一个程序并调用它时,它不会产生任何价值 def fgp(x, y): fgp = (x/y)*100 return; def fgpoutput(): x = int(input("How many shots made?")); y = int(input("How many shots at

一般来说,我对python和编码比较陌生。 我已经搜索了其他一些类似的问题,似乎已经找不到我所寻找的答案。 我正在开发一个小程序,它将计算篮球运动员的进攻效率,但当我定义一个程序并调用它时,它不会产生任何价值

def fgp(x, y):
    fgp = (x/y)*100
    return;

def fgpoutput():
    x = int(input("How many shots made?"));
    y = int(input("How many shots attempted?")); 
    fgp(x, y);
    output = "This player is shooting",fgp,"%"
    print(output)


fgpoutput()
我认为这似乎有效,但我无法判断,因为它返回以下内容:

How many shots made?5
How many shots attempted?10
('This player is shooting', function fgp at 0x02561858, '%')

我觉得我已经接近了,但似乎无法抓住它

函数和Python中的其他所有东西一样,都是对象。打印
fgp
时,您正在打印对可执行函数对象的引用

但是,与Matlab不同的是,要从Python中的函数返回,实际上必须
返回
值:

def fgp(x, y):
    return (x/y) * 100
如果要调用
fgp
,则必须执行它:

output = "This player is shooting", fgp(x, y), "%"
output=“该玩家正在射击”,fgp,“%”
正在打印“fgp”函数本身,而不是它正在计算的内容。您可能正在寻找的是:

def fgp(x, y):
    return (x / y) * 100
这将返回要计算的值。然后您可以在输出中调用它:

def fgpoutput():
    x = int(input("How many shots made?"))
    y = int(input("How many shots attempted?"))
    result = fgp(x, y)
    output = "This player is shooting {} %".format(result)
    print(output)

另外,在Python中,行尾不需要分号。

好的,这里有一些不同的问题

  • 您不会从
    函数fgp
    返回任何内容:
    返回fgp
    末尾的code>返回
    None
    ,在Python中,这表示没有值。我不想那样!相反,请使用:
    返回fgp
  • 您没有在
    fgpoutput
    中调用
    fgp
    ——您只是在打印函数本身。相反,您希望像这样调用函数:
    fgp(x,y)
    ,它现在返回计算值
  • 他们认为你构建
    输出的方式不太正确。在Python中,有一个字符串方法用于格式化字符串以包含数字:
    str.format()
    因此,我们总共得到:

    def fgp(x, y):
        fgp = (x/y)*100
        return fgp
    
    def fgpoutput():
        x = int(input("How many shots made?"));
        y = int(input("How many shots attempted?")); 
        output = "This player is shooting {} %".format(fgp(x, y))
        print(output)
    
    
    fgpoutput()
    

    总的来说,你肯定走对了方向。祝你好运

    似乎有一些问题

    首先是功能-

    def fgp(x, y):
        fgp = (x/y) * 100
        return;
    
    在上面的函数中,您在函数“fgp”内再次使用“fgp”。这本身不是一个问题(函数fgp是全局的,而函数fgp中的fgp是局部的),但它非常不可读且令人困惑。如果必须的话,你应该使用类似“f”的词

    第二-

    (x/y) * 100
    
    这个函数几乎总是返回0(如果x和y都是整数,并且在您的例子中它们都是且x
    (x * 100.0) / y # force the numerator to a float
    
    所以你的fgp变成了

    def fgp(x,y):
        assert y is not 0
        return (x * 100.0/y) 
    

    如果y恰好为零,则让它引发一个断言错误。应该是这样的

    您的字符串格式语法不正确,以及使用分号等其他一些位。嗯,字符串格式(或缺少分号)可以通过
    print(*output)
    而不是
    print(output)
    正确。但无论如何,这并不是最清晰的方法…谢谢你们的意见,责怪我的教授,因为她坚持我们必须使用分号,因此我总是把它们包括进来。你们的观点3是错误的。在Python3中(在给定输入调用的情况下,OP显然正在使用它),将两个int除以返回一个float。但除此之外,其他一切都有很好的解释。修复@abarnert。谢谢非常感谢你们的帮助。