简体   繁体   中英

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. 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...


>After user has entered info, program should create n HTML file, with the input, for a web page. Following is a sample HTML content for the above input.

<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 :

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.

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.

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. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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