Write a python function squareall that takes one parameter


Please Read My Descriptions!!!

I failed to answer the following question in my midterm. I am NOT asking you guys to do homework for me. Just want to know how to solve this problem. I only know how to solve this problem using list index, but this solution is not allowed here because the question has stated that I have to include "for x in nums" in the function.

I know that int is immutable, so what can I do? Thanks for the hint of "isinstance", but I'm sorry that we have not learned it before so I cannot use it in exams.

I had just learned how to solve similar problems using index. I think it can work like this:

def square_all(nums): new = [] for x in nums: new.append(x) for i in range(len(new)): if type(new[i]) != list: new[i] = new[i] ** 2 else: square_all(new[i]) return new
It doesn't work well. I think there is something wrong with 'else'. But how should I modify?

Question

Write a python function square_all that takes one parameter, a nested list of integers, and returns a new nested list of integers that is structurally identical to the given list, but in which all of the integers have been squared. Note that the function should no modify its parameter; it should build a new, separate list.

Complete the function by writing anything you think is needed above, within or below the loop. Don't write code outside of the function. Assume that there are no global variables available to you. don't make changes to the code that has been provided.

Example:

square_all([1,2,[3,4]]) = [1,4,[9,16]]
Given code:

def square_all(nums;'nested list of integers') -> 'nested list of integers': for x in nums:

Request for Solution File

Ask an Expert for Answer!!
Python Programming: Write a python function squareall that takes one parameter
Reference No:- TGS02893467

Expected delivery within 24 Hours