python:用python函数编写HTML。syntaxError:预期的b

2024-05-14 07:35:39 发布

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

我试图在python函数中生成一个html复选框。这是我的密码

import HTML 
def CreateEvent(str):
"This prints a passed string into this function"
   print str;
   return;

CreateEvent ('''
content-type: text/html 

 <html> 
 <head> 
 <title> the list of all possible events that can be notified by our system </title> 
 </head> 
 <body> 
<form>
<input type="checkbox" name="tsunami" value="tsunami">tsunami<br>
<input type="checkbox" name="earthquake" value="earthquake">earthquake<br>
<input type="checkbox" name="volcano" value="volcano">volcano<br>
<input type="checkbox" name="hurricane" value="hurricane">hurricane<br>
<input type="checkbox" name="sinkholes" value="sinkholes">sinkholes<br>
<input type="checkbox" name="tornado" value="tornado">tornado<br>
<input type="checkbox" name="landslide" value="landslide">landslide<br>
<input type="checkbox" name="downburst" value="downburst">downburst<br>
</form>

<input type="submit" value="Submit">
</body>
</html>
''')

但是当我试图编译它时,它给出了一个语法错误:需要一个预期的块。请告诉我如何解决这个问题。在


Tags: namebrinputvaluehtmltypetornadocheckbox
3条回答

你在评论中没有缩进。在

注意:注释是"""而不是{}

import HTML 
def CreateEvent(str):
    """This prints a passed string into this function"""
    #^ Indentation space needed here - Also docstrings are represented with """ and not "
    print str
    #Good practice to have 4 spaces as indentation.
    return
    #     ^ No need of ;

一个好主意是安装pep8python库并执行

^{pr2}$

如果有问题的话

您需要正确缩进:

def CreateEvent(str):
    "This prints a passed string into this function"
    ...

…这应该是一个三重引号的字符串as per PEP8

…不要在行尾加分号

错误消息的名称将提示您问题所在。在

您没有正确地缩进,因此得到“syntaxError:expected anindented块”。在

另外,在Python中,行不以分号结尾,这不是C#。在

我建议读一个基本的Python guide和一个Python tutorial

相关问题 更多 >

    热门问题