1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python3xでassert statement

Last updated at Posted at 2016-01-08

assert statement

Programmers use assert statements to verify expectations, such as the output of a function being tested.

boolean context。シンプルにfalseがでたらエラーを吐いて""を表示してくれるstatementtrueの場合は特に何も処理を行わない。
一般化するとassert (conditional statement), "otherwise reason or exception says"こういったテストは同じファイルの中で書くか、_test.pyと名付け別のファイルとして保存するのが通例。

your_age = int(input("your age is?"))
assert your_age >= 0, "how is your age negative?" # age always has to be greater than equal 0, otherwise...
print("your age is:", your_age)

ただfalseが出るとプログラム自体がクラッシュしてしまうのであまり好まれない。

ref:

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?