Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I can't get my Python code to work correctly and I was wondering if anyone could help? This is the problem that I am dealing
I can't get my Python code to work correctly and I was wondering if anyone could help? This is the problem that I am dealing with:
Learning Objectives:
- using a for loop to iterate over list elements
- write a helper function is_num() function to check list elements (will use type() as well as iteration through string characters)
- using if/else to return a correct Boolean value
- using Python's type() function to check the type of an object
- assembling the provided instructions into a function
Pictures attached of the problem below.
Learning Goals - using a for loop to iterate over list elements - write a helper function is_num () function to check list elements (will use type () as well as iteration through string characters) - using if/else to retum a correct Boolean value - using Pythoris type () function to check the type of an object - assembling the provided instructions into a function Instructions is_num() Write a function is_num() takes a string and checks if it represents a number (either an integer or a decimal) Write the initial tests that verify its accuracy. For example, \[ \begin{array}{l} \text { assert (is_num("6.7")) } \\ \text { assert(not is num("12r9")) } \end{array} \] isnumstr_list() Write a function is_num_str_list () that expects a parameter main_list (a list). The function checks if each element of the list is a string with a proper integer or float by calling the helper function is_num() (see above). The function should return Falae if the main_list is empty. I Since this is one of the requirements of the function, you need to ensure that you implement it. How would you write an assert to test it? The example below guides you step-by-step through the process. If you understand what needs to happen and how to accomplish it, then you can go directly to writing your function and testing it with the assert statements. Walkthrough Given a sample list [11,2 ', 3 , let's store it as a sample_list. Since our function needs to check that each element of the list is a string, let's loop over each item and display its side: for item in sample list: print (item, "is of type", type (itemi) Notice that inside of our function, if type (item) I= str, then we'd need to return False. We also know, that if we didrit trigger that return, the element stored in item is a string. which we can check using is_num ( ). This means that the basic structure of our function body is as fol ows: for item in : I check eaci element of the provided parameter if type (item) !. Notice that inside of our function, if type(item) l= str, then we'd need to return False. We also know, that if we didrit trigger that return, the element stored in item is a string, which we can check using is_num ( ). This means that the basic structure of our function body is as folows: for item in ... A check each element of the provided parameter if type (itemi + return False if is num(item) _- False: H call the helper function and check if it returned False return False return ... \# if rone of the above returns got triggered, we have a rumeric string as each element - Note that the function should not return True until it has gone through every element of the list. Hint - Remember to check the edge cases. What should happen if the list is emptyStep 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