Python 3.5.1 - 变量 returns none
Python 3.5.1 - variable returns none
我的问题是关于 Udacity 作业中的一些代码。以下代码不返回任何值。我假设我没有从 "normalized" 函数中正确调用 "scalar" 函数。 norm = self.scalar(scale)
returns 行输入 none。有人可以指点一下吗?
代码:
import math
from decimal import Decimal, getcontext
getcontext().prec = 10
class Vector(object):
def __init__(self, coordinates):
try:
if not coordinates:
raise ValueError
self.coordinates = tuple([Decimal(x) for x in coordinates])
self.dimension = len(self.coordinates)
except ValueError:
raise ValueError('The coordinates must be nonempty')
except TypeError:
raise TypeError('The coordinates must be an iterable')
def __eq__(self, v):
return self.coordinates == v.coordinates
def scalar(self, c):
new_coordinates = [Decimal(c)*x for x in self.coordinates]
#new_coordinates = []
#n = len(self.coordinates)
#for i in range(n):
# new_coordinates.append(self.coordinates[i] * c)
#print(Vector(new_coordinates))
def magnitude(self):
new_sq = [x**2 for x in self.coordinates]
new_mag = math.sqrt(sum(new_sq))
return (new_mag)
def normalized(self):
magnitude = self.magnitude()
scale = 1/magnitude
print(scale)
norm = self.scalar(scale)
#print(type(norm))
print(norm)
return (norm)
my_vector = Vector([1,2])
Vector.normalized(my_vector)
问题是您试图从 scalar
获取一个值,即使它 return 什么也没有。我不完全确定你想做什么 return 所以你必须自己处理。
一个值得注意的问题是您的方法调用 my_vector
实例的属性。这在技术上不是问题,但它可能应该改变。您的代码应如下所示。
my_vector = Vector([1,2])
my_vector.normalized()
Python 有一个很酷的小 技巧 如果没有指定,它总是 return None。因此,如果您编写一个没有 return 任何东西的函数 hello world,您将得到 None.
例如:
def hello_world():
print('hello world')
result = hello_world()
print(result) # prints nothing cause result==None
您的 scalar
方法中没有 return 语句,因此它将始终 return None.
我猜想 return 您在标量中创建的对象
def scalar(self, c):
new_coordinates = [Decimal(c)*x for x in self.coordinates]
return new_coordinates
或者为简洁起见
def scalar(self, c):
return [Decimal(c)*x for x in self.coordinates]
我的问题是关于 Udacity 作业中的一些代码。以下代码不返回任何值。我假设我没有从 "normalized" 函数中正确调用 "scalar" 函数。 norm = self.scalar(scale)
returns 行输入 none。有人可以指点一下吗?
代码:
import math
from decimal import Decimal, getcontext
getcontext().prec = 10
class Vector(object):
def __init__(self, coordinates):
try:
if not coordinates:
raise ValueError
self.coordinates = tuple([Decimal(x) for x in coordinates])
self.dimension = len(self.coordinates)
except ValueError:
raise ValueError('The coordinates must be nonempty')
except TypeError:
raise TypeError('The coordinates must be an iterable')
def __eq__(self, v):
return self.coordinates == v.coordinates
def scalar(self, c):
new_coordinates = [Decimal(c)*x for x in self.coordinates]
#new_coordinates = []
#n = len(self.coordinates)
#for i in range(n):
# new_coordinates.append(self.coordinates[i] * c)
#print(Vector(new_coordinates))
def magnitude(self):
new_sq = [x**2 for x in self.coordinates]
new_mag = math.sqrt(sum(new_sq))
return (new_mag)
def normalized(self):
magnitude = self.magnitude()
scale = 1/magnitude
print(scale)
norm = self.scalar(scale)
#print(type(norm))
print(norm)
return (norm)
my_vector = Vector([1,2])
Vector.normalized(my_vector)
问题是您试图从 scalar
获取一个值,即使它 return 什么也没有。我不完全确定你想做什么 return 所以你必须自己处理。
一个值得注意的问题是您的方法调用 my_vector
实例的属性。这在技术上不是问题,但它可能应该改变。您的代码应如下所示。
my_vector = Vector([1,2])
my_vector.normalized()
Python 有一个很酷的小 技巧 如果没有指定,它总是 return None。因此,如果您编写一个没有 return 任何东西的函数 hello world,您将得到 None.
例如:
def hello_world():
print('hello world')
result = hello_world()
print(result) # prints nothing cause result==None
您的 scalar
方法中没有 return 语句,因此它将始终 return None.
我猜想 return 您在标量中创建的对象
def scalar(self, c):
new_coordinates = [Decimal(c)*x for x in self.coordinates]
return new_coordinates
或者为简洁起见
def scalar(self, c):
return [Decimal(c)*x for x in self.coordinates]