Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please answer these questions Section A Answer ALL questions in this section Question 1 (20 marks) A programmer, new to Python, has produced the following

image text in transcribed

please answer these questions

Section A Answer ALL questions in this section Question 1 (20 marks) A programmer, new to Python, has produced the following piece of code to work out and store the first 5 multiples of u in a list by two methods: #Import required external modules import numpy import math #Initialise two strings to hold the calculated values list Pil = (1 list Pi2 = 1 numPi = 0 # initialise numi #Define Functions def appNpiToStr (list, listEntry): Function to append value within function'' list.append(listEntry) def ni (num): Function to calculate new value to be appended outside of function'' numPi = num*maths.pi return listNum = (1,2,3,4,5) listLen = len(listNum) #Attempt 1 using appNpi ToStr for i in range (listLen): appNpiToStr (listPil, listNum[i]*maths.pi) print('The values from appending within the function call are in', listPil) #attempt 2 using ni to calculate n'pi for i in range (listlen): nPi(listNum[i]) listPi2.append(numPi) print(" The values from appending outside the function call are ', listPi2) Unfortunately, this produces the following error: NameError Traceback (most recent call last) in 24 #Attempt 1 using appNpi Tostr 25 for i in range (listLen): ---> 26 appNpiToStr (listPii, listNum[i] *maths.pi) 27 28 print ("The values from appending within the function call are', listPil) NameError: name 'maths' is not defined a) Suggest two different ways to correct this error so that the code runs successfully.(6 marks) b) What is the name that is given to this form of variable naming? (2 marks) ENG1002 Engineering Mathematics and Scientific Computing Practice Paper A c) When code has been amended and runs it produces two quite different results as follows: The values from appending within the function call are [3.141592653589793, 6.283185307179586, 9.42477796076938, 12.566370614359172, 15.707963267948966) The values from appending outside the function call are [0, 0, 0, 0, 0] Clearly the two methods produce very different results. (i) What is the causing the second method to produce a list of zeros (2 marks) (ii) Suggest two simple (do not restructure the code!) changes/ additions to the code to overcome this error. (6 marks) d) Using a numpy function. write a single line of code that prints out an array of the first 5 multiples of a (4 marks)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What are the general types of interviews? Explain each.

Answered: 1 week ago

Question

4. Develop a self-directed learning module.

Answered: 1 week ago

Question

2. Provide recommendations for effective on-the-job training.

Answered: 1 week ago