如果

2017-09-16 97 views
-1

我有簡單的代碼在功能unresolver參考。我有問題:「解析參考」一個'更多...「。 在第三個如果功能發生此問題。如果

enter image description here

我必須聲明它的ABCD函數之外,否則在同時每個時間起作用的將根據是否被設置爲我聲明的值,而不是。怎麼做?

def abcd(s, e): 
if s<0.72: 
    if e>30: 
    a=0 
    return a 

else: 
    a=0 
    return a 
else: 
    if a == 1: 
     a = 1 
     return a 
    else: 
     a=1 
     return a 
while True: 
abcd 
+2

請解決您的壓痕。 –

+1

Python **沒有變量聲明**。如果您希望函數將'a'視爲全局'a',則可以在函數中使用'global a'指令,否則,由於您指定了'a',編譯器會將'a'標記爲* local * 。 –

+0

[在創建它們的函數中使用全局變量]的可能重複(https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-一個 - 創建它們) – wwii

回答

0

可以在ABCD的開頭設置= 0,像這樣:

def abcd(s, e): 
    a = 0 
    if s<0.72: 
     if e>30: 
      a=0 
      return a 
     else: 
      a=0 
      return a 
    else: 
     if a == 1: 
      a = 1 
      return a 
     else: 
      a=1 
      return a 
while True: 
    s = int(input("type value for s ")) 
    e = int(input("type value for e ")) 
    print(abcd(s, e) ) 

但是可以將其簡化:

def abcd(s, e): 
    if s<0.72: 
     return 0 
    return 1 

while True: 
    s = int(input("type value for s")) 
    e = int(input("type value for e")) 
    print(abcd(s, e) ) 
+0

不,我沒有。無論e(e> 30還是不),a都設爲0並返回。看看第一個代碼塊 –

+0

啊當然 –