我的Python for循环执行5005次,但它意味着执行100次

时间:2016-06-09 07:34:13

标签: python ranged-loops

我的输出是5005序列号,但我只想要100。 目前它将5005个序列号写入一个文件,我希望它能够写出100个序列号。这个程序将用于生成序列号,以便在另一个脚本中进行大多数安全登录请帮助:)

#!/usr/bin/python


#========Import Section========#
import random, hashlib, os
from sys import exit
from time import sleep
#========End Imports===========#
#====File paths for output=====#
database_check1 = os.path.expanduser('~/Codemaker/database.txt')
codemaker_dir_check1 = os.path.expanduser('~/Codemaker')
database_check = os.path.isfile(database_check1)
codemaker_dir_check = os.path.isdir(codemaker_dir_check1)
#====End File paths for output====#
#user import to ask if they would like to replace the file, quit or append more serial numbers to the database#
if codemaker_dir_check == True:
    pass
else:
    os.system('mkdir ~/Codemaker')

def choice():

    if database_check == True:
        replace_or_no = raw_input('Warning database.txt already exists do you want to relace it? y = yes n=no a = add more to end of file. y/n/a?: ')
        if replace_or_no == ('y'):
            os.system('rm ~/Codemaker/database.txt')
            os.system('> ~/Codemaker/database.txt')              

        elif replace_or_no == ('a'):
            pass
        elif replace_or_no == ('n'):
            print('We did not change the database :)')
            exit()

        else:
            print('An error has occured you probably pressed the wrong button if you wish to try again it should work')
            exit()
    else:
        os.system('> ~/Codemaker/database.txt')
#=============End user input==============# 
#=======Start variables=======#
Acceptable_letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
gencodelist = []
#=======End variables=======#
#=======Execute CLI======#
choice()
#=======End CLI======#
#=======path to database.txt=======#
database_file_append1 = os.path.expanduser('~/Codemaker/database.txt')
database_file_append = open(database_file_append1, 'a')
#=======End path to database.txt=======#


#=======Generate Serial Number========#
def gencode():
    for i in ('hih'):
        for i in ('hihi'):
            gencodelist.append(random.choice(Acceptable_letters))

        gencodelist.append('-')
    for i in ('hihi'):
        gencodelist.append(random.choice(Acceptable_letters))

    gencodelist.append('\n')
#======End Generate serial numbers======#
#=====write different serial numbers to file 100 times but it prints 5005 not what i want========#
for i in range(1, 100):
    gencode()
    gencodeout = ''.join(gencodelist)
    print(gencodeout)
    database_file_append.write(gencodeout)
#======End Write different serial numbers======#
#=====start end message=========#
message = ['100 codes have generated and been written to database.txt located in ', database_file_append1, '! :)']
finalmessage = "".join(message)
print(finalmessage)
#=====End end message=======#

1 个答案:

答案 0 :(得分:2)

让我们简化您的程序,以了解如何获得5005结果:

gencodelist = []
for i in range(1, 100):
  gencodelist.append(str(i) + ' ')
  gencodeout = ''.join(gencodelist)
  print(gencodeout)

这里我们将列表gencodelist中的元素添加99次(从1到99),并且每次都打印此列表的所有元素。所以,我们在输出中有1 + 2 + ... + 99个数字(事实上它是4950,而不是你的数字5005)。

所以,问题很简单:你没有在函数gencodelist的最开头清除gencode()