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()

- 15
- 1
-
6Variables declared inside a function are local to that function. – merlin2011 Feb 17 '15 at 22:22
-
means you have to pass `distance` value to `velocity` function? – Vivek Sable Feb 17 '15 at 22:23
-
1Protip: Start using function **arguments** and **return values** – Cory Kramer Feb 17 '15 at 22:24
-
`distance` is local to `Distanceinput` so therefore not visible for `velocity` [see here](http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules) – Finn Feb 17 '15 at 22:24
-
yes I need to be able to call the distance number that was input by the user in the first function in the second function which is called the velocity function. – Mags Feb 17 '15 at 22:25
-
1please sort the formatting – mjs Feb 17 '15 at 22:26
-
I just did, I hope that is better – Mags Feb 17 '15 at 22:28
-
Exception `negative number cannot be raised to a fractional power` is coming for `DilationFactor = (((1 - (velocityPercent ** 2)) / 10000) ** 0.5)` statement. – Vivek Sable Feb 17 '15 at 22:33
-
there isn't a problem with the math the problem is coming late. Also the velocityPercent is a decimal so the number isn't negative and the problem works. – Mags Feb 17 '15 at 22:39
-
The problem is coming with referencing distance in the velocity function. – Mags Feb 17 '15 at 22:40
4 Answers
distance = None
def Distanceinput():
global distance
distance = eval(input('Enter a distance (in light years):'))
print("Velocity Relative time to reach", distance, "light years")

- 1,999
- 2
- 24
- 29
-
-
for whatever reason every time I try a global variable it doesn't work. I just tried again and nothing changed in the error output. – Mags Feb 17 '15 at 22:35
-
I pasted your Code and it works. There is just another error in the syntax of `print(velocityPercent, + "% of light", perceptualSpeed, "years.")` the `+` should be deleted. (You trying to add a string and a integer) – Finn Feb 17 '15 at 22:44
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. 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. 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. You haven't said anywhere in your function that you needed to access the variable distance
that has a value elsewhere in your program. 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! :)

- 4,908
- 5
- 42
- 66
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
andexec
can execute malicious code, so unless you are the only person using your program, try to avoid them.int
will work just fine for given problem.- I created a class, from which you can create different instances. Each time you create an instance you get to input the
distance
. \n
is the newline character.velocityPercent /= 100.
is equivalent to set velocityPercent to its previous value divided by 100. Note that its100.
with a dot, in case you are using python 2.7. Then i removed the/100000
inDilationFactor
, and100
inperceptualSpeed
.
One thing you could change is the output of 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()

- 5,370
- 8
- 47
- 75
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

- 347
- 4
- 15
-
how is that going to help me in the variable function with the syntax error that keeps coming up regarding python not understanding what distance is? – Mags Feb 17 '15 at 22:33