How do you get Python to detect for no input

后端 未结 5 508
野趣味
野趣味 2020-12-11 07:04

I\'m new to coding in python and I was filling out some code that required a number of inputs. One thing it asked for was for the program to perform an action if the user pr

5条回答
  •  甜味超标
    2020-12-11 07:31

    Just another tips:

    In python you don't need to do equality test for empty string. Instead please use truth value testing. That is more pythonic.

    if not coinN:
    

    Truth value testing covers the following test:

    • None
    • False
    • zero of any numeric type, for example, 0, 0L, 0.0, 0j.
    • any empty sequence, for example, '', (), [].
    • any empty mapping, for example, {}.
    • instances of user-defined classes, if the class defines a nonzero() or len() method, when that method returns the integer zero or bool value False. 1

    Example:

    >>> s = ''
    >>> if not s:
    ...     print 's is empty'
    ...
    s is empty
    >>>
    

提交回复
热议问题