2017-07-06 49 views
0

我是python-ish的新手,並嘗試使用在另一個程序的select_target_server_type底部返回的服務器變量中保存的值。第一組代碼是我的menu.py,它只是一個輸入/決策樹。不能使用來自另一個函數/ python文件的返回值

我的第二個代碼是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 
+0

您是否已經驗證你實際上打的是return語句和它的非-空值? – Araymer

+0

當我學習c#時,我知道如何通過一個程序,但用python,我還沒有想出如何做到這一點。我不確定如何驗證。 – Dan

+1

不確定它是否與您的問題直接相關,但在發生異常時,打印更多信息而不僅僅是「oops」會很有用。或者讓錯誤不被捕獲,以便在崩潰期間獲得完整的診斷信息。如何在沒有堆棧跟蹤或錯誤消息的情況下調試問題? – Kevin

回答

0

有幾件事情。首先,似乎並非所有這些東西都需要在while循環中,但這是一個單獨的問題。其次,在執行menu.start_program()時,您正在運行函數select_target_server_type,但不存儲返回值。你只嘗試存儲返回值,當你再次調用整個事情:name = menu.select_target_server_type()

但是你永遠不會到那條線(與名稱= ...),因爲第一次調用,以start_program ,終止正在運行的進程。第三,你的程序遇到錯誤,並因此通過sys.exit(1)行終止,所以函數永遠不會有機會返回任何東西。

1

我認爲這是你的問題:

發表你的所有代碼!我們看不到顯示其他列表(即功能和功能)列表的代碼。我們如何幫助解決部分問題?更新你的文章!

menu.start_program()被調用,並且在menu.py中它調用select_target_server_type()。但是,select_target_server_type()設置爲return server,所以它會return sever到調用函數start_program()。但是,start_program()函數未編碼爲從其函數調用接收returnselect_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) 
相關問題