Python: try, except, and else

Post Reply
User avatar
Jason
Site Admin
Posts: 767
Joined: Sun Dec 21, 2025 8:56 pm

This tries a code to see if things are error-free. If not, the except stuff is returned. The except stuff has something following it, in this case, ValueError to indicate a wrong datatype.

Code: Select all

try:

	float_number = float(input("Enter a float number":))

except ValueError:

	return "Please pick a float datatype.  That type is a number with a decimal point."
	
else:

        print(f "Your chosen float number is { float_number}.")
	
 

POSTREACT(ions) SUMMARY

Post Reply