Assignment 2
Assignment 2
Q1. Discuss syntax, logical and runtime error. Write the program and show the output of in which all
error should be shown with proper justification.
Q2. Programs
In the particular example below, the if statement requires a colon (:) at the end. Without it, Python
raises a SyntaxError: Invalid syntax
Logical errors are mistakes in the program’s logic that lead to incorrect behavior or output, despite the
syntax being correct. The code runs but the answer is wrong since the logic is incorrect.
In the particular example below, the code runs but gives the wrong output because the logic (a < b) is
incorrect.
Runtime error in a program is an error that occurs while the program is running after being successfully
compiled. These errors are often caused by invalid operations, such as dividing by zero or accessing a
non-existent index in a list.
In the particular example below, Python raises a ZeroDivisionError at runtime because dividing a
number by zero is not allowed.
Ans 2) Explanation of code: The below two pictures are of the same code with different inputs resulting
in different outputs. The code first takes age as input and if the input is greater than equal to 18 so it
gives you are eligible to vote but if its less than 18 so it gives you are not eligible to vote. The above code
uses if and else conditions.
Ans 3) Explanation of Code: The code takes number 1 and number 2 as num1 and num2 inputs as float
number which accepts all kind of number (decimals and integers). This again takes if and else conditions
into account as if num1 is greater than num2 so larger number is num1, Elif takes if first option is not
met so Elif takes num2 is greater than num1 so larger number is num2. However, when both conditions
are not met it takes the last condition which means both numbers are equal.
Ans 4) Explanation of Code: This code checks the leap year as input in integer format only (year is in
integer format only). This code takes if and else condition as if considers as if the input is divisible by 4
but not by 100 unless its divisible by 400 so it’s categorized as leap year. Else condition says that if, if
condition is not met then it’s not a leap year.