Question

In: Computer Science

import random #the menu function def menu(list, question): for entry in list: print(1 + list.index(entry),end="") print(")...

import random #the menu function def menu(list, question): for entry in list: print(1 + list.index(entry),end="") print(") " + entry) return int(input(question))

plz explain this code

Solutions

Expert Solution

This code receives the list of menu options and display to the user and asks user to select the option and returns the user selected choice back to the calling function

Sample program:

import random

#the menu function

def menu(list, question):

  for entry in list:

    print(1 + list.index(entry),end="")

    print(") " + entry)

  return int(input(question))

menu(["Add ","Search","Exit"],"Select Choice?")

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

import random # define functions def rollDice(): # function returns a random number between 1 and...
import random # define functions def rollDice(): # function returns a random number between 1 and 6 def userWon(t1, t2): # function accepts player total and computer total # function returns true if player wins # function returns false if computer wins def main(): # each player rolls two Dice player = rollDice() + rollDice() computer = rollDice() + rollDice() # ask the player if they want to roll again again = int(input("Please enter 1 to roll again. Enter 2...
import sys var = 1 print(var) def function1(myVar):     print(myVar)     var = myVar + 1...
import sys var = 1 print(var) def function1(myVar):     print(myVar)     var = myVar + 1     print(var)     function2(var) def function2(myVar):     print(myVar)     var = myVar + 1     print(var)     function3(var) def function3(myVar):     print(myVar)     var = myVar + 1     print(var) def main(argv):     var = 10     print(var)     function1(var) if __name__=="__main__":     main(sys.argv) 1. As the program runs, what is the first line that will be interpreted by Python, and what action will...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2...
import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2 topBound = win.window_height() / 2 bottomBound = -win.window_height() / 2 turtleX = turt.xcor() turtleY = turt.ycor() stillIn = True if turtleX > rightBound or turtleX < leftBound: stillIn = False if turtleY > topBound or turtleY < bottomBound: stillIn = False return stillIn def main(): wn = turtle.Screen() # Define your turtles here june = turtle.Turtle() june.shape('turtle') while isInScreen(wn,june): coin = random.randrange(0, 2) if...
write pseudocode for this program . thank you import random class cal():    def __init__(self, a,...
write pseudocode for this program . thank you import random class cal():    def __init__(self, a, b):        self.a = a        self.b = b    def add(self):        return self.a + self.b    def mul(self):        return self.a * self.b    def div(self):        return self.a / self.b    def sub(self):        return self.a - self.b def playQuiz():    print("0. Exit")    print("1. Add")    print("2. Subtraction")    print("3. Multiplication")    print("4. Division")...
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2:...
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2: print("./") print("*") print(".\\") elif n == 3: print("../") print("./") print("*") print(".\\") print("..\\") elif n == 4: print(".../") annoying_valley(3) print("...\\") elif n == 5: print("..../") annoying_valley(4) print("....\\") elif n == 6: print("...../") annoying_valley(5) print(".....\\") else: print("." * (n - 1) + "/") annoying_valley(n - 1) print("." * (n - 1) + "\\") def annoying_int_sequence(n): if n == 0: return [] elif n == 1: return...
Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
#This function "binarySearchPythonCode" identifies the elements which is present in the start and end values def...
#This function "binarySearchPythonCode" identifies the elements which is present in the start and end values def binarySearchPythonCode(list, Element, startElement, endElement): #Variable which describes whether the element is present or not isPresent = False #condition check for start and end values while startElement<=endElement and not isPresent: #logic for calculating the mid value midElement = startElement + endElement//2 # if condition which checks whether the mid value is same as element to be searched if list[midElement] == Element: #if condition satisfies it...
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given...
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given list a in non-descending order. Precondition: 0 <= l and u < len(a)''' if l < u: mid = (l + u) // 2 three = [a[l], a[mid], a[u]] three.sort() if three[1] == a[l]: pivot_loc = l elif three[1] == a[u]: pivot_loc = u else: pivot_loc = mid a[u], a[pivot_loc] = a[pivot_loc], a[u] pivot = a[u] i = partition(a, l, u - 1, pivot)...
/* print_reverse function that prints the nodes in reverse order if the list is empty print...
/* print_reverse function that prints the nodes in reverse order if the list is empty print nothing */ include <bits/stdc++.h> using namespace std; class Node{     public:     int data;     Node *next;     Node *prev;     Node(int d, Node *p=NULL, Node *n=NULL){         data = d;         prev = p;         next = n;     } }; class DLL{     public:     Node *head;     DLL(){head=NULL;}     void push(int d){         Node *pNode = new Node(d,NULL,head);         if (head != NULL)             head->prev = pNode;         head = pNode;     }     void print_forward(){         Node *pCurr = head;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT