Can someone please explain how this code is working for me?
I don’t understand it from the try command down.
This while loop is working for me. However I do not understand how all of it works.
price = 110 #this i get
ttt = 1 #this i get
while price< 0 or price> 100: #this i get
if ttt >=2: #this i get
print "This is an invalid entry" #this i get
print "Please enter a number between 0 and 100" #this i get
try: #From here i do not understand , but without it, it does not work
price= int(raw_input("Please enter the price : "))
except ValueError:
price = -1
ttt +=1
As I’m a learner I don’t really want a more complicated way to do it.
I just want to fully understand what’s happening in the loop.
A try statement is a way to catch you code before it crashes, or exits do to an uncaught exception. Some functions may throw errors that can cause an application to crash/force quit, and the try block takes thoses would-be-errors, and lets you do something about it.
i.e. -> An app that is trying to open a log file, but no file is found…
In your example, it will take raw data from the prompt, and assign it to an integer value… however
abccan not be converted to an integer, so it would normally crash… Inside of a TRY block, it will return a -1, saying that it did not recieve a result that it was expecting.