Python Code 2
Python Code 2
Practical 4
12/04/2024
Suppose you are approached by the Namibia Statistics Agency to develop a census
system that captures the names, ages, addresses of a household and further gives
the total number of people living in that specific house. Provide the solution to
1. Pseudocode (5 marks)
Pseudocode
1. Start
❖ pandas
❖ openly
a. Loop indefinitely:
b. Loop indefinitely:
❖ Prompt the user to enter the household name.
❖ Initialize an empty list `individuals_list` to store individuals' details for the current household
❖ 'Name': person_name
❖ 'Age': person_age
❖ 'Address': person_address
❖ extend `households_data` with `individuals_list` to include all individuals' data for the current
household.
❖ Prompt the user to enter the file name (without extension) to save data.
6. End
2. Flowchart (6 marks)
3. Python source code (7 marks)
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
# 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'
Submit your practical document with your full name and student number via
Moodle