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 of the following is a valid IP address that can be used on the Internet (meaning the public addressing scheme)? Group of a
VladimirAG [237]

Answer:

168.16.1.1 is correct.

Explanation:

168.16.1.1 is the legitimate Internet Protocol address that can be used on the Internet.

Internet Protocol 10.10.1.1 and internet Protocol 172.30.1.1 are the private internet protocol addresses so they could not be used on the internet.

The Internet Protocol 234.1.1.1 has been used as a multicast address so they may not be used on the Internet.

7 0
3 years ago
Write a Java statement that declares and creates an array of Strings named Breeds. Your array should be large enough to hold the
Leno4ka [110]

Answer: String[]Breeds = new String[100]

5 0
3 years ago
The next elected president should be someone who can be very nice, kind, stick-up for him/herself, and take care of the people w
PSYCHO15rus [73]

Answer:trump

Explanation:

3 0
2 years ago
Read 2 more answers
Consider the following code: x=random.randint (1, 100) The randint is a ____.​
Mrrafil [7]
Random integer, in this case betweeen 1 and 100
5 0
3 years ago
Write a simple nested loop example that sums the even and odd numbers between 5 and 15 and prints out the computation.
lianna [129]

Answer:

for e in range(6, 15, 2):

for o in range(5, 15, 2):

calc = e+o

print(e, "+", o, "=", calc)

Explanation:

4 0
3 years ago
Other questions:
  • Sean is white hat hacker, who is demonstrating a network level session hijack attacks and as part of it, he has injected malicio
    6·1 answer
  • Which cloud computing service model gives software developers access to multiple operating systems for testing?
    5·1 answer
  • The eastern front was longer than other fronts of the war true or false
    7·2 answers
  • Given the following narrative for Bambino’s Pizzeria, list the actors that should be used in the use-case diagram.
    6·1 answer
  • 2
    9·2 answers
  • Mary from sales is asking about the plan to implement Salesforce.com's application. You explain to her that you are in the proce
    9·1 answer
  • Around what time did the Internet come around and how did it all start? Would like to know more of the history of how the intern
    12·1 answer
  • An online supermarket wants to update its item rates during the sales season. They want to show the earlier rates too but with a
    6·2 answers
  • The user can close all the programs if he closes the open virtual desktop true false
    10·1 answer
  • What is the difference between a status bar, title bar, and tabs?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!