# "Beginners course"? Yeah right:)

Hi guys! Hope all is well. Started a “beginners course” the other week. I found it not so much of a beginners course. Struggling:)

Anyway, I’ve got a task to solve now and I cant really get my head around it.

The info I have is this;

Write a program that stores information about a number of people, and calculates the average age of women and men in two separate groups.

Follow the following Structure diagram (and then there’s the picture)

Not really sure how to do this. Been trying to figure out how to add names and ages from an input function into a dictionary. Feel a bit lost. Thankful for tips and ideas.

Best regards / Kristoffer

To be fair, it’s not really asking for ‘names’, right?

``````'''Write a program that stores information about a number of people,
and calculates the average age of women and men in two separate groups.'''

def get_info():
gender, age = False, False
while not gender:
gender = input("(M)ale or (F)emale: ").upper()
if gender not in ("M", "F"):
gender = False
while not age:
age = input("Age: ")
if age.isnumeric():
age = int(age)
else:
age = False
print()
return gender, age

gender, age = get_info()

# just to see the output

print(gender, age)
``````

Thank you so much Rob, ill try that!

1 Like

It was very helpful Rob, but to be honest I’m still not getting there. The program only seems to run one time and I want to run as long as I’m still adding more people. And all the people I add also needs to be added to a list. Don’t know how to do that. This is really a jungle for me. Ive just started to know what floats and int’s are😅

Hey man; we’ve all been there, so don’t sweat it.

Let’s see where you’re at so that I (or someone) can guide you: post your code, but make sure that it’s formatted correctly, as the indentation as a critical factor.

To retain the formatting, before you ‘post’, your code block, in the on-line editor, should look like this:

`````````python
# this is where your code should be put
```
``````

The fencing makes are called ‘backtics’ and can usually be found at the top L/H side of the keyboard, just below the Esc key.

As for floats and integers, we’ll get to that, one step at a time.

Thanks buddy!

Ive honestly don’t have anything more then the code you sent me. When i run it this happen;

(M)ale or (F)emale: f
Age: 43

F 43

And then it stops. So I’d like to go on until I say no to the program. And Then I’d like to store all of the Male/Female in two groups so that I can calculate the average age in each group.

Sorry, feel quite useless, this way above my pay grade;)

Okay, so, looking back at the flowchart, we’ve done the ‘input person’, so now we need to do something with that data before we ask for any more.

The flowchart is the guide here: it’s saying ‘add 1 to A1 and age to G1’ for one branch, and ‘add 1 to A2 and age to G2’ for the other branch.

I’ve given you the `get_info()` function, so below that we’ll set up the variables [functions should always be at the head of your code, by convention]:

``````
A1 = 0
G1 = 0
A2 = 0
G2 = 0

# as an alternative...

A1 = A2 = G1 = G2 = 0
``````

So, the diamond is a decision that needs to be made as to which variable should be incremented by `1` (as in `A2 += 1`) and which variable should have the `age` added to it (as in `G2 += age`). Maybe a `if/else` branch will do that job. Do you know how to do that?

To be clear, this is where you should be at:

``````def get_info():
gender, age = False, False
while not gender:
gender = input("(M)ale or (F)emale: ").upper()
if gender not in ("M", "F"):
gender = False
while not age:
age = input("Age: ")
if age.isnumeric():
age = int(age)
else:
age = False
print()
return gender, age

A1 = A2 = G1 = G2 = 0

gender, age = get_info()

# now do the if/else branch
``````

To add a comment: I think that the flowchart could have been better thought out and have “G” for the gender count and “A” for the age summation, but no matter: the lesson here is to code the app from the flowchart; the data presentation can be dealt with once we have the logic working – that should always be job #1

Thanks again Rob! Just woke up here in Scandinavia. Thanks for your ideas and help. Ill try to work with that. I’ve worked with if/else before, it just gets very confusing when the code is so long. So many different functions😅

I’m sorry I feel lost. Could I use something like this;

for ‘M’ in str(gender):
A1.append(1)

Or is that just stupid?

No, not quite: you’re over thinking it.

Look at what we have if we add this to the bottom:

``````print(f"gender = {gender}")
print(f"age = {age}")
``````

… and run the script with an input of M and 21

So, `if gender == 'M':` ← Now look at your flowchart. What do you do next?

``````# A1 is the number of Males
# G1 is the total age of all the Males
# A2 is the number of Females
# G2 is the total age of all the Females
``````

Thanks, i’m working on it. I’m trying to figure out what it is that all the code does in the input function. Line by line, its confusing. Trying to understand the logic of it:)

It’s good that you’re trying to work that out. The reason that I designed the function in the way that I did, was so that the returned data is constrained to either a uppercase M or a uppercase F (for Male and Female) and a integer value for the age. By doing that, it makes the ‘control code’ (that’s the code that is outside of that function) so much more straight forward to deal with, which I thought would be of help to a beginner, such as yourself.

Just treat it as a ‘black box’ for now and accept what it is doing; just like you do (or would) for a builtin function, such as the `print()` function.

To add: if you are worried that you’ll be called out on that function, by your tutor, then come clean and say that you had some help with that. It is, when all said and done, just a function (it’s the first box after the ‘START’ on the flowchart) and in no way compromises the other coding work that you need to do.

Thanks again. I’m trying to figure out how to use the If-function to add things to my dictionary / list

I must be missing some details here: aside from the flowchart, what else have you been given in order to complete the task?

To add and ask the question in another way: if you end off with a simple script that produces this output:

``````Results of the survey.

Total number of Females surveyed: 4
The average age of Females: 24

Total number of Males surveyed: 4
The average age of Males: 29

Total number of people surveyed: 8
``````

… what would the issue with that be?

As I understand it its supposed to be a program that first asks you: Female or Male, then the age of that person. The person gets stored in the correct list and then the program asks again, as long as you want to put in more people. Once you’ve decided theres enough you move on to the next step where the program calculates the average age in each group:)

So i suppose there also needs to be a question that asks if you want to add more people or not

… which is exactly how the script runs from which I posted that output.

If you follow what I’m asking you to do, that’s what you’ll end off with, but for some reason you seem very reluctant to do that.

If you want my function to accept ‘Male’ and ‘Female’ as inputs, then change it to:

``````def get_info():
gender, age = False, False
while not gender:
gender = input("Male or Female: ")
if gender not in ("Male", "Female"):
gender = False
while not age:
age = input("Age: ")
if age.isnumeric():
age = int(age)
else:
age = False
print()
return gender, age
``````

Or, scrap it altogether and have:

``````gender = input("Male or Female: ")
age = input("Age: ")
``````

… as the first two lines of your script, if you’d prefer. But, you’ll have to accept that anything that is entered incorrectly by a user, will crash the script – you choose.

oh sorry, I’m not reluctant I simply don’t know how to move forward. Its my second week of programming ever so I guess its like talking to a kid. F and M is perfect I don’t want to change that.
Your code is working beautiful I’m just stuck after that. The output that you suggested where the total number of females, average age etc look great just don’t know how to get to that.

I don’t want to take anymore of your time now, again, thank so much for being so helpful. It’s just me who’s a newbie at this. Cheers!

Let me refer you back to this and again say…

I’ve done the first part for you:

``````if gender == 'M':
# now, what do you think would be here?
``````

Try explaining the logic in English first: say you find out that the person is male - now what should you do with the information that was given about that person? Specifically, what should you do with the age number?

Alternately: let’s start over in trying to understand the problem, and consider what’s written on the flowchart. What do you think is the intended meaning of the “A1”, “A2”, “G1”, “G2” variables? What is their role in computing the averages? Given that, for example, we will “add 1 to A1” in one of the steps, what do you think should be the starting value for A1? What type should it be?

Alternately: let’s say you were going to do the task by hand. Step by step, how would you describe the process? If you want to find the average of some numbers, do you actually have to know what all the numbers are at the same time (i.e., remember them in a list)? What information do you actually need about the numbers - the sum and the count, right? Can you think of a way to find out those results, by only considering the input numbers one at a time? (Hint: the flowchart shows you how.)