entry is invalid, 1 if they are under the age limit or 2 if they are over the age limit. The function is shown below. The variables age and agelimit will both be integers.01 function checkage(age, agelimit):02 if age < 0 then03 return 004 elseif age < 38 then05 return 106 else:07 return 208 endif09 end functionThe code executes, but it does not give the correct result when called with: checkage(30, 20)The function returns 1, but it should return 2. State the type of error in the code and explain the change that needs to be made for the function to work.[3]
Question
entry is invalid, 1 if they are under the age limit or 2 if they are over the age limit. The function is shown below. The variables age and agelimit will both be integers.01 function checkage(age, agelimit):02 if age < 0 then03 return 004 elseif age < 38 then05 return 106 else:07 return 208 endif09 end functionThe code executes, but it does not give the correct result when called with: checkage(30, 20)The function returns 1, but it should return 2. State the type of error in the code and explain the change that needs to be made for the function to work.[3]
Solution
The error in the code is a logical error. The function is not considering the 'agelimit' parameter at all. It's only checking if the 'age' is less than 38 or not, which is not the correct logic as per the problem statement.
To correct the function, you need to compare the 'age' with 'agelimit' instead of 38. Here is the corrected function:
function checkage(age, agelimit):
if age < 0 then
return 0
elif age < agelimit then
return 1
else:
return 2
endif
end function
Now, if you call the function with checkage(30, 20), it will correctly return 2 because 30 is greater than 20.
Similar Questions
Create a function of name check_age(). The function will receive age of a child. If the age is less than 6, then the function return “Invalid age”. If the age is less than 9, then the function return “This age is eligible for primary school”. If the age is greater than or equal to 9, then the function return “This age is not eligible for primary school”.
Problem StatementIn a theme park, only people who meet specific criteria can experience the thrill of the roller coaster. Develop a program that takes age and height as inputs, determining eligibility using logical operators. If the age is 18 or older and the height is 150 cm or taller, output "Allowed!" Otherwise, output "Sorry, Not allowed."Input format :The first line of input consists of an integer, which represents the person's age.The second line of input consists of an integer, which represents the person's height (in cm).Output format :The output displays either "Allowed!" or "Sorry, Not allowed" based on the given conditions.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases will fall under the following constraints:6 ≤ age ≤ 10050 ≤ height ≤ 200Sample test cases :Input 1 :28151Output 1 :Allowed!Input 2 :17151Output 2 :Sorry, Not allowedInput 3 :6200Output 3 :Sorry, Not allowedInput 4 :18150Output 4 :Allowed!
This script checks whether a person is eligible for voting or not. Which variable is most appropriate for line 2?1 age=int(input("Enter your age"))2 if ___ >=18:3 print("_____ for voting")4 ____:5 print("not eligible for voting")agevotenameprint
The following code fragment is intended to keep reading in an integer from the user until the number entered is between 1 and 10 (inclusive). What kind of error does this code contain, and where?a.Logic error, on line 7b.Syntax error, on line 7c.Syntax error, on line 6d.Runtime error, on line 6e.Runtime error, on line 7f.Logic error, on line 6Clear my choice
Choose the code to assign your age into age variable?age=45print(age)age="45 "print(age)age=str(99)print(age)None of the above
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.