python - 类型错误:不支持的操作数类型/: 'str' 和 'str'

标签 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')

每当我运行这个程序我得到这个

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

我该怎么做才能将 pyc 除以 tpy?

最佳答案

通过将它们变成整数:

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

在 python 3 中,input() 函数返回一个字符串。总是。这是 Python 2 的一个变化; raw_input() 函数已重命名为 input()

关于python - 类型错误:不支持的操作数类型/: 'str' 和 'str',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15235703/

相关文章:

python - 为 python 脚本禁用 IPv6

python - 递归回溯面试题

python - "Can' t 将 'float' 对象隐式转换为 str"

php - CakePHP 不支持的操作数

C++ 使用方括号和指向实例的指针

python - 包含1的随机数函数python?

python - 获得英语单词的基本形式

javascript - context.drawImage(bufferContext,0,0) 返回类型错误?

javascript - 同位素 - 无法读取未定义的属性 'filteredItems'

c++:为什么整数前面的减号会给出一个疯狂的数字?