我的 Python for 循环执行 5005 次,但它意味着执行 100 次 [关闭]
Posted
技术标签:
【中文标题】我的 Python for 循环执行 5005 次,但它意味着执行 100 次 [关闭]【英文标题】:My Python for loop executes 5005 times but it is meant to execute 100 times [closed] 【发布时间】:2016-10-09 17:33:08 【问题描述】:我的输出是 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】:让我们简化您的程序以了解如何获得 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)。
所以,问题很简单:你没有在函数gencode()
的最开始清除gencodelist
。
【讨论】:
非常感谢!以上是关于我的 Python for 循环执行 5005 次,但它意味着执行 100 次 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章