Python - int()

参考 https://docs.python.org/3/library/functions.html?highlight=int#int

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base

问题:想将input()中返回的字符串转换为int,故尝试用int(),转换失败。

int(str), str只能是整数的字面量,例如int('1.5')会报错。

解决办法:

  1. int(float('1.5'))
  2. round(float('1.5')) #注意,此输出为2
  3. math.floor(float('1.5'))
posted @ 2018-04-11 16:47  Rocin  阅读(254)  评论(0编辑  收藏  举报