Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

while loop - Trying to find average yields TypeError: unsupported operand type(s) for +: 'int' and 'str'

I am trying to find the average in a list of ages for boys and girls in Python 3.9.1 for an assignment. My code starts by defining a list for both boy and girl ages.

bagelist = [];
gagelist = [];

I then include a while loop to gather the ages and end it by trying to use the sum() / len() function to get the average for both lists.

v4 = input("enter name")

while v4 != "":
    
    v5 = input("enter sex (boy/girl) ")
    
    v6 = input("How old are they? ")
    
    if(v5 == "boy"):
        boys += 1
        bagelist.append((v6))
        
    elif(v5 == "girl"):
        girls += 1
        gagelist.append((v6))

    v4 = input("Is there anyone else to enter? If not, press ENTER.")

sum(bagelist) / len(bagelist)

sum(gagelist) / len(gagelist)

This just returns the TypeError: unsupported operand type(s) for +: 'int' and 'str' error when I go through the program though and will not average the lists. I have tried using the mean() function as well by typing statistics import mean as well and it does not seem to want to import. I was hoping someone here would be able to tell me how to derive the mean from these lists without receiving this error.

I will say that I apologize if I am not importing the mean function quite right as I am incredibly new to Python as well as programming overall. I do also apologize if my question isn't written well as I am also new to Stack Overflow. Constructive criticism is appreciated on these as well as the question at hand. thank you very much for any help.

question from:https://stackoverflow.com/questions/65917305/trying-to-find-average-yields-typeerror-unsupported-operand-types-for-int

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...