'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 i do to divide pyc by tpy?
Solution 1:[1]
The first thing you should do is learn to read error messages. What does it tell you -- that you can't use two strings with the divide operator.
So, ask yourself why they are strings and how do you make them not-strings. They are strings because all input is done via strings. And the way to make then not-strings is to convert them.
One way to convert a string to an integer is to use the int function. For example:
percent = (int(pyc) / int(tpy)) * 100
Solution 2:[2]
I would have written:
percent = 100
while True:
try:
pyc = int(input('enter pyc :'))
tpy = int(input('enter tpy:'))
percent = (pyc / tpy) * percent
break
except ZeroDivisionError as detail:
print 'Handling run-time error:', detail
Solution 3:[3]
There is another error with the forwars=d slash.
if we get this : def get_x(r): return path/'train'/r['fname']
is the same as def get_x(r): return path + 'train' + r['fname']
Solution 4:[4]
name = input ('What is your name?: ')
age = input('How old are you?: ')
date = input ('What year is it?: ')
year = (int(date) - int(age) + 100)
print('You\'ll be 100 in the year ', year)
#That's how I decided to hardcode it. You could get more specific with actual birthdates or else it'll probably be off by a year unless your bday passed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bryan Oakley |
| Solution 2 | Kovalchuk |
| Solution 3 | Oscar Rangel |
| Solution 4 | chrisirl617 |
