2014-03-31 141 views
-1

下面援助是我的代碼部分:蟒蛇 - 與計數循環

if again(): 
     print ('%s T: %s') % (m, hsh) 
     count = 1 
     m = 0.001 
     amount = m/0.01 
     amount = int(amount) 
     print ('Betting %s m') % m 
     apply(amount, int(count)) 
    else: 
     print ('%s T: %s') % (m, hsh) 
     try: 
      count = count * 2 
     except: 
      count = 1 
      count = count * 2 

     print count  
     m = 0.001 * count 
     amount = m/0.01 
     amount = int(amount) 
     print ('K %s m') % m 
     apply(amount, int(count)) 

如果函數again()返回true,什麼是suppost做的部分是打印計數,這是始終設置爲1,如果again()是真的。

如果返回false,它打印數* 2

如果它返回真3倍,它打印

1 
1 
1 

如果返回false 3倍,它打印

2 
4 
8 

但是,它只是打印

2 
2 
2 

如果第一次返回false,try除了未分配的變量錯誤。

我不指定變量數甚至在其他地方使用它。

+2

聽起來像一個範圍的問題,可能是。你是否嘗試使用超出範圍的變量? – ThorSummoner

+0

所有的變量都在那裏創建 – user3412816

回答

1

我不知道你的代碼的其餘部分是什麼樣子,但count應該是一個全局變量,你用try/catch塊的破解是非常糟糕的做法和非標準。

count一個全局變量,然後你可以重構你的if/else的功能,只是調用:

count = 1 

# the rest of your code, I'm guessing some loop 

def my_function(m, hsh): 
    if again(): 
     print ('%s T: %s') % (m, hsh) 
     count = 1 
     m = 0.001 
     amount = m/0.01 
     amount = int(amount) 
     print ('Betting %s m') % m 
     apply(amount, int(count)) 
    else: 
     print ('%s T: %s') % (m, hsh) 
     count = count * 2 
     print count  
     m = 0.001 * count 
     amount = m/0.01 
     amount = int(amount) 
     print ('K %s m') % m 
     apply(amount, int(count)) 
+0

這個修復了它,謝謝。 – user3412816

1

您需要定義count外面的if/else語句的。

count = 1 #This could be defined outside the method or class and accessed with global 


global count 
if again(): 
     print ('%s T: %s') % (m, hsh) 
     count = 1 #this will access the count outside the if 
     m = 0.001 
     amount = m/0.01 
     amount = int(amount) 
     print ('Betting %s m') % m 
     apply(amount, int(count)) 
    else: 
     print ('%s T: %s') % (m, hsh) 
     count = count * 2 

     print count  
     m = 0.001 * count 
     amount = m/0.01 
     amount = int(amount) 
     print ('K %s m') % m 
     apply(amount, int(count)) 

不知道正是你正在嘗試做的