在打印语句中循环
Loop within a print statement
import sys
outfile = open( r'/Users/x/Desktop/myDoc.txt', 'w' )
i = 0
lis = []
n = int(raw_input("How many interviews are there? "))
while n:
i += 1
istart = raw_input("Interview Start Time: ")
iend= raw_input("Interview End Time: ")
ipeople= raw_input("What are the interviewer names: ")
itype= raw_input("What is the interview type: ")
lis.append((istart, iend, ipeople, itype))
n-=1
a = "<html><head></head><body><TABLE border=1><TR> </TR> <TR>\
<TH>Start</TH>\
<th>End</th>\
<th>People</th>\
<TH>Interview Type</TH></TR><TR ALIGN=CENTER></TR> \
<td>fff</td>dd<td>dddd</td><td>ddd</td><td>ddddd</td></TABLE></body></html>"
outfile.write(a)
outfile.close()
所以基本上这个打印语句正在写入我计算机上的文件,但我 运行 遇到了在这个打印语句中包含另一个循环的问题,因为如果用户说有 5 次采访,我需要 5 行其中每一行是列表中的一个元组,每一列是该元组中的每个项目(如果用户说 6 次采访,我需要 6 行,依此类推)。有办法吗?
这是否解决了您的问题?循环只需要创建行,你不应该把文档的开始和结束放在循环中。
import sys
outfile = open( r'/Users/x/Desktop/myDoc.txt', 'w' )
i = 0
lis = []
n = int(raw_input("How many interviews are there? "))
table = "<html><head></head><body><TABLE border=1><TR>\
<TH>Start</TH>\
<th>End</th>\
<th>People</th>\
<TH>Interview Type</TH></TR><TR ALIGN=CENTER></TR> \
%s</TABLE></body></html>"
while n:
i += 1
istart = raw_input("Interview Start Time: ")
iend= raw_input("Interview End Time: ")
ipeople= raw_input("What are the interviewer names: ")
itype= raw_input("What is the interview type: ")
lis.append("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % (istart, iend, ipeople, itype))
n-=1
outfile.write(table % ''.join(lis))
outfile.close()
您可以使用 range(n) 而不是 "while n",这样您就不需要任何计数器变量了。
import sys
outfile = open( r'/Users/x/Desktop/myDoc.txt', 'w' )
i = 0
lis = []
n = int(raw_input("How many interviews are there? "))
while n:
i += 1
istart = raw_input("Interview Start Time: ")
iend= raw_input("Interview End Time: ")
ipeople= raw_input("What are the interviewer names: ")
itype= raw_input("What is the interview type: ")
lis.append((istart, iend, ipeople, itype))
n-=1
a = "<html><head></head><body><TABLE border=1><TR> </TR> <TR>\
<TH>Start</TH>\
<th>End</th>\
<th>People</th>\
<TH>Interview Type</TH></TR><TR ALIGN=CENTER></TR> \
<td>fff</td>dd<td>dddd</td><td>ddd</td><td>ddddd</td></TABLE></body></html>"
outfile.write(a)
outfile.close()
所以基本上这个打印语句正在写入我计算机上的文件,但我 运行 遇到了在这个打印语句中包含另一个循环的问题,因为如果用户说有 5 次采访,我需要 5 行其中每一行是列表中的一个元组,每一列是该元组中的每个项目(如果用户说 6 次采访,我需要 6 行,依此类推)。有办法吗?
这是否解决了您的问题?循环只需要创建行,你不应该把文档的开始和结束放在循环中。
import sys
outfile = open( r'/Users/x/Desktop/myDoc.txt', 'w' )
i = 0
lis = []
n = int(raw_input("How many interviews are there? "))
table = "<html><head></head><body><TABLE border=1><TR>\
<TH>Start</TH>\
<th>End</th>\
<th>People</th>\
<TH>Interview Type</TH></TR><TR ALIGN=CENTER></TR> \
%s</TABLE></body></html>"
while n:
i += 1
istart = raw_input("Interview Start Time: ")
iend= raw_input("Interview End Time: ")
ipeople= raw_input("What are the interviewer names: ")
itype= raw_input("What is the interview type: ")
lis.append("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % (istart, iend, ipeople, itype))
n-=1
outfile.write(table % ''.join(lis))
outfile.close()
您可以使用 range(n) 而不是 "while n",这样您就不需要任何计数器变量了。