TypeError: unsupported operand type(s) for /: 'str' and 'str'
up vote
4
down vote
favorite
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?
python typeerror operand
add a comment |
up vote
4
down vote
favorite
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?
python typeerror operand
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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?
python typeerror operand
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?
python typeerror operand
python typeerror operand
asked Mar 5 '13 at 22:52
Deric miller
27126
27126
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
11
down vote
accepted
By turning them into integers instead:
percent = (int(pyc) / int(tpy)) * 100;
In python 3, the input()
function returns a string. Always. This is a change from Python 2; the raw_input()
function was renamed to input()
.
4
Or, potentially,float
s ordecimal.Decimal
s as appropriate, if you need to accept numbers that are not whole integers.
– Gareth Latty
Mar 5 '13 at 22:54
add a comment |
up vote
5
down vote
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
add a comment |
up vote
0
down vote
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
thanks, im new to programming and i never would have known to do this
– Deric miller
Mar 7 '13 at 0:12
If you're going to add in error handling, you probably want anexcept ValueError
clause to handle invalid inputs…
– abarnert
Sep 19 '13 at 20:02
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
By turning them into integers instead:
percent = (int(pyc) / int(tpy)) * 100;
In python 3, the input()
function returns a string. Always. This is a change from Python 2; the raw_input()
function was renamed to input()
.
4
Or, potentially,float
s ordecimal.Decimal
s as appropriate, if you need to accept numbers that are not whole integers.
– Gareth Latty
Mar 5 '13 at 22:54
add a comment |
up vote
11
down vote
accepted
By turning them into integers instead:
percent = (int(pyc) / int(tpy)) * 100;
In python 3, the input()
function returns a string. Always. This is a change from Python 2; the raw_input()
function was renamed to input()
.
4
Or, potentially,float
s ordecimal.Decimal
s as appropriate, if you need to accept numbers that are not whole integers.
– Gareth Latty
Mar 5 '13 at 22:54
add a comment |
up vote
11
down vote
accepted
up vote
11
down vote
accepted
By turning them into integers instead:
percent = (int(pyc) / int(tpy)) * 100;
In python 3, the input()
function returns a string. Always. This is a change from Python 2; the raw_input()
function was renamed to input()
.
By turning them into integers instead:
percent = (int(pyc) / int(tpy)) * 100;
In python 3, the input()
function returns a string. Always. This is a change from Python 2; the raw_input()
function was renamed to input()
.
answered Mar 5 '13 at 22:53
Martijn Pieters♦
695k12924042245
695k12924042245
4
Or, potentially,float
s ordecimal.Decimal
s as appropriate, if you need to accept numbers that are not whole integers.
– Gareth Latty
Mar 5 '13 at 22:54
add a comment |
4
Or, potentially,float
s ordecimal.Decimal
s as appropriate, if you need to accept numbers that are not whole integers.
– Gareth Latty
Mar 5 '13 at 22:54
4
4
Or, potentially,
float
s or decimal.Decimal
s as appropriate, if you need to accept numbers that are not whole integers.– Gareth Latty
Mar 5 '13 at 22:54
Or, potentially,
float
s or decimal.Decimal
s as appropriate, if you need to accept numbers that are not whole integers.– Gareth Latty
Mar 5 '13 at 22:54
add a comment |
up vote
5
down vote
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
add a comment |
up vote
5
down vote
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
add a comment |
up vote
5
down vote
up vote
5
down vote
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
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
answered Mar 5 '13 at 22:53
Bryan Oakley
210k21248408
210k21248408
add a comment |
add a comment |
up vote
0
down vote
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
thanks, im new to programming and i never would have known to do this
– Deric miller
Mar 7 '13 at 0:12
If you're going to add in error handling, you probably want anexcept ValueError
clause to handle invalid inputs…
– abarnert
Sep 19 '13 at 20:02
add a comment |
up vote
0
down vote
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
thanks, im new to programming and i never would have known to do this
– Deric miller
Mar 7 '13 at 0:12
If you're going to add in error handling, you probably want anexcept ValueError
clause to handle invalid inputs…
– abarnert
Sep 19 '13 at 20:02
add a comment |
up vote
0
down vote
up vote
0
down vote
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
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
answered Mar 6 '13 at 2:49
Kovalchuk
315
315
thanks, im new to programming and i never would have known to do this
– Deric miller
Mar 7 '13 at 0:12
If you're going to add in error handling, you probably want anexcept ValueError
clause to handle invalid inputs…
– abarnert
Sep 19 '13 at 20:02
add a comment |
thanks, im new to programming and i never would have known to do this
– Deric miller
Mar 7 '13 at 0:12
If you're going to add in error handling, you probably want anexcept ValueError
clause to handle invalid inputs…
– abarnert
Sep 19 '13 at 20:02
thanks, im new to programming and i never would have known to do this
– Deric miller
Mar 7 '13 at 0:12
thanks, im new to programming and i never would have known to do this
– Deric miller
Mar 7 '13 at 0:12
If you're going to add in error handling, you probably want an
except ValueError
clause to handle invalid inputs…– abarnert
Sep 19 '13 at 20:02
If you're going to add in error handling, you probably want an
except ValueError
clause to handle invalid inputs…– abarnert
Sep 19 '13 at 20:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15235703%2ftypeerror-unsupported-operand-types-for-str-and-str%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown