simple Python code for a "Contact Us" page that saves user's input into a CSV file:
Code:
import csv
def save_contact(name, email, message):
with open('contacts.csv', mode='a', newline='') as contacts_file:
fieldnames = ['name', 'email', 'message']
writer = csv.DictWriter(contacts_file, fieldnames=fieldnames)
writer.writerow({'name': name, 'email': email, 'message': message})
print('Thank you for contacting us!')
def contact_us():
print('--- Contact Us ---')
name = input('Name: ')
email = input('Email: ')
message = input('Message: ')
save_contact(name, email, message)
contact_us()
Explanation:
The save_contact() function takes three parameters: name, email, and message. It opens a CSV file named contacts.csv (if it doesn't exist, it creates one) and appends a new row with the values of the parameters.
The contact_us() function prompts the user to input their name, email, and message, and calls the save_contact() function with those values.
Finally, the contact_us() function is called to start the process of capturing user inputs.
Note: This code assumes that the CSV file (contacts.csv) is saved in the same directory as the Python script. You can change the file name and/or path as per your requirement.

0 Comments