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

Python Code 2

The document outlines a practical assignment for a programming fundamentals course, requiring students to develop a census system for the Namibia Statistics Agency. It includes instructions for creating pseudocode, a flowchart, and Python source code to capture household data such as names, ages, and addresses. The assignment is due on April 17, 2024, and must be submitted via Moodle.

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 views5 pages

Python Code 2

The document outlines a practical assignment for a programming fundamentals course, requiring students to develop a census system for the Namibia Statistics Agency. It includes instructions for creating pseudocode, a flowchart, and Python source code to capture household data such as names, ages, and addresses. The assignment is due on April 17, 2024, and must be submitted via Moodle.

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/ 5

Student name: NANGUNDA LUCIA T

Student no: 224019791


Module name: PROGRAMMING FUNDAMENTALS 1
Module code: S3531DP
Assignment no: 4
S3531DP/CMP3511 Programming Fundamentals I

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

the problem by providing the following:

1. Pseudocode (5 marks)

Pseudocode

1. Start

2. Import necessary libraries:

❖ pandas

❖ openly

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

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

# 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()

Submit your practical document with your full name and student number via

Moodle

Due date: Wednesday, 17/04/2024

You might also like