Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的程序不允许我从python中以前的函数调用变量_Python_Variables_Syntax Error - Fatal编程技术网

我的程序不允许我从python中以前的函数调用变量

我的程序不允许我从python中以前的函数调用变量,python,variables,syntax-error,Python,Variables,Syntax Error,我的问题可以在下面的评论中找到 def Distanceinput(): distance = eval(input('Enter a distance (in light years):')) print("Velocity Relative time to reach", distance, "light years") 问题出现在下面的代码块中,我试图使用距离,但它作为错误返回。我能做些什么来纠正这个问题 def velocity(velocityPercent):

我的问题可以在下面的评论中找到

def Distanceinput():
    distance = eval(input('Enter a distance (in light years):'))
    print("Velocity Relative time to reach", distance, "light years")
问题出现在下面的代码块中,我试图使用距离,但它作为错误返回。我能做些什么来纠正这个问题

def velocity(velocityPercent):
    DilationFactor = (((1 - (velocityPercent ** 2)) / 10000) ** 0.5)
    perceptualSpeed = (distance * DilationFactor) * 100
    print(velocityPercent, + "% of light", perceptualSpeed, "years.")
上面的代码块就是导致问题的原因

def Time():
    Distanceinput()
    print()
    velocity(10)
    print()
    velocity(25)
    print()
    velocity(50)
    print()
    velocity(75)
    print()
    velocity(90)
    print()
    velocity(99)
    print()
Time()

有两种方法可以做到这一点:

  • 您使用的是全局变量
  • 在参数中传递变量

请看这里:

局部变量和全局变量之间的区别在于,局部变量只能在函数内部访问,而全局变量可以在整个程序中的任何位置访问。注意名称,本地(在特定区域内可用)和全局(在任何地方可用)

def Distanceinput():
    distance = eval(input('Enter a distance (in light years):'))
    print("Velocity Relative time to reach", distance, "light years")

def velocity(velocityPercent):
    DilationFactor = (((1 - (velocityPercent ** 2)) / 10000) ** 0.5)
    perceptualSpeed = (distance * DilationFactor) * 100
    print(velocityPercent, + "% of light", perceptualSpeed, "years.")
您的
Distanceinput()
很好。不过,您应该返回该值,以便以后在程序中使用。返回到局部和全局变量,
速度(velocityPercent)
中的
距离
被视为局部变量。您没有在函数中的任何地方说过需要访问变量
distance
,该变量在程序的其他地方有值。您可以这样完成此任务:

# Somewhere at the top of your code...
distance = None
然后,在您的函数中:

def Distanceinput():
    global distance # Says that your function requires the following global variable
    # rest of your code
    return distance # In case you need the output of the function later on.

def velocity(velocityPercent):
    global distance # Again, the same action as above

    DilationFactor = (((1 - (velocityPercent ** 2)) / 10000) ** 0.5)
    perceptualSpeed = (distance * DilationFactor) * 100
    print(velocityPercent, " % of light ", perceptualSpeed, " years.")

希望有帮助!:)

我对代码的组织方式有点不同。 另外,如果我理解正确的话,你的速度函数中有一些错误,所以我也修改了它


说明:

  • eval
    exec
    可以执行恶意代码,因此,除非您是唯一使用您的程序的人,否则请尽量避免使用它们<对于给定的问题,code>int可以正常工作
  • 我创建了一个类,可以从中创建不同的实例。每次创建实例时,都需要输入距离
  • \n
    是换行符
  • velocityPercent/=100。
    相当于将velocityPercent设置为 它以前的值除以100。请注意,它的
    100.
    带有一个点, 以防您正在使用。然后我删除了
    /100000
    膨胀系数中
    ,在
    感知速度中
    100中
您可以更改的一件事是
velocity
的输出

class Galaxy(object):

    DISTANCE = 'i like dogs'

    def Distanceinput(self):
        self.DISTANCE = int(input('\nEnter a distance (in light years):'))
        print("Velocity Relative time to reach", self.DISTANCE, "light years")

    def velocity(self, velocityPercent):
        velocityPercent /= 100.
        DilationFactor = ((1 - (velocityPercent ** 2)) ** 0.5)
        perceptualSpeed = self.DISTANCE * DilationFactor
        print(velocityPercent,  "% of light", perceptualSpeed, "years.")

    def Time(self):
        self.Distanceinput()
        print()
        for vel in (10, 25, 50, 75, 90, 99):
            self.velocity(vel)


galaxy_1 = Galaxy()

galaxy_1.Time()

函数中声明的变量是该函数的本地变量。这意味着您必须将
distance
值传递给
velocity
函数?Protip:开始使用函数参数并返回值
distance
distance输入的本地变量
因此对于
velocity
不可见。请对格式进行排序这将有助于我在变量函数中处理语法错误,因为python不理解距离是多少?您还应该解释为什么使用
global
关键字。:)无论出于什么原因,每次我尝试一个全局变量时,它都不起作用。我只是再试了一次,错误输出中没有任何更改。我粘贴了你的代码,它工作了。
print的语法中还有一个错误(velocityPercent,+%oflight),感知速度,“years.”
应该删除
+
。(您正在尝试添加字符串和整数)
class Galaxy(object):

    DISTANCE = 'i like dogs'

    def Distanceinput(self):
        self.DISTANCE = int(input('\nEnter a distance (in light years):'))
        print("Velocity Relative time to reach", self.DISTANCE, "light years")

    def velocity(self, velocityPercent):
        velocityPercent /= 100.
        DilationFactor = ((1 - (velocityPercent ** 2)) ** 0.5)
        perceptualSpeed = self.DISTANCE * DilationFactor
        print(velocityPercent,  "% of light", perceptualSpeed, "years.")

    def Time(self):
        self.Distanceinput()
        print()
        for vel in (10, 25, 50, 75, 90, 99):
            self.velocity(vel)


galaxy_1 = Galaxy()

galaxy_1.Time()