Do I need to use the Return Function in this Code?

Issue

This is a code I wrote to get a patient’s data before and after their visit to the doctor’s office.
I want to know if I should use the return function in this code? And if I need to, any assistance on where it would be required would be appreciated. Thanks.

patients_info = [
{
"Name": "Patricia Allen",
"Age": 25,
"Nationality": "America",
"State of Origin": "New York",
"Marital Status": "Single",
"Gender": "Female",
"Purpose of Visit": "Chest Xray and blood pressure checks",
"Next Visit date": "28th August, 2022",
},
{
"Name": "Nicholas Carmen",
"Age": 30,
"Nationality": "America",
"State of Origin": "Seattle",
"Marital Status": "Single",
"Gender": "Male",
"Purpose of Visit": "Blood pressure check up and weight checks",
"Next Visit date": "25th September, 2022",
},
]
record_again = True
while record_again:
  name = input("Patient's name: ")
  age = int(input("Patient's age: "))
  nation = input("Nationality: ")
  state = input("State of Origin: ")
  marital = input("Marital status: ")
  gender = input("Gender: ")
  purpose = input("Purpose of visit: ")
  next_visit = input("Next visit date: ")
  def new_patients_info(name, age, nation, state, marital, gender, purpose, next_visit):
    new_info = {}
    new_info["Name"] = name
    new_info["Age"] = age
    new_info["Nationality"] = nation
    new_info["State of Origin"] = state
    new_info["Marital Status"] = marital
    new_info["Gender"] = gender
    new_info["Purpose of Visit"] = purpose
    new_info["Next Visit date"] = next_visit
    patients_info.append(new_info)

  another_patient = input("Will you need to record another patient? 'Yes' or 'No'?  
 ").lower()
  if another_patient == 'no':
    new_patients_info(name, age, nation, state, marital, gender, purpose, next_visit)
    print(patients_info)
    record_again = False
  else:
    new_patients_info(name, age, nation, state, marital, gender, purpose, next_visit)

Solution

No, it is not necessary to return, since the function has a side effect of appending to the patients_info list, and there is no value to return that would be useful to the caller.

Note that you can simplify the loop quite a bit by moving the function definition out of it, using break to break when it’s time to stop entering patients, and then doing the print after the loop has broken:

patients_info = [
    {
        "Name": "Patricia Allen",
        "Age": 25,
        "Nationality": "America",
        "State of Origin": "New York",
        "Marital Status": "Single",
        "Gender": "Female",
        "Purpose of Visit": "Chest Xray and blood pressure checks",
        "Next Visit date": "28th August, 2022",
    },
    {
        "Name": "Nicholas Carmen",
        "Age": 30,
        "Nationality": "America",
        "State of Origin": "Seattle",
        "Marital Status": "Single",
        "Gender": "Male",
        "Purpose of Visit": "Blood pressure check up and weight checks",
        "Next Visit date": "25th September, 2022",
    },
]

def new_patients_info(name, age, nation, state, marital, gender, purpose, next_visit):
    patients_info.append({
        "Name": name,
        "Age": age,
        "Nationality": nation,
        "State of Origin": state,
        "Marital Status": marital,
        "Gender": gender,
        "Purpose of Visit": purpose,
        "Next Visit date": next_visit,
    })

while True:
    new_patients_info(
        input("Patient's name: "),
        int(input("Patient's age: ")),
        input("Nationality: "),
        input("State of Origin: "),
        input("Marital status: "),
        input("Gender: "),
        input("Purpose of visit: "),
        input("Next visit date: "),
    )
    if input(
        "Will you need to record another patient? 'Yes' or 'No'?  "
    ).lower() == 'no':
        break

print(patients_info)

Answered By – Samwise

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published