首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图在函数中返回要从另一个函数调用的变量

试图在函数中返回要从另一个函数调用的变量
EN

Stack Overflow用户
提问于 2016-04-22 10:10:02
回答 1查看 16关注 0票数 0

我正在尝试返回def "DMS_to_DD_calculations(x)“中的变量”DMS_to_DD_calculations“。

然后,我希望将该变量赋值给test (我尝试将它赋值为d),以便在def "test(d)“中使用decimal_degrees的值。

代码语言:javascript
复制
# -*- 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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-22 10:22:41

使用DMS_to_DD_calculations中的行DMS_to_DD_calculations,可以将decimal_degrees赋值给函数之外的变量(例如dec_degrees = DMS_to_DD_calculations(x))。

然后dec_degrees (或您命名的变量)可以传递到test函数中。

因为函数test没有返回值,所以行decimal_degrees = test(d)将返回None

另外,由于decimal_degrees不是全局变量,因此需要将其名称更改为d,就像test中的参数名称一样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36790909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档