简体   繁体   English

无法使用另一个函数/ python文件中的返回值

[英]Cant use a return value from another function/python file

I am new to python-ish and trying to user the value held in the server variable that is returned at the bottom of select_target_server_type in another program. 我是python-ish的新手,正在尝试使用服务器变量中包含的值,该变量在另一个程序的select_target_server_type底部返回。 The first set of code is my menu.py and its just an input/decision tree. 第一组代码是我的menu.py,它只是一个输入/决策树。

My second code is rest_engine.py which calls menu.start_program() and that works great but when I want to retrieve the value of a function in one of the menu functions I cant get it to work it seems. 我的第二个代码是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

Second program rest_engine.py (calls the first) 第二个程序rest_engine.py(调用第一个)

#!/usr/bin/python3

import json
import requests
import menu


menu.start_program()

name = menu.select_target_server_type()
print(name)

Functioning Code Output: 功能代码输出:

> /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

I think this is your problem: 我认为这是您的问题:

Post all your code! 发布所有代码! We cannot see the code that displays the other lists (ie the function and FUNCTION) lists. 我们看不到显示其他列表(即函数和FUNCTION)列表的代码。 How can we help with a partial problem? 我们如何为部分问题提供帮助? Update your post! 更新您的帖子!

menu.start_program() is called, and in menu.py it calls select_target_server_type() within it. menu.start_program()被调用,并在menu.py调用select_target_server_type()内它。 However, select_target_server_type() is set to return server , so it will return sever to the calling function start_program() . 但是, select_target_server_type()设置为return server ,因此它将return server return sever给调用函数start_program() However, the start_program() function isn't coded to receive a return from its function call to select_target_server_type() . 然而, start_program()函数不编码接收return从函数调用select_target_server_type() However, before it can even return server an error is being caught somewhere. 但是,在它甚至不能return server之前,就会在某个地方捕获到错误。 But we cant see api_top_menu() so we can't help you there unless you update your post. 但是我们看不到api_top_menu()因此除非您更新您的文章,否则我们将无法为您提供帮助。

Try these changes: 尝试以下更改:

rest_engine.py rest_engine.py

#!/usr/bin/python3

import json
import requests
import menu

name = menu.start_program()

print (name)

menu.py 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)

A few things. 一些东西。 First, seems like not all of those things need to be in while loops, but that's a separate issue. 首先,似乎并非所有这些事情都需要在while循环中进行,但这是一个单独的问题。 Second, when you do menu.start_program(), you're running your function select_target_server_type, but you don't store the return value. 其次,当您执行menu.start_program()时,您正在运行函数select_target_server_type,但不存储返回值。 You only try to store the return value when you call the whole thing again: name = menu.select_target_server_type() 您只有在再次调用整个对象时才尝试存储返回值:name = menu.select_target_server_type()

But you never get to that line (with name=...), because the first call, to start_program, terminates the running process. 但是您永远都不会到达该行(使用name = ...),因为对start_program的第一次调用会终止正在运行的进程。

Third, your program is running into an error and thus terminating via the sys.exit(1) line, so the function never gets the chance to return anything. 第三,您的程序遇到错误,因此通过sys.exit(1)行终止,因此该函数永远没有机会返回任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM