[英]How to use a variable from def
例子
G1 = 500
G2 = 400
avg1 = 25
avg2 = 20
def produce(x):
production_time = Gx /avgx
return production_time
print (produce(1))
#out: 20
我想要做的是將 G 和 x 結合起來,以便我可以在頂部使用我的變量 G1。
看來您正在尋找這個:
如果我理解正確你想訪問一個值為 x 的變量,那么聲明它為Dictionary
將最適合你
G = { 1: 500 }
avg = 25
def produce(x):
production_time = G[x] / avg
return production_time
print (produce(1))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.