[英]How to return the for loop values in python and access them in other script
[英]Python - how to return values from function in other script
我想在腳本 MAIN.py 中返回一個值,其中該函數在另一個腳本中聲明? 我該怎么做呢?
在函數 SUB.py 中,我定義了:
def SUM_POWER(a,b):
c = a+b
d = a**b
return [c,d]
現在我想進入一個腳本 MAIN.py: c and d for a=3 and b=4。 我在 MAIN.py 中嘗試過:
import SUB
c,d = SUM_POWER(3,4)
我做錯了什么? 最好我想在 MAIN.py 中命名不同於 c,d 的變量。 所以例如out1,out2。 按照返回值的順序,out1 必須對應於 c,out2 對應於 d。
問題在於函數調用,以及您在嘗試將列表存儲在元組中時返回列表。
試試這個 SUB.py -
def SUM_POWER(a,b):
c = a+b
d = a**b
return c,d
主要.py -
import SUB
c,d = SUB.SUM_POWER(3,4)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.