Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

, * * Instructions: * * , 1 . Complete the tasks below by editing / adding the code in

"
",
"**Instructions:**
",
"1. Complete the tasks below by editing/adding the code in the code cells directly below each question.
",
"2. Submit your completed .ipynb file on the module website under the appropriate link.
",
"
",
"For any queries about this assignment, email Katherine Malan at malankm@unisa.ac.za"
]
},
{
"cell_type": "markdown",
"id": "a90e88af",
"metadata": {},
"source": [
"## Question 1: debugging (9 marks)
",
"The following code is supposed to print out the Fibonacci sequence up to the number of terms specified by the user.
",
"For example, if the user enters 10, the output should be:
",
"
",
"Fibonacci sequence of 10 terms:
",
"0,1,1,2,3,5,8,13,21,34
",
"
",
"However, the program contains a number of errors. Your task is to fix the code until it works correctly.
",
"For each error that you find in the program, add a comment to explain the error."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9a221c9",
"metadata": {},
"outputs": [],
"source": [
"num_terms = input(\"Enter the number of terms: \")
",
"t1=0
",
"t2=1
",
"counter =1 # counter for checking when to stop
",
"if num_terms <=0
",
" print(\"No terms - number is not positive\")
",
"else:
",
" while counter < num_terms:
",
" print(\"Fibonacci sequence of\", num_terms, \" terms:\")
",
" if (counter = num_terms -1): # the last term
",
" print(t1)
",
" else:
",
" print(t1, end =\",\")
",
" next_term = t1+ t2
",
" # update values
",
" t1= t2
",
" t2= next_term"
]
},
{
"cell_type": "markdown",
"id": "48b09078",
"metadata": {},
"source": [
"## Question 2: number of characters and words (5 marks)
",
"Write code to input a sentence from the user and output the number of characters in the sentence as well as the estimated number of words in the sentence (excluding leading or trailing spaces).**Hint**: You can estimate the number of words by counting the number of spaces in the sentence.
"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3076120",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "00a5627a",
"metadata": {},
"source": [
"## Question 3: Ackley's function (8 marks)
",
"Write code to input two numbers from the user (x1 and x2) and then print out the result of the following fuction (known as Ackley's function). You can test your code using the following test cases: f(0,0) $\\approx$ 0, and f(31.5,31.5) $\\approx$ 22.314.**Hint**: In Python, trigonometric functions (such as cos and sin) are defined in the math package.
",
"
",
" $f(x_1,x_2)=-20\\,\\textrm{exp}\\big(-0.2\\sqrt{0.5(x_1^2+ x_2^2)}\\big)-\\textrm{exp}(0.5(\\cos2\\pi x_1+\\cos2\\pi x_2))+20+\\textrm{exp}(1)$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "edc5c278",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "009a4bcc",
"metadata": {},
"source": [

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions