TypeError: unsupported operand type(s) for /: 'str' and 'str'



name = input('Enter name here:')
pyc = input('enter pyc :')
tpy = input('enter tpy:')
percent = (pyc / tpy) * 100;
print (percent)
input('press enter to quit')

whenever i run this program i get this

TypeError: unsupported operand type(s) for /: 'str' and 'str'

what can iido to divide pyc by tpy?



The input function always returns a string. That is why you are not able to divide inputs. You have to turn them to integers before applying division. Replace the "percent = (pyc / tpy) * 100;" line with the below line.

percent = (int(pyc) / int(tpy)) * 100;



If you want to unleash your potential in this competitive field, please visit the Python course page for more information, where you can find the Python tutorials and Python frequently asked interview questions and answers as well.


This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.