0% found this document useful (0 votes)
4 views4 pages

Python Assignment

The document outlines a pseudocode and Python source code for a survey census program that collects household information. It includes functions for safe integer input and data collection for each household, storing individual details in a list, and saving the data to an Excel file. The program prompts users for household names, number of people, and individual details, then saves the compiled data to a specified file.

Uploaded by

hinguinuse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Python Assignment

The document outlines a pseudocode and Python source code for a survey census program that collects household information. It includes functions for safe integer input and data collection for each household, storing individual details in a list, and saving the data to an Excel file. The program prompts users for household names, number of people, and individual details, then saves the compiled data to a specified file.

Uploaded by

hinguinuse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Pseudocode

1. Start

2. Import necessary libraries:

❖ pandas
❖ openpyxl

3. Define a function `safe_input_int(prompt)`:

a. Loop indefinitely:

❖ Prompt the user with `prompt` to enter input.


❖ Try to convert the user input to an integer using `int()`.
❖ If successful, return the integer value.
❖ If conversion fails (ValueError), print an error message and continue looping.

4. Define the main function `survey_census()`:

a. Initialize an empty list `households_data` to store household information.

b. Loop indefinitely:

❖ Prompt the user to enter the household name.


❖ Use `safe_input_int()` to get the number of people in the household (`num_people`).
❖ Initialize an empty list `individuals_list` to store individuals' details for the current household
❖ Loop `num_people` times:

A. Prompt the user to enter the person's name.


B. Use `safe_input_int()` to get the person's age.
C. Prompt the user to enter the person's address.

D. Create a dictionary `person_details` with keys:

❖ 'Household Name': household_name


❖ 'Name': person_name
❖ 'Age': person_age
❖ 'Address': person_address

E. Append `person_details` to `individuals_list`.


❖ extend `households_data` with `individuals_list` to include all individuals' data for the current
household.

❖ Prompt the user if they want to add another household.


- If response is 'no', exit the loop.

❖ Convert `households_data` to a DataFrame `df` using pandas.

❖ Prompt the user to enter the file name (without extension) to save data.

❖ Add `.xlsx` extension to `file_name` if not already provided.

❖ Save `df` to an Excel file named `file_name.xlsx` using `df.to_excel()`.

❖ Print a success message indicating that data has been saved.

5.Call `survey_census()` to execute the survey.

6. End
Python Source Code
import pandas as pd

def safe_input_int(prompt):
while True:
user_input = input(prompt)
try:
value = int(user_input)
return value
except ValueError:
print("Error: Please enter a valid integer.")

def survey_census():
households_data = []

while True:
household_name = input("Enter household name: ")
num_people = safe_input_int("Enter number of people in the household: ")

individuals_list = []

for j in range(1, num_people + 1):


person_name = input(f"Enter name of person {j}: ")
person_age = safe_input_int(f"Enter age of person {j}: ")
person_address = input(f"Enter address of person {j}: ")

person_details = {
'Household Name': household_name,
'Name': person_name,
'Age': person_age,
'Address': person_address
}

individuals_list.append(person_details)

households_data.extend(individuals_list)

response = input("Do you want to add another household? (yes/no): ").lower()


if response != 'yes':
break

# Convert data to DataFrame


df = pd.DataFrame(households_data)
# Prompt user for file name to save data
file_name = input("Enter the file name to save data: ")

# Append .xlsx extension if not already provided


if not file_name.endswith('.xlsx'):
file_name += '.xlsx'

# Save data to Excel file


df.to_excel(file_name, index=False)

print(f"Data saved successfully to {file_name}")

# Call the survey_census function to execute the survey


survey_census()

You might also like