2016-12-12 30 views
0

所以,我想創建一個在學校的一個項目密碼猜測器,我遇到的麻煩的代碼的繼承人的一部分:我是否正確地向變量添加數字?

dictfile = open('c:/ScienceFairDictionaryFolder/wordsEn.txt', 'r') 
DictionaryWords = dictfile.readlines() 


def Number_Finder(): 
    for x in DictionaryWords: 
     Intadd = 0 
     if x.replace("\n", " ") + str(Intadd) == Password: 
      print("Congrats, you found the password!") 
     else: 
      while Intadd <= 10: 
       Intadd +=1 
       print(x) 

Number_Finder() 

我試圖瞄準是的代碼是這個樣子:

Wildcat1 
Wildcat2 
Wildcat3 
Wildcat4 
Wildcat5 
Wildcat6 
Wildcat7 
Wildcat8 
Wildcat9 
Wildcat10 

然而,當我運行代碼,我得到這個:

Wildcat 
Wildcat 
Wildcat 
Wildcat 
Wildcat 
Wildcat 
Wildcat 
Wildcat 
Wildcat 
Wildcat 

我認爲這是與我如何試圖將英塔結合的問題DD與變量,我不確定。謝謝您的幫助!

+1

爲什麼許多神奇x'後'出現在'print'聲明,如果你不問它? – Julien

回答

1

我認爲你需要連接Intadd值到x

def Number_Finder(): 
    for x in DictionaryWords: 
     Intadd = 0 
     if x.replace("\n", " ") + str(Intadd) == Password: 
      print("Congrats, you found the password!") 
     else: 
      while Intadd <= 10: 
       Intadd +=1 
       print(x + str(Intadd)) 
0
dictfile = open('c:/ScienceFairDictionaryFolder/wordsEn.txt', 'r') 
DictionaryWords = dictfile.readlines() 


def Number_Finder(): 
    for x in DictionaryWords: 
     Intadd = 0 
     if x.replace("\n", " ") + str(Intadd) == Password: 
      print("Congrats, you found the password!") 
     else: 
      while Intadd <= 10: 
       Intadd +=1 
       print(x.replace("\n", " ") + str(Intadd)) 

Number_Finder() 
0

下面是一個交替服用,使用的溶液cool tool from itertools

from itertools import product 

def number_finder(): 
    for word, number in product(dictionary_words, range(11)): 
     test_password = ''.join((word.strip(), str(number))) 
     if test_password == password: 
      print('Congrats...') 
      break 
     else: 
      print(test_password)