Answered step by step
Verified Expert Solution
Question
1 Approved Answer
################################################################################################ Q1. ############################################################################################### PAGE 148 ############################################################### PLEASE JUST ANSWER BONUS 2; Bonus 2) Create a function greater_list, which takes a list myList and a value
################################################################################################
Q1.
###############################################################################################
PAGE 148
###############################################################
PLEASE JUST ANSWER BONUS 2;
Bonus 2) Create a function greater_list, which takes a list myList and a value val as input parameters, and returns a new list that contains all elements from myList that are greater than val. Return None if none of the values are greater than val. Remember your answer from Q.1 and a function append () from Section 2.3. An example from p. 148 might be helpful. Include at least two pytest functions to verify the greater_list function's accuracy. 1. (3 pts) In Section 3.4, the author talks about Python Variables. What kind of statement creates a variable in Python (i.e., what do you need to do to make a new variable exist in your code)? If the user enters no city, the empty list should be returned: >>> cities) Enter city: Clearly, function cities() should be implemented using a loop that interactively asks the user to enter a city in every iteration. Since the number of iterations is not known, we need to use a while loop. The condition of this while loop should check whether the user entered the empty string. That means that the user should be asked to enter the first city before even entering the while loop. We will, of course, also need to ask the user to enter a city in every iteration of the while loop: def cities(): returns the list of cities that are interactively entered by the user; the empty string ends the interactive input 1st = 0 city - input('Enter city: ') #ask user to enter first city while city ! !! # if city is not the flag value 1st.append(city) # append city to list city - input('Enter city: ') # and ask user once again return ist Note that the function uses the accumulator loop pattern to accumulate the cities into a list. In function cities(), there are two input() function calls: one before the while loop statement and one inside the while loop code block. A way to eliminate one of those "redundant" statements and make the code more intuitive is to use an infinite loop and an if statement inside the body of the while loop. The if statement would test whether the user entered the flag value: def cities2(): returns the list of cities that are interactively entered by the user; the empty string ends the interactive input 1st - O while True: #forever repeat: city - input('Enter city: ') #ask user to enter city if city ": return ist # if city is the flag value # return list 1st.append(city) # append city to list When executing function cities20), the last iteration of the while loop is the one during which the user enters the empty string. In this iteration, only "half of the body of the for loop is executed; the statement ist.append(city) is skipped. For this reason, the loop pattern in cities2() is commonly referred to as the loop-and-a-half patternStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started