在打印语句中循环

2024-05-19 12:34:43 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

所以基本上这个print语句是在我的计算机上写入一个文件,但是我遇到了在这个print语句中包含另一个循环的问题,因为如果用户说有5个访谈,我需要5行,其中每行是列表中的一个元组,每列是该元组中的每一项(如果用户说有6个访谈,我需要5行)需要6行,以此类推)。有办法吗?你知道吗


Tags: inputrawtimestarttrareoutfiletd
1条回答
网友
1楼 · 发布于 2024-05-19 12:34:43

这能解决你的问题吗?循环只需要创建行,不应该将文档的开始和结束放在循环中。你知道吗

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”,这样就不需要任何计数器变量。你知道吗

相关问题 更多 >