0

I am trying to find the value of totalDistance, but when I type it at the bottom after returning the function: print(totalDistance) I get function is not define. Here is the entire piece I am working on. And what i am trying to get is the value totalDistance because I have to use it to represent the actual number value. Another question is how can I function ordinal so that it shows the "st" "nd" "rd" and "th" were I have str(s1). I am new to this and is hw so nothing fancy if you all may, basic.

i = 1
while i == 1:
    s1 = int(input("Starting street: "))
    while s1 % 2 == 0:
            print("Street must be a positive odd number between 1 and 99!")
            s1 = int(input("Starting street: "))
            if s1 <= 0:
                print("Street must be a positive odd number between 1 and 99!")
                s1 = int(input("Starting street: "))
            if s1 > 99:
                print("Street must be a positive odd number between 1 and 99!")
                s1 = int(input("Starting street: "))

    a1 = int(input("Starting avenue: "))
    while a1 % 2 != 0:
            print("Avenue must be a positive even number between 2 and 98!")
            a1 = int(input("Starting avenue: "))
            if a1 <= 1:
                print("Avenue must be a positive even number between 2 and 98!")
                a1 = int(input("Starting avenue: "))
            if a1 > 98:
                print("Avenue must be a positive even number between 2 and 98!")
                a1 = int(input("Starting avenue: "))

    s2 = int(input("Ending street: "))
    while s2 % 2 == 0:
            print("Street must be a positive odd number between 1 and 99!")
            s2 = int(input("Ending street: "))
            if s2 <= 0:
                print("Street must be a positive odd number between 1 and 99!")
                s2 = int(input("Starting street: "))
            if s2 > 99:
                print("Street must be a positive odd number between 1 and 99!")
                s2 = int(input("Starting street: "))

    a2 = int(input("Ending avenue: "))
    while a2 % 2 != 0:
            print("Avenue must be a positive even number between 2 and 98!")
            a2 = int(input("Ending avenue: "))
            if a2 <= 1:
                print("Avenue must be a positive even number between 2 and 98!")
                a2 = int(input("Starting avenue: "))
            if a2 > 98:
                print("Avenue must be a positive even number between 2 and 98!")
                a2 = int(input("Starting avenue: ")) 
    i = i + 1

def getDistance(s1, a1, s2, a2):
    streets = (s2 - s1)/2 * 1000
    if streets <= -1:
        import math
        streets = math.fabs(streets)

    avenues = (a2 - a1)/2 * 1000
    if avenues <= -1:
        import math
        avenues = math.fabs(avenues)

        totalDistance = streets + avenues

    return getDistance(s1, a1, s2, a2)

def ordinal(n):
    if n % 100/10 != 1:
        if n % 10 == 1:
            print(str(n) + "st")
        elif n % 10 == 2:
            print(str(n) + "nd")
        elif n % 10 == 3:
            print(str(n) + "rd")
        else:
            print(str(n) + "th")
    return ordinal       

def getDirections(s1, a1, s2, a2):
    print("Directions from " + str(s1) + " and " + str(a1) + " to " + str(s2) + " and " + str(a2))
    print("Total distance traveled :" + "ft")

getDirections(s1, a1, s2, a2)    
1
  • When I run your code, I get a NameError: s1 is not defined, and if I substitute some value for the four variables, your code runs perfectly. Commented Apr 26, 2014 at 0:15

4 Answers 4

1

The totalDistance variable only exists inside your getDistance function; you cannot see it from outside (this is known as the variable's scope).

Instead, return a value from the function like so:

def get_distance(s1, a1, s2, a2):
    streets = abs(s2 - s1) * 500
    avenues = abs(a2 - a1) * 500
    return streets + avenues      # function has to return the result

total_distance = get_distance(s1, a1, s2, a2)
Sign up to request clarification or add additional context in comments.

Comments

1

Any variable inside a function is considered a local variable, which means it cannot be accessed from outside the function. Instead, you can:

return totalDistance

or:

return streets + avenues

You could also try unindenting:

totalDistance = streets + avenues

Because it may be because it is inside the if statement.

Comments

0

Your function needs to return the distance that it calculated, eg,

return totalDistance

Be careful with the indentation on the last expression as well.

Comments

0

The error message indicates that s1 is not defined. When I look at your code, I don't see where s1 is defined (or s2 or a1 or a2 for that matter)

So when you say

getDistance(s1, a1, s2, a2)

You need to define those variables beforehand, eg:

s1 = 0
a1 = 1
s2 = 2
a2 = 3
getDistance(s1, a1, s2, a2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.