Answer:
In Python:
def main():
n = int(input("List items: "))
mylist = []
for i in range(n):
listitem = input(", ")
if listitem.isdecimal() == True:
mylist.append(int(listitem))
else:
mylist.append(listitem)
print(game_of_eights(mylist))
def game_of_eights(mylist):
retVal = "False"
intOnly = True
for i in range(len(mylist)):
if isinstance(mylist[i],int) == False:
intOnly=False
retVal = "Please, enter only integers"
if(intOnly == True):
for i in range(len(mylist)-1):
if mylist[i] == 8 and mylist[i+1]==8:
retVal = "True"
break
return retVal
if __name__ == "__main__":
main()
Explanation:
The main begins here
def main():
This gets the number of list items from the user
n = int(input("List items: "))
This declares an empty list
mylist = []
This iterates through the number of list items
for i in range(n):
This gets input for each list item, separated by comma
listitem = input(", ")
Checks for string and integer input before inserting item into the list
<em> if listitem.isdecimal() == True:</em>
<em> mylist.append(int(listitem))</em>
<em> else:</em>
<em> mylist.append(listitem)</em>
This calls the game_of_eights function
print(game_of_eights(mylist))
The game_of_eights function begins here
def game_of_eights(mylist):
This initializes the return value to false
retVal = "False"
This initializes a boolean variable to true
intOnly = True
This following iteration checks if the list contains integers only
for i in range(len(mylist)):
If no, the boolean variable is set to false and the return value is set to "integer only"
<em> if isinstance(mylist[i],int) == False:</em>
<em> intOnly=False</em>
<em> retVal = "Please, enter only integers"</em>
This is executed if the integer contains only integers
if(intOnly == True):
Iterate through the list
for i in range(len(mylist)-1):
If consecutive 8's is found, update the return variable to True
<em> if mylist[i] == 8 and mylist[i+1]==8:</em>
<em> retVal = "True"</em>
<em> break</em>
This returns either True, False or Integer only
return retVal
This calls the main method
if __name__ == "__main__":
main()