我是编程新手。打算直接把那个开火。现在我将列出三个代码模块:
def GCD(x,y):
#gives the Greatest Common Divisor of two values.
while y != 0:
r = x
while r >= y: #these two lines are used to
r -= y #calculate remainder of x/y
x = y
y = r
print x
这是我编写的原始程序,基于 GCD 的欧几里得算法。它运行正常。我现在想删除上面的两个注释行并将其替换为对我所做的另一个模块的调用,该模块计算余数:
余数计算器
def xy(x, y):
#Gives the remainder of the division of x by y. Outputs as r.
while x >= y:
x -= y
r = x
该程序也可以正常运行。我想在我编辑的程序中使用名称“r”的值。我试图在下面这样做,但它会导致问题:
def GCD(x,y):
import remainder
#gives the Greatest Common Divisor of two values.
while y != 0:
remainder.xy(x,y)
from remainder import r #here is the problem. This line is my attempt at
#importing the value of r from the remainder calculating
#module into this module. This line is incorrect.
x = y
y = r #Python cannot find 'r'. I want to use the value for 'r' from the execution
#of the remainder calculating module. Attempts to find how have been
#unsuccessful.
print x
我需要找出如何在我的第二个 GCD 模块中使用我的 xy 模块中计算出的 'r' 值。我试过使用
global r
尽管我没有成功,但在我的模块中。我不确定我是否正确解释了“全局”的功能。
我会很感激你的帮助。
杰特霍尔特