【问题标题】:TypeError: unsupported operand type(s) for -: 'int' and 'str' (python) [duplicate]TypeError:不支持的操作数类型-:'int'和'str'(python)[重复]
【发布时间】:2016-10-27 19:54:50
【问题描述】:

我真的需要帮助。我对 python 很陌生(实际上我是昨天开始的),我不断收到这条消息:

TypeError:-: 'int' 和 'str' 的操作数类型不受支持

尝试这样做时:

age = input()
year = (2016-age)
print (year)

因为我是新人,所以请简单回答。

【问题讨论】:

  • 如果你已经学习了一天的 Python,你应该还在阅读教程和指南。

标签: python


【解决方案1】:

input 函数返回一个字符串,因此您的age 变量中有一个字符串。 您不能从整数中减去字符串,因此您必须使用 int 函数将字符串转换为整数。

age = int(input())

【讨论】:

  • 具体input()在Python 3中返回字符串,而不是在Python 2中。它与python 2中的raw_input()相同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 2012-11-29
  • 2012-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多