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.
To be fair, it’s not really asking for ‘names’, right?
So how about this to get you started:
'''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():
print("Please enter your gender and age.")
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)
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😅
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.
Ive honestly don’t have anything more then the code you sent me. When i run it this happen;
Please enter your gender and age.
(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;)
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():
print("Please enter your gender and age.")
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😅
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.
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
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:)
… 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():
print("Please enter your gender and age.")
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!
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.)