简体   繁体   English

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

[英]Called function not returning a value in python

I'm relatively new to python and coding in general. 我对python和一般编码来说还是比较新的。 I've searched through some of the other similar questions already cannot seem to find the answer for what I'm looking for. 我已经搜索了其他一些类似的问题,但似乎找不到我要找的答案。 I'm working on a small program that will calculate a basketball players Offensive Efficiency, yet when I define a program and call it back it does not produce a value. 我正在开发一个小程序,它将计算篮球运动员的进攻效率,但是当我定义一个程序并将其回调时,它不会产生任何价值。

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()

This seems to work I think, but I cannot tell because it returns this: 我认为这似乎可行,但我无法确定,因为它返回了以下内容:

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

I feel like I've gotten close, but cannot seem to nail it. 我觉得自己已经接近了,但似乎无法确定。

output = "This player is shooting",fgp,"%" is printing the "fgp" function itself, not what it is calculating. output = "This player is shooting",fgp,"%"本身正在打印“ fgp”功能,而不是其计算值。 What you are probably looking for is: 您可能正在寻找的是:

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

That will return the value you want to calculate. 这将返回您要计算的值。 Then you can call it in your output: 然后,您可以在输出中调用它:

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)

Also, you don't need the semicolons at the end of the lines in Python. 另外,在Python行的末尾不需要分号。

Ok, you've got a few different issues at play here. 好的,您在这里遇到了一些不同的问题。

  1. You don't return anything from function fgp : the return; 您不会从function fgp返回任何内容: return; at the end of fgp returns None , which in Python, indicates the absence of a value. fgp的末尾返回None ,这在Python中表示没有值。 Don't want that! 不想那样! Instead, use: return fgp . 而是使用: return fgp
  2. You're not calling fgp in fgpoutput - you're simply printing the function itself. 您不是在fgpoutput调用fgpfgpoutput只是在打印函数本身。 Instead, you want to call the function like this: fgp(x, y) , which now returns the calculated value. 相反,您想像这样调用函数: fgp(x, y) ,该函数现在返回计算出的值。
  3. They way you construct output isn't quite right. 他们构造output方式不太正确。 In Python, there's a string method for formatting strings to include numbers: str.format() . 在Python中,有一个用于格式化字符串以包括数字的字符串方法: str.format() Check out the documentation on it here . 在此处查看有关文档

So, altogether we get: 因此,我们总共得到:

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()

Overall though, you're definitely on the right track. 总体而言,您绝对是在正确的轨道上。 Good luck! 祝好运!

A function is, as everything else in Python, an object. 与Python中的其他所有功能一样,函数是一个对象。 When printing fgp , you are printing a reference to the executable function object. 打印fgp ,您正在打印对可执行函数对象的引用。

But, differing to Matlab, to return from a function in Python, you actually have to return the value: 但是,与Matlab不同,要从Python中的函数返回,实际上必须return值:

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

If you want to call fgp , you'll have to execute it: 如果要调用fgp ,则必须执行它:

output = "This player is shooting", fgp(x, y), "%"

There appear to be a few problems 似乎有一些问题

First the function - 首先功能-

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

In the function above you are using 'fgp' again inside the function 'fgp'. 在上面的函数中,您将再次在函数“ fgp”内部使用“ fgp”。 That per se is not a problem (the function fgp is a global while the fgp inside function fgp is local), but it's extremely unreadable and confusing. 这本身不是问题(函数fgp是全局的,而函数fgp内部的fgp是局部的),但这是极其不可读和令人困惑的。 You should use something like 'f' if you have to. 如果需要,您应该使用类似“ f”的名称。

Second - 第二-

(x/y) * 100

This function will almost always return 0 (if both x and y are integers and in your case they are and x < y). 该函数几乎总是返回0(如果x和y均为整数,并且在您的情况下它们均为x <y)。 That line should look something like 那条线应该看起来像

(x * 100.0) / y # force the numerator to a float

So your fgp becomes 所以你的fgp变成

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

Let it raise an AssertionError if y happens to be zero. 如果y恰好为零,则使其引发AssertionError This shoud be it 这应该是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM