I think you are learning Python. Other answers are correct. But I'm going to answer your main question: "how to calculate percentage in python"
Although it works the way you did, it does not look very pythonic. Also, what happens if you need to add a new topic? You will need to add another variable, use a different input, etc. I think you want to get the average value for all brands, so you also have to change the number of items every time you add a new one! It seems a mess ...
I will throw a piece of code where the only thing you need to do is add the name of the new object to the list. If you try to understand this simple piece of code, your Python programming skills will experiment with a little kick.
#!/usr/local/bin/python2.7 marks = {} #a dictionary, it a list of (key : value) pairs (eg. "Maths" : 34) subjects = ["Tamil","English","Maths","Science","Social"] # this is a list #here we populate the dictionary with the marks for every subject for subject in subjects: marks[subject] = input("Enter the " + subject + " marks: ") #and finally the calculation of the total and the average total = sum(marks.itervalues()) average = float(total) / len(marks) print ("The total is " + str(total) + " and the average is " + str(average))
Here you can test the code and experiment with it.
JJGuerrero
source share