0% found this document useful (0 votes)
13 views

Assignment #01

This document contains Hamza Hasan Zia's responses to three questions for Assignment #01 in CS1A-043. Q1 defines and provides examples of three types of programming errors: syntax errors, runtime errors, and logic errors. Q2 asks to rewrite some Python code to handle multiple alternatives. Q3 asks to identify common built-in Python functions used in for loops for data processing.

Uploaded by

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

Assignment #01

This document contains Hamza Hasan Zia's responses to three questions for Assignment #01 in CS1A-043. Q1 defines and provides examples of three types of programming errors: syntax errors, runtime errors, and logic errors. Q2 asks to rewrite some Python code to handle multiple alternatives. Q3 asks to identify common built-in Python functions used in for loops for data processing.

Uploaded by

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

Hamza Hasan Zia

CS1A-043
Assignment # 01

Q1. Discuss how programming errors can be classified into three distinct
types: syntax errors, runtime …… errors, and logic errors, and explain the
key characteristics of each. Provide a Python code snippet …… that
demonstrates each of these error types.

Syntax Errors:
The error which is in the construction of codes written and
when the code does not follow the structure of the programming language.They
are detected by the compiler or interpreter at. the code compilation or
interpretation.

if x=5
print('x =',x)

Like in this code as I didn’t put colon after the if statement


at the end it will not proceed further than that and gonna show me a syntax
error that something is expected at the end of if statement.

Logic Errors:
Logic errors, occur when the code's logic is incorrect, which
leads to disruptive program behavior. The code may run without error messages,
but it won’t produce the expected output.These types of errors are the most
difficult to identify and correct because they involve incorrect program design or
algorithm.

radius = 3
area = 3.14 * radius * radius
print("Area of the circle:", area)

Like using this code the area calculated of the circle would be
wrong because the radius r is not multiplied by itself rather than it has power
exponent 2 like this r2 so the error is in the logic the formula is wrong and the
output would be wrong in this way.

1
Runtime Errors:
Runtime errors happen during the execution of the
program.These errors are not caught by the compiler or interpreter and may
cause the program to fully terminate.The most common runtime error is
division by zero.

x=5
y=0
result = x / y

When this code will be executed it will give an error saying Zero Division Error:
during the execution and it will not be executed.

Q2. Rewrite the following Python code to convert it into the preferred
format for handling multiple ……alternatives.

# Prompt the user to enter weight in pounds


weight = float(input("Enter weight in pounds: "))

# Prompt the user to enter height in inches


height = float(input("Enter height in inches: "))

Kg_per_lbs = 0.45359237
meters_per_inch = 0.0254

# Compute BodyMassIndex

weightInKg = weight * Kg_per_lbs


heightInMeters = height * meters_per_inch
bmi = weightInKg / (heightInMeters**2)

# Display result

print("BMI is =", format(bmi, ".2f"))

if bmi < 18.5:


print("Underweight")
elif bmi < 25:
print("Normal")
elif bmi < 30:
print("Overweight")
else:
print("Obese")

2
Q3. Identify and list some common built-in Python functions that are
frequently used within for loop.scenarios for data processing and
manipulation.

These are some commonly used functions which are used in for loop
scenarios:

range( ): Used to generate a sequence of numbers, which is often used as an


iterable in for loops.

len( ): Returns the length (number of elements) of a sequence, such as a list or


a string.

break: a conditional function used to terminate the loop at certain iteration or


after receiving the output in a searching kind of program.

Continue: a function used during the loop to skip the remaining code during
current iteration only.

sum( ): sum function Calculates the sum of all the elements in an iterable,
such as a list of numbers.

min( ) and max( ): is used Find the minimum and maximum values in an
iterable, respectively.

any( ) and all( ): Check if any or all elements in an iterable satisfy a given
condition, respectively..

You might also like