我在弄清楚如何将z == 2时返回的值复制到循环函数中的z == 3 elseif时遇到问题。我认为它与以下事实有关:当我在z == 3中打印行时,我再次调用行函数导致生成随机id。
import datetime
import random
import string
def date():
date1 = datetime.date.today()
return date1.strftime('%Y%m%d')
def loops():
# User defined input
# new file create
import os
os.path.abspath('fdc/')
filename = str(prefix1 + date() + '.txt')
# row loop
z = int(0)
with open(filename, 'w') as f:
# howmany variable no longer exists because there will be 15 lines - 1 for each scenario
while z < 3:
if z == 1:
row()
elif z == 2:
row2 = str(row())
row3 = row2
elif z == 3:
print row3
else:
row()
f.write(str(row()))
z += 1
f.write(str(trailer()))
f.close()
def rand_char(y):
return ''.join(random.choice(string.ascii_lowercase) for x in range(y))
def trailer():
trailer1 = str("X").ljust(20, "X") + str("0").ljust(8, "0") + date().ljust(8, " ") + str(" ").ljust(413, " ") + \
str('\n').ljust(1, " ")[:1]
# print trailer1
return trailer1
# End of line 1 - ASCII 0A {LF}
eoline = str('\n').ljust(1, " ")[:1]
def row():
# FDI Internal Merchant ID Generation
fdimerch_id = ((prefix1 + date() + str(random.randint(0, 50000)) + str(rand_char(10))).rjust(20, " "))[:20]
row1 = fdimerch_id + eoline
return row1
prefix1 = str(raw_input("Enter a prefix:"))
email = str(raw_input("Enter an email: "))
loops()