Skip to content
Advertisement

Don’t know how to saparate the integral from the list in python

Hello~ This is my first time to use stackoverflow.

I have difficulty solving the question.

I want to add the integrals but don’t know how to separate from the list and transfer from strings to integrals.

Could you help me? thank you.

Sorry my English is not good.

the code:

records = input("Add everyone's scores:n").split(', ')
print("Here's everyone's scores:")
for record in records:
    print(record)
    for amount in record.split(" ")[1]:
        print(amount)
amounts = int(amount)
print(records)

input:

Alice 80, Brian 60, Susan 70

and my output is:

Add everyone's scores:
Alice 80, Brian 60, Susan 70
Here's everyone's scores:
Alice 80
8
0
Brian 60
6
0
Susan 70
7
0
['Alice 80', 'Brian 60', 'Susan 70']

the output I want :

Add everyone's scores:
Alice 80, Brian 60, Susan 70
Here's everyone's scores:
Alice 80
Brian 60
Susan 70
the average of the scores = 70

Advertisement

Answer

Go for something like this:

records = input("Add everyone's scores:n").split(', ')
print("Here's everyone's scores:")
amount = 0
for record in records:
    print(record)
    amt = record.split(" ")[1]
    amount += int(amt)

print('The average of the scores is = ',str(amount/len(records)))

If can not understand any part please comment.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement