0
我的代碼有問題,它始終有錯誤'file' object has no attribute '__getitem__'
。這裏是我的代碼:'文件'對象沒有任何屬性'__getitem__'
def passHack():
import random
p1 = random.choice(["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"])
p2 = random.choice(["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"])
p3 = random.choice(["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"])
p4 = random.choice(["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"])
p = (p1 + p2 + p3 + p4)
g = 0000
a = 0
import time
start_time = time.time()
print "Working..."
while (g != p):
a += 1
f = open('llll.txt')
g = f[a]
print g
f.close()
if (g == p):
print "Success!"
print "Password:"
print g
print "Time:"
print("%s seconds" % (time.time() - start_time))
def loopPassHack():
t = 0
while (t <= 9):
passHack()
t += 1
print "\n"
passHack()
我注意到,這是發生在我加入g = f[a]
,但我嘗試添加屬性__getitem__
到g
,f
和a
,但它仍然會返回相同的錯誤。請幫助,並感謝您迴應!
我的你的標識符很短。我建議不要在任何地方使用一個字母的變量名稱,但這更適合編程實踐。 – 2014-12-08 02:29:00
順便說一句,作爲一般規則,你不應該在函數內部有'import'語句,如果你打算多次調用該函數,_especially_。另外,在Python中,你不需要圍繞條件表達式使用括號,例如'while(g!= p):'可以更清晰地寫爲'g!= p:' – 2014-12-08 05:34:55