Linking python variables to a HTML script -


i've never written question here before, please bare me if i'm not entirely clear on i'm doing.

i've written python 3.2 script select 10 random questions out of list of 18, having extracted them sqlite 3 database. script works fine, want render using html - know how make question boxes in html, not how asked question print, in place of "question 1" placeholder.

html code below:

def printgame():     print( """     <html>     <div id = "textbody">             <h2>quiz</h2>                 <form method="post" action="quiz.cgi">                     <table border="0">                         <tr>                               <td>question 1:</td>                               <td><input type="text" name="a1"/></td>                         </tr>                         <tr>                               <td>question 2:</td>                               <td><input type="text" name="a2"/></td>                         </tr>                         <tr>                               <td>question 3:</td>                               <td><input type="text" name="a3"/></td>                         </tr>                         <tr>                               <td>question 4:</td>                               <td><input type="text" name="a4"/></td>                         </tr>                         <tr>                               <td>question 5:</td>                               <td><input type="text" name="a5"/></td>                         </tr>                         <tr>                               <td>question 6:</td>                               <td><input type="text" name="a6"/></td>                         </tr>                         <tr>                               <td>question 7:</td>                               <td><input type="text" name="a7"/></td>                         </tr>                         <tr>                               <td>question 8:</td>                               <td><input type="text" name="a8"/></td>                         </tr>                         <tr>                               <td>question 9:</td>                               <td><input type="text" name="a9"/></td>                         </tr>                         <tr>                               <td>question 10:</td>                               <td><input type="text" name="a10"/></td>                         </tr>                         <tr>                             <td><input type="submit" value="submit answers"></td>                         </tr>                     </table>                 </form>              </div>         </body>     </html>     """) 

just clarify, want replace "question n"-type things questions pulled sqlite 3 database. questions loaded qaskedn variables (where n replaced question number).

sounds want string formatting, flexible format string/variable.

i haven't checked, perhaps?

def printgame():     htmlquestions = []     question in qaskedn:         htmlquestions.append("""                         <tr>                               <td>{} :</td>                               <td><input type="text" name="a1"/></td>                         </tr>""".format(question))     html = ("""     <html>     <div id = "textbody">             <h2>quiz</h2>                 <form method="post" action="quiz.cgi">                     <table border="0">"""      + "".join(htmlquestions) +      """                </table>                 </form>              </div>         </body>     </html>     """) 

update: opening triple double quote last lines.


Comments