输入是什么并不重要,输出总是0,就像全局变量永远不会改变一样。请帮帮我,我疯了,因为我没有看到任何理由只显示0
x=0
y=0
import math
import operator
def move(steps,op_func,z):
if z == "x":
global x
x = op_func(x, int(steps))
else:
global y
y = op_func(y, int(steps))
def main():
user_direct=raw_input("Enter the way that you want the plane to move and how many steps you want it to move\n")
while user_direct != " ":
steps=user_direct[-1]
direction=user_direct[:-1]
directions = {"UP":move(steps,operator.add,"x"),
"DOWN":move(steps,operator.sub,"x"),
"RIGHT":move(steps,operator.add,"y"),
"LEFT":move(steps,operator.sub,"y")}
directions[direction.replace(" ","")]
user_direct=raw_input("Enter the way that you want the plane to move and how many steps you want it to move\n")
global x
global y
distance=math.sqrt(x**2+y**2)
print distance
main()
答案 0 :(得分:2)
正如我的评论所说,你的dict初始化实际上是运行那些函数并将值设置为返回值(NoneType
)。做这样的事情。
1。)在你的while循环之外启动你的dict,因为你不需要每次都重新初始化。
2。)更改它,以便值是您的参数的元组,如:
directions = {"UP":(operator.add,"x"),
"DOWN":(operator.sub,"x"),
"RIGHT":(operator.add,"y"),
"LEFT":(operator.sub,"y")}
请注意,dict
不包含步骤,因为您将把它用作每次调用的变量。
3。)使用以下行更改函数调用:
move(steps,directions[direction][0],directions[direction][1])
代替你当前的dict init / dict电话。
这个问题是,如果你的命令不是一个有效的命令,它会导致错误,所以我会将所有内容放在Try块中,如:
try:
move(steps,directions[direction][0],directions[direction][1])
except KeyError:
print('Not a valid key, try again')
else:
#here is where you put the math code you do to edit the globals
答案 1 :(得分:1)
你做错了这些事情:
此外,全局存储这些信息并不是您想要做的事情,而是将其存储在while循环的范围内。
让我们看一下你的字典定义在做什么:
directions = {"UP":move(steps,operator.add,"x"),
"DOWN":move(steps,operator.sub,"x"),
"RIGHT":move(steps,operator.add,"y"),
"LEFT":move(steps,operator.sub,"y")}
这些分配调用中的每一个都使用适当的值移动,并将它们设置为值。但是,move()
返回None
,因为没有为这些语句设置return
语句:全局变量在函数内部更新。因此,在一个while
循环之后,您的directions
数组看起来像这样:
{"UP": None, "DOWN": None, "RIGHT": None, "LEFT": None}
您的x
和y
全局值已经递增和递减一次。您可以使用以下内容替换move
函数来证明这一点:
def move(steps,op_func,z):
if z == "x":
global x
x = op_func(x, int(steps))
print("x is now {}".format(x))
else:
global y
y = op_func(y, int(steps))
print("y is now {}".format(y))
在REPL中,这就是你所看到的:
>>> y= 0
>>> x= 0
>>> steps = 1
>>> directions = {"UP":move(steps,operator.add,"x"),
>>> "DOWN":move(steps,operator.sub,"x"),
... "RIGHT":move(steps,operator.add,"y"),
... "LEFT":move(steps,operator.sub,"y")}
... x is now 1
x is now 0
y is now 1
y is now 0
然而,局部可以提供帮助:
>>> f = partial(move, op_func=operator.add, z="x")
>>> f(1)
>>> x is now 1
使用上述内容,您可以像这样定义directions
地图:
directions = {"UP":partial(move, op_func=operator.add, z="x"),
"DOWN":partial(move, op_func=operator.sub, z="x"),
"RIGHT":partial(move, op_func=operator.add, z="y"),
"LEFT":partial(move, op_func=operator.sub, z="y")}
这样做是替换每个"键"在你的字典中使用"部分功能"。部分功能为某些的参数'填入'以及稍后您可以仅使用剩余值调用该功能。正式:
partial(f(a, b, c), b, c) -> g(a)
每当您致电g
时,b
和c
将始终为f
的函数调用定义。
现在你所要做的就是改变这一行:
directions[direction.replace(" ","")]
对此:
move_func = directions[direction.replace(" ","")]
move_func(steps)
重新编写和清理程序有点收益:
import math
import operator
from functools import partial
# Always do imports first then global vars
x=0
y=0
def move(steps,op_func,z):
if z == "x":
global x
x = op_func(x, int(steps))
else:
global y
y = op_func(y, int(steps))
def main():
# We only need to define this once
directions = {"UP":partial(move, op_func=operator.add, z="x"),
"DOWN":partial(move, op_func=operator.sub, z="x"),
"RIGHT":partial(move, op_func=operator.add, z="y"),
"LEFT":partial(move, op_func=operator.sub, z="y")}
user_direct=raw_input("Enter the way that you want the plane to move and how many steps you want it to move\n")
while user_direct != " ":
steps=user_direct[-1]
direction=user_direct[:-1].replace(" ","")
move_func = directions[direction]
move_func(steps)
user_direct=raw_input("Enter the way that you want the plane to move and how many steps you want it to move\n")
global x
global y
distance=math.sqrt(x**2+y**2)
print distance
main()