Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 712
Deric miller Avatar asked Mar 05 '13 22:03

Deric miller


People also ask

What does TypeError unsupported operand type S for /: list and int?

The Python "TypeError: unsupported operand type(s) for /: 'list' and 'int'" occurs when we try to use the division / operator with a list and a number. To solve the error, figure out how the variable got assigned a list and correct the assignment, or access a specific value in the list.

How do you solve unsupported operand type S for STR and STR?

The Python "TypeError: unsupported operand type(s) for -: 'str' and 'str'" occurs when we try to use the subtraction - operator with two strings. To solve the error, convert the strings to int or float values, e.g. int(my_str_1) - int(my_str_2) .

How do I fix unsupported operand type s?

The Python "TypeError: unsupported operand type(s) for /: 'str' and 'int'" occurs when we try to use the division / operator with a string and a number. To solve the error, convert the string to an int or a float , e.g. int(my_str) / my_num .

What does unsupported operand mean?

The TypeError: unsupported operand type(s) for +: 'int' and 'str' error occurs when an integer value is added with a string that could contain a valid integer value. Python does not support auto casting. You can add an integer number with a different number. You can't add an integer with a string in Python.


2 Answers

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().

like image 196
Martijn Pieters Avatar answered Sep 30 '22 19:09

Martijn Pieters


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 
like image 42
Bryan Oakley Avatar answered Sep 30 '22 17:09

Bryan Oakley