How to convert Nonetype to int or string?

前端 未结 10 1983
情歌与酒
情歌与酒 2020-11-29 17:23

I\'ve got an Nonetype value x, it\'s generally a number, but could be None. I want to divide it by a number, but Python raises:

<
10条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 18:20

    You should check to make sure the value is not None before trying to perform any calculations on it:

    my_value = None
    if my_value is not None:
        print int(my_value) / 2
    

    Note: my_value was intentionally set to None to prove the code works and that the check is being performed.

提交回复
热议问题