不能使用另一个函数/ python文件的返回值

时间:2017-07-06 19:17:59

标签: python python-3.x return-value

我是python-ish的新手,并尝试使用另一个程序中select_target_server_type底部返回的服务器变量中保存的值。第一组代码是我的menu.py,它只是一个输入/决策树。

我的第二个代码是rest_engine.py,它调用了menu.start_program()并且效果很好但是当我想在其中一个菜单函数中检索函数的值时,我似乎无法使它工作。

def start_program():
    select_target_server_type()


def select_target_server_type():
    # SUPPORTED SERVERS FOR API CALLS
    # DISPLAYS A LIST TO USER TO CHOOSE FROM

    print("\nWelcome!\nThese are the supported systems, please select a value by entering the corresponding "
          "numbered value\n")

    while True:
        try:
            print("1. APIC-EM")
            print("2. ISE")
            print("3. CSR")
            print("4. Quit")
            server = int(input("\nPlease select a SYSTEM TYPE to continue:   "))

            if 0 >= server or server >= 5:
                print("\n***Incorrect Selection***\n\n")
                pass
            else:
                break

        except Exception as e:
            print("\n\n***oops***")
            sys.exit(1)

    while True:
        try:
            if server == 1:
                print("\nYou have selected APIC-EM \n")
                api_top_menu()
            elif server == 2:
                print("\nYou have selected ISE \n")
                api_top_menu()
            elif server == 3:
                print("\nYou have selected CSR \n")
                api_top_menu()
            elif server == 4:
                print("\nQUITTING APPLICATION\n")
                sys.exit(0)
        except Exception as e:
            print("\n\n***oops***")
            sys.exit(1)
        return server

第二个程序rest_engine.py(调用第一个)

#!/usr/bin/python3

import json
import requests
import menu


menu.start_program()

name = menu.select_target_server_type()
print(name)

运行代码输出:

> /usr/bin/python3.5 /home/danield/PycharmProjects/YODA/rest_engine.py
> 
> Welcome! These are the supported systems, please select a value by
> entering the corresponding numbered value
> 
> 1. APIC-EM
> 2. ISE
> 3. CSR
> 4. Quit
> 
> Please select a SYSTEM TYPE to continue:   1
> 
> You have selected APIC-EM 
> 
> 1. GET
> 2. PUT
> 3. DELETE
> 4. quit Please select a function to continue:   1
> 
> You chose to use GET
> 
> 1. INVENTORY
> 2. NETWORK DISCOVERY
> 3. TBD
> 4. Quit Please select the FUNCTION category:   1
> 
> You chose Inventory
> 
> 
> 
> ***oops***
> 
> Process finished with exit code 1

2 个答案:

答案 0 :(得分:1)

我认为这是你的问题:

发布所有代码!我们看不到显示其他列表(即函数和FUNCTION)列表的代码。我们如何帮助解决部分问题?更新你的帖子!

menu.start_program()被调用,在menu.py中,它会调用select_target_server_type()。但是,select_target_server_type()设置为return server,因此它会return sever到调用函数start_program()。但是,start_program()函数未经过编码,无法从函数调用return接收select_target_server_type()。但是,在它甚至return server之前,某个地方就会发现错误。但我们无法看到api_top_menu(),因此除非您更新帖子,否则我们无法为您提供帮助。

尝试以下更改:

<强> rest_engine.py

#!/usr/bin/python3

import json
import requests
import menu

name = menu.start_program()

print (name)

<强> menu.py

def start_program():
    server = select_target_server_type()


def select_target_server_type():
    # SUPPORTED SERVERS FOR API CALLS
    # DISPLAYS A LIST TO USER TO CHOOSE FROM

    print("\nWelcome!\nThese are the supported systems, please select a value by entering the corresponding "
          "numbered value\n")

    while True:
        try:
            print("1. APIC-EM")
            print("2. ISE")
            print("3. CSR")
            print("4. Quit")
            server = int(input("\nPlease select a SYSTEM TYPE to continue:   "))

            if 0 >= server or server >= 5:
                print("\n***Incorrect Selection***\n\n")
                pass
            else:
                break

        except Exception as e:
            print("\n\n***oops***")
            sys.exit(1)

    while True:
        try:
            if server == 1:
                print("\nYou have selected APIC-EM \n")
                api_top_menu() # we need to see this code too!
            elif server == 2:
                print("\nYou have selected ISE \n")
                api_top_menu()
            elif server == 3:
                print("\nYou have selected CSR \n")
                api_top_menu()
            elif server == 4:
                print("\nQUITTING APPLICATION\n")
                sys.exit(0)
            return server # will end the infinite try here
        except Exception as e:
            print("\n\n***oops***")
            sys.exit(1)

答案 1 :(得分:0)

一些事情。首先,似乎并非所有这些事情都需要在while循环中,但这是一个单独的问题。其次,当你执行menu.start_program()时,你正在运行你的函数select_target_server_type,但是你没有存储返回值。您只在再次调用整个事物时尝试存储返回值:name = menu.select_target_server_type()

但是你永远不会到达那一行(名字= ...),因为第一次调用start_program会终止正在运行的进程。

第三,你的程序遇到错误,因此通过sys.exit(1)行终止,所以函数永远不会有机会返回任何东西。