gpt4 book ai didi

python - python 中调用的函数没有返回值

转载 作者:行者123 更新时间:2023-12-01 04:43:50 25 4
gpt4 key购买 nike

我对 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, '%')

我觉得我已经接近了,但似乎无法确定。

最佳答案

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

  1. 您不会从 function fgp 返回任何内容:fgp 末尾的 return; 返回 None,在 Python 中表示缺少值。不想那样!相反,请使用:return fgp
  2. 您没有在 fgpoutput 中调用 fgp - 您只是打印函数本身。相反,您希望像这样调用函数:fgp(x, y),它现在返回计算值。
  3. 他们构造输出的方式不太正确。在 Python 中,有一个字符串方法用于格式化字符串以包含数字:str.format()Check out the documentation on it here .

所以,我们总共得到:

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

总体而言,您绝对走在正确的道路上。祝你好运!

关于python - python 中调用的函数没有返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29933701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com