Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

B. Define a function called all_integers () that takes a tuple, returns True if

ID: 3913013 • Letter: B

Question

B. Define a function called all_integers () that takes a tuple, returns True if all the items in the tuple are integers 2l, and returns False otherwise. For example, all_integers(3, 5, 17, 257, 65537), all_integers( (-1,, and all_integers () should all return True, but all_integers( (2.0,4.0) and all_integers (8, 4, "2", 1) should both return False. Your function should use a for loop to do this calculation. 121 Hint: Don't forget about the isinstance) function, which we introduced in Homework 04.

Explanation / Answer

#Function to accept a tuple and return True if all elements are integers and False if not

def all_integers(tup):

#initialize flag to zero
flag = 0

#Iterate through all elements in tuple
for i in range (0,len(tup)):

#See if the current element is an integer
if isinstance(tup[i],int)==False:

#Set flag to 1 if the current element is not integer
flag = 1;

#If all are integers return True
if flag == 0:
return True

#Otherwise return False
else:
return False