尝试返回函数内的变量以从另一个函数调用

时间:2016-04-22 10:10:02

标签: function variables return

我正在尝试返回变量" decimal_degrees"在def" DMS_to_DD_calculations(x)"

然后我想将该变量分配给测试(我尝试将其指定为d)因此我可以在def" test(d)"

中使用decimal_degrees的值
# -*- coding: utf-8 -*-

def main():

    print "Welcome to the Forward and Back Bearing Calculator"
    print "This program displays the back bearing of a given bearing"
    print ""
    print "How to use:"
    print "Type in the value for the bearing then press enter"
    print ""
    print "Example:"
    print "To enter a bearing the following bearing 33°","15'",'22.5"',"would be entered as 33.15225"
    print ""
    print "The entered bearing will be converted to a back bearing and the result displayed"
    print ""

    user_input= float(raw_input("DMS bearing:"))

    DMS_to_DD_calculations(user_input)
    decimal_degrees = test(d)

def DMS_to_DD_calculations(x):
    #These 3 functions assign an individual value for the variables: Degrees (d), Minutes (m) and Seconds (s) for the given bearing
    # 1. Degrees is equal to the integer value of the user entered bearing
    d= int(x)

    # 2. Minutes:
    #       First - Find the user entered value minus the integer of that value times 100.
    #       Second - The integer of THAT value is the minutes
    no_int_m= (x-int(x))*100
    int_m= int(no_int_m)
    m= int_m

    # 3. Seconds:
    #       First -  Find the user entered value minus the integer of that value times 100
    #       Second - Minus the integer of THAT value is the seconds
    s= (no_int_m-int_m)*100

    decimal_degrees= (((s/60)+m)/60)+d

    print ""
    print "DD BEARING =", decimal_degrees,"°"
    return decimal_degrees

def test(d):
    print "yes"
    if decimal_degrees > 0:
        print "success"

main()

1 个答案:

答案 0 :(得分:0)

使用return decimal_degrees中的行DMS_to_DD_calculations,您可以将decimal_degrees分配给该功能之外的变量(例如dec_degrees = DMS_to_DD_calculations(x)

然后dec_degrees(或你命名变量的任何东西)可以传递给函数test

由于函数test没有返回值,因此行decimal_degrees = test(d)将返回None

另外,因为decimal_degrees不是全局变量,所以需要将其名称更改为dtest

中的参数名称也是如此