我是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
您是否已經驗證你實際上打的是return語句和它的非-空值? – Araymer
當我學習c#時,我知道如何通過一個程序,但用python,我還沒有想出如何做到這一點。我不確定如何驗證。 – Dan
不確定它是否與您的問題直接相關,但在發生異常時,打印更多信息而不僅僅是「oops」會很有用。或者讓錯誤不被捕獲,以便在崩潰期間獲得完整的診斷信息。如何在沒有堆棧跟蹤或錯誤消息的情況下調試問題? – Kevin