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
Definition of blog? computer science
inysia [295]

Blogs are sites online owned by a person and or a group of people, usually containing things like videos, pictures and overall things that happened in their day. Blogs are updated daily and have explanations or an overall report on what their day was like.

7 0
4 years ago
____ that allow individuals to perform specific tasks are launched via the operating system, such as by using the windows start
schepotkina [342]
Programs that allow individuals to perform specific tasks are launched via the operating system, such as by using the windows start menu on windows computers. Programm is a set of instructions written for a computer.<span>The Start menu contains icons for all installed programs and data collections, usually for programs.</span>
3 0
3 years ago
Unwanted email sent to large groups of people who did not request the communication is called _____
viktelen [127]
Spam mail is the answer
4 0
3 years ago
You found an image on the Creative Commons that you would like to use. How do you give credit to the author? *
Archy [21]
You need to write the author’s name
7 0
3 years ago
Read 2 more answers
Write a java program.
Sliva [168]

Answer:

The source code has been attached to this response. It contains comments explaining each line of the code. Kindly go through the comments.

To run this program, ensure that the file is saved as ArithmeticProcessor.java

Keep editing line 7 of the code to test for other inputs and arithmetic operation.

A sample output has also been attached to this response.

6 0
3 years ago
Other questions:
  • Bullet points on a slide should be limited to _____. 2 4 8 10
    15·2 answers
  • What are technology trends in science check all that apply
    13·1 answer
  • Which term describes encryption that protects the entire original ip packet's header and payload?
    8·2 answers
  • Having a conversation with someone using a cellular phone is an example of _____ transmission.A. full-duplexB. half-duplexC. ana
    14·1 answer
  • Write a program named SortWords that includes a method named SortAndDisplayWords that accepts any number of words, sorts them in
    10·1 answer
  • Who wants to join my go.ogle classroom?
    15·2 answers
  • Haya would like to complemely delete Slide 11 from her presentation so that slides 12–16 become slides 11-15. What is
    9·1 answer
  • Using simplified language and numbers, using large font type with more spacing between questions, and having students record ans
    9·1 answer
  • When should students practice netiquette in an online course? Check all that apply.
    9·1 answer
  • Which of the following types of tasks are comparatively easy for artificial intelligent systems?
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!