r/pythonhelp • • Jan 13 '17

Please help me with a script - I'm struggling with the count for variables in a list

Let me preface this by saying that I'm a total beginner and have been reading from online books. I apologize in advance for the n00b question. I need a script that prompts a user to input their namge, their gender and their age and repeat these questions until they are done. The script runs and does mostly what I expect it to. However, it doesn't seem to be calculating the count correctly for the number of 'M' entries that I've entered into the script. It's calculating the number of 'F' entries just fine. Here is the script that I have put together....

count = 0
total = 0
gender = []
ask = input("Do you want to enter a survey? (Y/N)")
try:
    while ask == "Y":
        age = float(input("Enter your Age : "))
        count = count + 1
        total = total + age
        gender = input("What is your gender - M or F?")
        continued_ask = input("Do you want to continue entering another survey? (Y/N) ")
        if continued_ask == "Y":
            continue
        elif continued_ask == "N":
            print ("The average of", count, "ages is", total / count)
            print ("The total number of Males is", gender.count("M"))
            print ("The total number of Females is", gender.count("F"))
            break
except:
    print ("Average cannot be calculated")

Where have I gone wrong? Please keep in mind that I'm very much a beginner and could use a nudge in the right direction.

2 Upvotes

2 comments sorted by

2

u/CrayonConstantinople Jan 22 '17

This is pretty good! Welldone! Simple mistake as is usually the case here:

You have a gender variable assigned to a list on line 3.

However, you overwrite it with a string response in line 10. What you actually want to do here is capture the response and append it to your list like so:

genders = []
gender = input("What is your gender - M or F?")
genders.append(gender)

Your new code would look like this:

count = 0
total = 0
genders = []
ask = input("Do you want to enter a survey? (Y/N)")
try:
    while ask == "Y":
        age = float(input("Enter your Age : "))
        count = count + 1
        total = total + age
        gender = input("What is your gender - M or F?")
        genders.append(gender)
        continued_ask = input("Do you want to continue entering another survey? (Y/N) ")
        if continued_ask == "Y":
            continue
        elif continued_ask == "N":
            print ("The average of", count, "ages is", total / count)
            print ("The total number of Males is", genders.count("M"))
            print ("The total number of Females is", genders.count("F"))
            break
except:
    print ("Average cannot be calculated")

Edit: You'll probably get a faster response time if you post your questions to r/learnpython. Some very smart and friendly people over there who are ready to help. This sub doesn't seem to get much action!

2

u/jennordinary Jan 22 '17

Thank you so much for the helpful reply! I knew that there was something wrong with how the variable was being stored, I just couldn't sort it out myself. Thank you again and I'll keep an eye on r/learnpython as well!