1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Lesechka [4]
3 years ago
15

Write a function called 'game_of_eights()' that accepts a list of numbers as an argument and then returns 'True' if two consecut

ive 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.
Computers and Technology
1 answer:
MrRa [10]3 years ago
7 0

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

You might be interested in
Which tool can capture the packets transmitted between systems over a network?
Dvinal [7]

Packet analyzer is a tool that captures every packet transmitted over a network and analyzes the content. Basically this is used to gather network statistics and troubleshoot network issues.

7 0
3 years ago
Josh is learning about hackers known for causing damage and altering functions of websites. Which type of hacker is he learning
amid [387]

Answer:

WHITE HACKERS

Explanation: When a white hat hacker discovers a vulnerability, they will exploit it only with permission and not tell others about it until it has been fixed. In contrast, the black hat will illegally exploit it or tell others how to do so. The gray hat will neither illegally exploit it nor tell others how to do so.

4 0
3 years ago
Read 2 more answers
When you see ##### in a cell, you should A. increase the cell width. B. increase the cell height. C. decrease the cell width. D.
Usimov [2.4K]
A. Increase the cell width this is because there is not enough room in the cell to display the number that was given. 
4 0
4 years ago
Anyone here good with PS4's?
Nookie1986 [14]
I can help you out ! What do you need to know bud ?
6 0
3 years ago
Read 2 more answers
Software referd to the physical parts of the computer True or False
ikadub [295]

Answer:

False

Explanation:

Software refers to the programs that run on the computer. Hardware refers to the physical part making up the computer.

8 0
4 years ago
Read 2 more answers
Other questions:
  • TCP will guarantee that your packets will arrive at the destination, as long as the connection is still established. True False
    11·1 answer
  • ____________________ software is a type of security software designed to identify and neutralize web bugs, ad-serving cookies, a
    8·1 answer
  • What do the points on this website do?
    14·2 answers
  • Sammy created a new logo for his client enlarged to use on a billboard ad. Now Sammy needs to redo the logo. What should he do t
    5·1 answer
  • How do unstar another user
    14·1 answer
  • (5 pt.) The name of a variable in the C programming language is a string that can contain uppercase letters, lowercase letters,
    15·1 answer
  • Sites like Zillow get input about house prices from a database and provide nice summaries for readers. Write a program with two
    12·1 answer
  • I’m failing this class and i’m stuck on this one, this is the code from the last lesson:
    7·1 answer
  • Express 42 as a product  of its prime factor​
    7·2 answers
  • Feistel proposed that we can approximate the ideal block cipher by utilizing the concept of a __________ cipher, which is the ex
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!