user2685621
user2685621

Reputation: 9

Percentage Calculations Not Working

mark=input("Please enter the mark you received for the test:")
total=input("Please enter the mark the test was out of:")

percentage=(mark*100/total)

print("Your percentage is:"),percentage,"%"

When I run this in python 3.3.2 mac it comes up with this error.

Traceback (most recent call last):
  File "/Users/user1/Desktop/Percentage2.py", line 4, in <module>
    percentage=(mark/total*100)
TypeError: unsupported operand type(s) for /: 'str' and 'str'

How can I fix it?

Upvotes: 0

Views: 5296

Answers (3)

James Clarke
James Clarke

Reputation: 149

An input returns a string, You are trying to do "30"*100/total, Strings cannot be mathematically calculated, try int(mark) and int(total) and then do the maths.

try:
    Mark = int(input("Please enter the mark you received for the test."))
    Total = int(input("Please enter the mark the test was out of."))
    Perc = (Mark*100) / Total
    print("Your Percentage is"+Perc)
except:
    print("Numbers not entered. Please try again")

Upvotes: 3

Brionius
Brionius

Reputation: 14118

percentage=(float(mark)*100/float(total))

print("Your percentage is: ",percentage,"%", sep='')

Upvotes: 6

e.doroskevic
e.doroskevic

Reputation: 2167

When you make "print" statement do :

print ("your percentage is: %p" % (percentage))

Additionally you can use format :

print("your percentage is: {0} ".format(percentage))

Personally recommend to use format.

Upvotes: 0

Related Questions