在Python 3.4中打印最大最小值
我正在学习Python,这是我喜欢的Udacity课程,“通过Python介绍计算机科学”。我在这里的尝试:在Python 3.4中打印最大最小值,python,Python,我正在学习Python,这是我喜欢的Udacity课程,“通过Python介绍计算机科学”。我在这里的尝试: def biggest(x,y,z): max = x if y>max: max = y if z>max: max = z return max def smallest(x,y,z): min = x if y<min: min = y if z&l
def biggest(x,y,z):
max = x
if y>max:
max = y
if z>max:
max = z
return max
def smallest(x,y,z):
min = x
if y<min:
min = y
if z<min:
min = z
return min
def set_range(x,y,z):
result==biggest-smallest
return result
print set_range(10, 4, 7)
为什么会出现此错误?这里有一些更正
def biggest(x,y,z):
max = x
if y>max:
max = y
if z>max:
max = z
return max
def smallest(x,y,z):
min = x
if y<min:
min = y
if z<min:
min = z
return min
def set_range(x,y,z):
big = biggest(x,y,z) #assign the function result to a variable
small = smallest(x,y,z) #also let it inherit the inputs
result = big - small
print big
print small
return result
print set_range(10, 4, 7)
def最大值(x,y,z):
最大值=x
如果y>最大值:
最大值=y
如果z>最大值:
最大值=z
返回最大值
def最小值(x、y、z):
最小=x
如果y我不知道为什么您有两个相同的函数,但您需要实际调用这些函数,传递参数并使用=
分配not=
以实现相等:
def biggest(x, y, z):
mx = x
if y > mx:
mx = y
if z > mx:
mx = z
return mx
def smallest(x, y, z):
mn = x
if y < mn:
mn = y
if z < mn:
mn = z
return mn
def set_range(x, y, z):
# use "=" for assignment not "==" for equality
result = biggest(x, y, z) - smallest(x, y, z)
return result
print set_range(10, 4, 7)
def最大值(x,y,z):
mx=x
如果y>mx:
mx=y
如果z>mx:
mx=z
返回mx
def最小值(x、y、z):
mn=x
如果y
=
用于测试两个值是否相等,即1==1
,单个=
用于为变量分配名称,即foo=1
最好避免隐藏内置的max和min函数,因此我更改了函数中的名称。看起来您需要重新学习赋值(=
而不是=
)和调用函数。您正在测试相等性,而不是对函数进行赋值,并且从不调用同样涉及传递xy和z的函数result=maxist(x,y,z)-minimate(x,y,z)
您不能减去两个函数。您需要做的是从result
中的函数中读取最大值
和最小值
的返回值,我认为还值得一提的是,整个代码可以重构为my_numbers=[10,4,7]
,打印最大值(my_numbers)-min(my_numbers)
@jesuime,可能是因为问题基本上是“这是我的代码,这是错误,开始。”
def biggest(x, y, z):
mx = x
if y > mx:
mx = y
if z > mx:
mx = z
return mx
def smallest(x, y, z):
mn = x
if y < mn:
mn = y
if z < mn:
mn = z
return mn
def set_range(x, y, z):
# use "=" for assignment not "==" for equality
result = biggest(x, y, z) - smallest(x, y, z)
return result
print set_range(10, 4, 7)