We start our Python tutorials with some basic commands to warm up, you can test it on the Test Python basic code page to see results.
inputNumber = input("Input a number between 1 and 60 ")
oddNumbersArray = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]
try:
intNumber = int(inputNumber)
if intNumber in oddNumbersArray:
print("Number is odd ", intNumber)
else:
if intNumber > 60 or intNumber < 1:
print("Number is out of bounds")
else:
print("Number is even")
except Exception as e:
print(e)
Lets request for a number and verify if it is odd or even, just for showing you some basics with Python.
Line 1 Request a number
Line 2 Create a List of odd numbers
Line 3 Use try catch exception just in case somebody type something different than a number
Line 4 Convert the input string to a int
Line 5 Verify if number is in the list
Line 6 If it is in the list we print “Number is odd” with the number
Line 7 If not we verify if the number is between bounds
Line 8 If it is not we print a message “Number is out of bounds”
Line 9 If it is we print a message “Number is even”
Line 10 Catch the exception as variable e. (in case there is an exception)
Line 11 We print the exception message. (in case there is an exception)