+ 115
Assertions
So to clarify, assertions in python are pretty much made for your use only and they are placed around the program to check that an integer, string, float, etc., is what you want it to be, and to notify you when it isn’t.
47 Réponses
+ 70
yes it is. well simplified. but there are other uses as well when you want to run a segment if and only if the assertion condition you put is true. in short it can be used as conditional purposes too. if I am wrong please correct me.
+ 68
Real life usage of Assertion and try/except in program.
def d2w(n):
dicts = {
1 : "One",
2 : "Two",
3 : "Three",
4 : "Four",
5 : "Five",
6 : "Six",
7 : "Seven"
}
return dicts[n]
while True:
ui = input("Enter a number: ")
try:
assert 0 < int(ui) <= 7
print(d2w(int(ui)))
except AssertionError:
print("Number should be between 1 and 7")
except ValueError:
try:
assert ui.lower() == "stop"
print("Bye!")
break
except AssertionError:
print("U didn't Entered INT or Stop")
# This program takes number as input converter it to words and stops when user input "stop".
# While writing this code i realised that if-else conditions can be replaced with Assertion and try/except at least for checking user input.
***You query or suggestions is highly appreciated.
+ 18
Jevon I think the purpose of assertion is to keep the code running. Otherwise, the code will terminate and nothing else would be done after an exception. Exceptions don't have to be the end of the code. Hope this helps
+ 17
def age(age):
assert age >= 0, "correct your age"
print(f"your age is {age}")
age(-8)
+ 8
Aditya
yes, there was some comment, I had replied to it the same that it is not any question. instead it is a reader's view on specific topic.
No worries, it seems that comment is taken down
+ 6
Well, personally i dont feel assertion will really be useful because i've been using VS Code IDE until now, when it comes to invalid type of int, float, string,etc. It usually comes right down inside terminal and tells us which line is problematic. So i dont need to clarify with assertion because it's not neccessary.
+ 6
I think assertions in python can be useful for debugging our programs. It halts the program as soon as the return statemet is false. It is also possible to handle this errors which make them more useful.
+ 5
Its not understanding for me
+ 3
Fili Siegl "The lower() method returns a string where all characters are lower case." In the case, put on ui variable.
+ 3
Thank you!
+ 2
yes
+ 2
Mohammad Hasibul Haque correct
+ 2
Well explained! Now i got it!
+ 2
Using assertion bring us some sort of proof of what we are doing and this help wen you are starting with programming new projects or even understanding the step by step the programm do
+ 2
So if I understood correctly, Assertions are nothing but the indicators which indicates/inform us when a particular condition is given but not being followed.Pls correct me if I am wrong....
+ 2
Of course, but it reduce the utility.
+ 1
yes
+ 1
I think we use assertion to takes values logical
+ 1
I see the assert statement as a security during the development of a program. It allows to stop a program as soon as a condition is not valid without going further in the execution. Tell me if I'm wrong.
+ 1
Is assertion used for test by software tester??