后浪云Python教程:python怎么输入整数

后浪云Python教程:python怎么输入整数插图

使用input()函数输入一个整数

a=input(“请输入一个整数\n”)

相关推荐:《python基础教程》

结果却报错TypeError: 'str' object cannot be interpreted as an integer

查阅文档发现input()函数返回值是str型。

我们只需要这样转换:

a=int(input("请输入一个整数"))(强制类型转换,其他同理)或者用a=eval(input("请输入一个整数"))(自动类型转换)

THE END