How to use assertion in Python

2 Answers

0 votes
celsius = -2

fahrenheit = ((9.0 / 5.0) * celsius) + 32

print(fahrenheit) 
print(fahrenheit <= 30) 

assert fahrenheit >= 30, "It's very cold" # if false

print("Temperature in Fahrenheit =", fahrenheit)




'''
run:

28.4
True
Traceback (most recent call last):
  File "<string>", line 8, in <module>
AssertionError: It's very cold

'''

 



answered Oct 6, 2021 by avibootz
0 votes
assert 2 + 2 == 4, "true"

assert 2 + 2 == 5, "false"





'''
run:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
AssertionError: false

'''

 



answered Oct 6, 2021 by avibootz

Related questions

1 answer 198 views
198 views asked Sep 23, 2021 by avibootz
1 answer 332 views
6 answers 685 views
2 answers 290 views
...