函数没有在python中定义

时间:2014-04-26 00:11:33

标签: python

我试图找到totalDistance的值,但是当我在底部键入它之后 返回函数:print(totalDistance)我得到的函数没有定义。这里是 我正在做的整件作品。我想要得到的是值totaldistance 因为我必须用它来表示实际的数值。另一个问题是我如何运作顺序,以便它显示" st" " ND" " RD"和" th"我有str(s1)。我是新手,并且如果你们都可以,那就没什么特别的了。

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)    

4 个答案:

答案 0 :(得分:1)

totalDistance变量仅存在于getDistance函数中;你不能从外面看到它(这被称为变量的范围)。

相反,从函数中返回一个值,如下所示:

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)

答案 1 :(得分:1)

函数内的任何变量都被视为局部变量,这意味着无法从函数外部访问它。相反,你可以:

return totalDistance

或:

return streets + avenues

你也可以尝试unindenting:

totalDistance = streets + avenues

因为它可能是因为它在if语句中。

答案 2 :(得分:0)

您的功能需要返回其计算的距离,例如

return totalDistance

还要注意最后一个表达式的缩进。

答案 3 :(得分:0)

错误消息表明s1 is not defined。当我查看您的代码时,我看不到s1的定义位置(或s2a1a2

所以当你说

getDistance(s1, a1, s2, a2)

您需要事先定义这些变量,例如:

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