176k views
5 votes
Write a function called 'game_of_eights()' that accepts a list of numbers as an argument and then returns 'True' if two consecutive eights are found in the list. For example: [2,3,8,8,9] -> True. The main() function will accept a list of numbers separated by commas from the user and send it to the game_of_eights() function. Within the game_of_eights() function, you will provide logic such that: the function returns True if consecutive eights (8) are found in the list; returns False otherwise. the function can handle the edge case where the last element of the list is an 8 without crashing. the function prints out an error message saying 'Error. Please enter only integers.' if the list is found to contain any non-numeric characters. Note that it only prints the error message in such cases, not 'True' or 'False'. Examples: Enter elements of list separated by commas: 2,3,8,8,5 True Enter elements of list separated by commas: 3,4,5,8 False Enter elements of list separated by commas: 2,3,5,8,8,u Error. Please enter only integers.

User Krjw
by
4.6k points

1 Answer

3 votes

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()

Step-by-step 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

if listitem.isdecimal() == True:

mylist.append(int(listitem))

else:

mylist.append(listitem)

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"

if isinstance(mylist[i],int) == False:

intOnly=False

retVal = "Please, enter only integers"

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

if mylist[i] == 8 and mylist[i+1]==8:

retVal = "True"

break

This returns either True, False or Integer only

return retVal

This calls the main method

if __name__ == "__main__":

main()

User Sam Skirrow
by
4.6k points