0% found this document useful (0 votes)
6 views5 pages

Assignment 2

Uploaded by

areebaahmed151
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Assignment 2

Uploaded by

areebaahmed151
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment # 2

Name Areeba Ahmed


Student ID
Submission Date 20th December 2024 Till 10 A.M.

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

Write a program that:


 Takes the user's age as input.
 Uses an if-else condition to check if the user is eligible to vote (age >= 18).
 Outputs whether the user can vote or not.
Write a program that:
 Accepts two numbers as input.
 Uses an if-else condition to find and print the larger number.
 If both numbers are equal, display a message saying "Both numbers are equal."
Write a program that:
 Accepts a year as input.
 Uses if-else to determine if the year is a leap year or not.
(Hint: A year is a leap year if it is divisible by 4, but not divisible by 100 unless it is also divisible
by 400.)
Ans 1) Syntax error is an error in the syntax of a sequence of characters that is intended to be written in
a particular programming language. It’s like a grammatical error in a programming language. These
errors occur when the code does not conform to the rules and grammar of the language. Examples can
be missing brackets, parentheses, semicolons, quotes, incorrect indentations, and misspelled keywords
or identifiers.

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.

This example takes age below 18.


Now it takes age above 18

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.

You might also like