简体   繁体   English

最新错误= AttributeError:'str'对象没有属性'write'

[英]Latest error = AttributeError: 'str' object has no attribute 'write'

Program is still a mess and giving me lots of errors....sadly after many, many tries. 程序仍然是一团糟,给我很多错误....遗憾的是经过许多尝试。

Tried changing file.write to f.write...after reading some other solutions. 尝试将file.write更改为f.write ...之后阅读其他一些解决方案。 But know I am wrong. 但是知道我错了。

f = 'file'

def main():
    statement
    statement
    statement
    statement
    statement

def write_html_file():
   statement
    statement
    statement
    statement
    statement

def write_html_head():
    statement
    statement
    statement
    statement

def write_html_body():
    statement
    statement
    statement
    statement
    statement
   print('Your web page is done')   

main()

The following is a sample of what the user should see: 以下是用户应该看到的示例:

Enter your name: First Last Describe yourself: I am trying to be a great python student...just not there yet... 输入你的名字:First Last描述一下自己:我想成为一名优秀的蟒蛇学生......就在那里......


>After user has entered info, program should create n HTML file, with the input, for a web page. >用户输入信息后,程序应为网页创建带有输入的n个HTML文件。 Following is a sample HTML content for the above input. 以下是上述输入的示例HTML内容。

<html>
<head>
</head>
<body>
   <center>
      <h1> Firs Last </h1>
   </center>
   <hr />
   I am trying to be a great python student...just not
there yet...
   <hr />
</body>
</html>

remove the global file variable it is useless and for other functions you should pass a parameters that indicates the file , name and description like this : 删除它没用的全局file变量,对于其他函数,你应该传递一个指示filenamedescription的参数,如下所示:

def main():
    name = input("Enter your name: ")
    description = input("Describe yourself: ")

    f = open("my_page.html", "w")
    write_html_file(f, name, description)
    f.close()


def write_html_file(f, name, description):
    f.write(
        f"<html>\n<head>\n</head>\n<body>\n<center>\n<h1>{name}</h1>\n</center>\n<hr />{description}<hr />\n</body>\n</html>"
    )


main()

this should fix your attribute error 这应该修复你的属性错误

the html code could've been written in a better way but this will work and you don't need a several functions to write to a file just make one only. html代码可以用更好的方式编写,但这样可以工作,你不需要几个函数来写一个文件只做一个。

you could split each line in a different write statement but make sure to use formatting to provide the name and description and that can be done with f at the beginning of a string or with .foramt() method. 您可以在不同的write语句中拆分每一行,但请确保使用格式提供名称和描述,并且可以使用字符串开头的f.foramt()方法完成。

You need to make your variables accessible from the other functions. 您需要从其他函数访问变量。 If we declare f outside the main method, we are able to use the f.write on the other functions as well. 如果我们在main方法之外声明f,我们也可以在其他函数上使用f.write。 Your code will run like this: 您的代码将运行如下:

f = 'file'
name=input('Enter your name: ')
description=input('Describe yourself: ')

f = open('my_page.html','w')

def main():
   write_html_file()
   f.close()

def write_html_file():
   f.write('<html>\n')
   f.write('<head>\n')
   f.write('<body>\n')
   f.write('</html>\n')



def write_html_head():
   f.write('<head>\n')
   f.write('<title>My Personal Web Page</title>')
   f.write('/<head>\n')


def write_html_body():
   f.write('<body>\n')
   f.write('\t<center>\n')
   f.write('\t\t<h1>'+name+'</h1>\n')
   f.write('\t<hr />\n')
   f.write(description+'\n')
   f.write('\t<hr />\n')
   f.write('\t</center>\n')
   f.write('</body>\n')
   print('Your web page is done')   

main()

Your output looks like this when running: 运行时输出如下所示:

Enter your name: Sykezlol
Describe yourself: I like python
Your web page is done


<html>
<head>
<body>
</html>
<body>
    <center>
        <h1>Sykezlol</h1>
    <hr />
I like python
    <hr />
    </center>
</body>
<head>
<title>My Personal Web Page</title>/<head>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM