简体   繁体   English

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

[英]my program is not allowing me to call a variable from a previous function in python

  1. My questions are found in the comments below 我的问题可以在下面的评论中找到

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

    the problem is coming in the following chunk of code where I attempt to use distance from above but it comes back as an error. 问题出在下面的代码块中,我尝试从上面使用距离,但它作为错误返回。 what can I do to rectify this problem? 我该怎么办才能纠正此问题?

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

    the above chunk of code is what is causing problems 上面的代码块是导致问题的原因

     def Time(): Distanceinput() print() velocity(10) print() velocity(25) print() velocity(50) print() velocity(75) print() velocity(90) print() velocity(99) print() Time() 
distance = None
def Distanceinput():
    global distance
    distance = eval(input('Enter a distance (in light years):'))
    print("Velocity Relative time to reach", distance, "light years")

The difference between local and global variables are that local variables may only be accessed inside a function, whereas a global variable may be accessed anywhere throughout the program. 之间的区别localglobal变量是局部变量只能在函数内部访问,而全局变量可以在任何地方在整个程序中访问。 Note the names, local (available within a certain area), and global, available everywhere. 注意名称(本地(在特定区域内可用)和全局),随处可用。

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.")

Your Distanceinput() is fine. 您的Distanceinput()很好。 You should return the value though so that it can be used later on in the program. 但是,您应该返回该值,以便以后可以在程序中使用它。 Returning to the local and global variables, distance in velocity(velocityPercent) is considered a local variable. 回到局部变量和全局变量, velocity(velocityPercent) distance被视为局部变量。 You haven't said anywhere in your function that you needed to access the variable distance that has a value elsewhere in your program. 您在函数的任何地方都没有说过需要访问在程序中其他distance具有值的可变distance You can accomplish this like so: 您可以这样实现:

# Somewhere at the top of your code...
distance = None

Then, in your function: 然后,在您的函数中:

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.")

Hope it helps! 希望能帮助到你! :) :)

I organized your code a bit differently. 我对您的代码进行了一些不同的组织。 Also, if i understood correctly there were some bugs in your velocity function, so i changed it as well. 另外,如果我正确理解了您的速度函数中的一些错误,那么我也将其更改。


Explanation: 说明:

  • eval and exec can execute malicious code, so unless you are the only person using your program, try to avoid them. evalexec可以执行恶意代码,因此,除非您是唯一使用程序的人,否则请尽量避免使用它们。 int will work just fine for given problem. 对于给定的问题, int可以正常工作。
  • I created a class, from which you can create different instances. 我创建了一个类,您可以从中创建不同的实例。 Each time you create an instance you get to input the distance . 每次创建实例时,都需要输入distance
  • \\n is the newline character. \\n是换行符。
  • velocityPercent /= 100. is equivalent to set velocityPercent to its previous value divided by 100 . velocityPercent /= 100.等效于将velocityPercent设置为其先前值除以100 Note that its 100. with a dot, in case you are using python 2.7 . 请注意,如果您使用的是python 2.7 ,则其带有点的100. .。 Then i removed the /100000 in DilationFactor , and 100 in perceptualSpeed . 然后我在DilationFactor删除了/100000 ,在perceptualSpeed删除了100

One thing you could change is the output of velocity . 您可以更改的一件事是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()

There's 2 way of doing this : 有两种方法:

  • You work with a global variable 您使用全局变量
  • You pass the variable in parameter 您在参数中传递变量

Look here : http://paste2.org/_wpZH5I8m 看这里: http : //paste2.org/_wpZH5I8m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM