0

I am trying to create code with python that will ask the user for a first and last name, address, city, state, and zip code followed by a paragraph that describes the user. With this information, the program should generate an HTML file. The HTML file only needs to be created in my files not immediately pulled up in the web browser. The file should also end up named as the user's last name.

name = input("Please enter your full name (First Last): ")
address = input("Please enter your address: ")
area = input("Please enter your city, state and zipcode: ")
describe = input("Describe yourself: ")
person_html = open('personal.html', 'w')

person_html.write('''<!DOCTYPE html>
<html> 
  <head> 
  <title>My Personal Web Page</title> 
    <style> 
      .center {text-align: center;} 
      .blue   {color: blue;} 
      footer  {text-align: center; font-style: italic;} 
      h1, h3  {margin: 0;} 
    </style> 
  </head> 
  <body> 
    <div class="center"> 
      <h1 class="blue">(name) </h1> 
      <h3> (address) </h3> 
      <h3> (area) </h3> 
    </div> 
    <hr> 
    <div class="center"> 
 (describe)
    </div> 
    <hr> 
    <footer>
      (Date)
    </footer> 
  </body> 
</html>''')

person_html.close()
  

The HTML code above was what I was given to work with I am more so confused about how to get my inputs placed into the HTML code itself. I put () around the variables in the HTML code just to remind myself where each input goes. As it is right now the program just asks for the inputs and stops. Earlier before I even entered the inputs I was able to get it to save as a HTML in my files but now it just saves as a python file. Also, I do not know how to get the file name to change based on the user's last name.

Natawee
  • 17
  • 8
  • "Also, I do not know how to get the file name to change based on the user's last name." Well, what is the part of the code that opens the file? How does that part of the code, decide the file name that it will use? Can you write code that tells you the name of the file that you want to use? What if you try using that code, in the place where the file name is chosen? – Karl Knechtel Oct 04 '22 at 06:34

1 Answers1

1

Use an f-string to interpolate the variables into the HTML.

I also

  • moved the CSS to a separate variable because it'd otherwise need awkward double {{ everywhere (so as to escape the { and } used by f-strings).
  • switched to with for managing the file.
name = input("Please enter your full name (First Last): ")
address = input("Please enter your address: ")
area = input("Please enter your city, state and zipcode: ")
describe = input("Describe yourself: ")

style = """
.center {text-align: center;} 
.blue   {color: blue;} 
footer  {text-align: center; font-style: italic;} 
h1, h3  {margin: 0;} 
"""

with open("personal.html", "w") as person_html:
    person_html.write(
        f"""<!DOCTYPE html>
<html> 
  <head> 
  <title>My Personal Web Page</title> 
    <style> 
      {style}
    </style> 
  </head> 
  <body> 
    <div class="center"> 
      <h1 class="blue">{name}</h1> 
      <h3> {address} </h3> 
      <h3> {area} </h3> 
    </div> 
    <hr> 
    <div class="center"> 
{describe}
    </div> 
    <hr> 
    <footer>
      (Date)
    </footer> 
  </body> 
</html>"""
    )
AKX
  • 152,115
  • 15
  • 115
  • 172