0

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>
SMc
  • 15
  • 1
  • 1
  • 6
  • 1
    Possible duplicate of [Correct way to write line to file?](https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file) – James Apr 30 '19 at 19:59

2 Answers2

0

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.

Amr Bashir
  • 103
  • 1
  • 2
  • 12
  • Thank you...will try this method once I have all the other functions and their jobs in this program understood, and I was unclear if I could pass more than two arguments. Who knew...? – SMc Apr 30 '19 at 20:45
0

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>
sykezlol
  • 88
  • 9
  • Appreciated your help. Still learning and like to see the way each function plays a role in making the program run...Know in the future make the programs smaller. – SMc Apr 30 '19 at 20:42