We continue with our tutorials showing you the For loop, we modify our previous code to request 3 times a number
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]
for i in range(3):
inputNumber = input("Input a number between 1 and 60 ")
try:
intNumber = int(inputNumber)
if intNumber <= 60 and intNumber >= 1:
if intNumber in oddNumbersArray:
print("Number is odd ", intNumber)
else:
print("Number is even")
else:
print("Number is out of bounds")
except Exception as e:
print(e)
Line 1 Create a List of odd numbers
Line 2 Use For loop where i is a variable for counting the iterations which we define in the range of 3 times
Line 3 Request a number
Line 4 Use try catch exception just in case somebody type something different than a number
Line 5 Convert the input string to a int
Line 6 Verify if the number is between bounds
Line 7 Verify if number is in the List
Line 8 If the number is in the list we print “Number is odd” with the number
Line 9 If the number is not on the list 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)
This For loop repeat 3 times and after that it just finish. That’s it plane and simple.