Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding user generated input into HTML file on python

I am taking a beginning python class. Here is the question:

"Write a program that asks the user for his or her name, then asks the user to enter a sentence that describes himself or herself. Here is an example of the program’s screen:

Enter your name: Julie Taylor Enter

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Describe yourself: I am a computer science major, a member of the
Jazz club, and I hope to work as a mobile app developer after I
graduate. Enter

Once the user has entered the requested input, the program should create an HTML file,
containing the input, for a simple Web page."

This is the code I have so far.

# Collect user data.
name = input("Enter your name: ")
content = input("Describe yourself: ")

# Create a file object.
f = open("program6.html", "w")


# Adding input data to the HTML file
f.write("<html>\n<head>\n<title> \nOutput Data in an HTML file \
        </title>\n</head> <body><h1>name</h1>\
        \n<h2>content</h2> \n</body></html>")

# Create a string to store the html script.
data = '''
<html>
<head>
</head>
<body>
   <center>
      <h1>name</h1>
   </center>
   <hr />
   {content}
   <hr />
</body>
</html>'''

            
# Saving the data into the HTML file
f.close()

I am struggling because when it creates the webpage, it inputs the words "name" and "content" versus utilizing the user input. How can I insert the users responses?

Thanks!

>Solution :

place name and content in curly brackets and then use .format() or a leading f to format your string.

f.write(f"<html>\n<head>\n<title> \nOutput Data in an HTML file \
        </title>\n</head> <body><h1>{name}</h1>\
        \n<h2>{content}</h2> \n</body></html>")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading