Skip to content

Commit

Permalink
Control Execution
Browse files Browse the repository at this point in the history
  • Loading branch information
saksham-1304 committed Dec 6, 2024
1 parent c790b49 commit 5e5159f
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 04_Conditional_Execution/14_try_except.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
try:
x = input("Enter number:")
x = int(x) + 1
print(x)
except:
print("Invalid Input")

"""
Output 1
Enter number:pp\
Invalid Input
"""

"""
Output 2
Enter number:5
6
"""
20 changes: 20 additions & 0 deletions 04_Conditional_Execution/_07_if_else.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
if condition:
# Code to be implemented later
pass # pass-->is a null operator statement.It doesn't do anything when executed and acts as a placeholder where syntactically some code is required but you don't want to perform ant action.Helps interpreter to pass over without raising any error
"""

x = 10
if x > 5:
print("x is greater than 5")

# The code execution exits the if block after the print statement
print("This line is executed after the if block.")
print("Program continues here...")

"""
Output
x is greater than 5
This line is executed after the if block.
Program continues here...
"""
26 changes: 26 additions & 0 deletions 04_Conditional_Execution/_08_alternate_execution_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Get user input
num = int(input("Enter a number: "))

# Check if the number is even or odd
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")

# Code continues after the if-else block
print("Program continues...")


"""
Output 1
Enter a number: 4
4 is even
Program continues...
"""

"""
Output 2
Enter a number: 3
3 is odd
Program continues...
"""
20 changes: 20 additions & 0 deletions 04_Conditional_Execution/_09_chained_conditionals__1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Chained Conditionals

# Given shirt color
shirt_color = "blue"

# Chained conditionals to determine who gets the shirt

if shirt_color == "red":
print("Give the shirt to John")
elif shirt_color == "green":
print("Give the shirt to Klara")
elif shirt_color == "yellow":
print("Give the shirt to Kathy")
else:
print("Give the shirt back")

"""
Output
Give the shirt back
"""
14 changes: 14 additions & 0 deletions 04_Conditional_Execution/_10_chained_conditionals__2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Code tempelete for chained conditionals

x = 5
if x < 0:
print("x is negative")
elif x == 0:
print("x is zero")
else:
print("x is positve")

"""
Output
x is positve
"""
32 changes: 32 additions & 0 deletions 04_Conditional_Execution/_11_nested_conditionals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Nested Conditionals

# Student's score
score = 32

# Determine the grade based on the score
if score >= 90:
grade = "A"
if score >= 95:
feedback = "Excellent! You're at the top of the class."
else:
feedback = "Great job!Keep up the good work."

elif score >= 80:
grade = "B"
if score >= 85:
feedback = "Well done!You're doing great."
else:
feedback = "Good Effort!You're close to the next grade"
elif score >= 70:
grade = "C"
if score > 75:
feedback = "Not bad,but there's room for improvement"
else:
feedback = "You're almost there! Keep pushing yourself"
else:
grade = "D"
feedback = "You need to work harder to improve your grade"

print("Student's score:", score)
print("Student's grade:", grade)
print("Feedback:", feedback)
47 changes: 47 additions & 0 deletions 04_Conditional_Execution/_12_try_catch_or_try_except.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Try Catch

try:
numerator = int(input("Enter the numerator: "))
denominator = int(input("Enter the denominator: "))
result = numerator / denominator
print("Result:", result)
except ZeroDivisionError as zero_division_error:
print("Error", zero_division_error)


"""
Output 1
Enter the numerator: 7
Enter the denominator: 7
Result: 1.0
"""

"""
Output 2
Enter the numerator: 6
Enter the denominator: 0
Error division by zero
"""

"""
Built-in Exceptions
SyntaxError: Raised when the Python interpreter encounters a syntax error in the code.
IndentationError: Raised when there are incorrect indentation levels in the code.
TypeError: Raised when an operation or function is applied to an object of inappropriate type.
ValueError: Raised when a built-in operation or function receives an argument of the correct type but with an invalid value.
ZeroDivisionError: Raised when division or modulo operation is performed with a divisor of zero.
IndexError: Raised when trying to access an index that does not exist in a sequence.
KeyError: Raised when trying to access a key that does not exist in a dictionary.
FileNotFoundError: Raised when trying to open or access a file that does not exist.
NameError: Raised when a local or global name is not found.
"""
15 changes: 15 additions & 0 deletions 04_Conditional_Execution/_13_try_catch_or_try_except.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
try:
if True:
print("This line has an indentation error.")
except IndentationError as e:
print("An IndentationError occurred",e)


"""
Output
File "c:\Users\HP\Desktop\Codes\Python_Code_Snippets\04_Conditional_Execution\_13_try_catch_or_try_except.py", line 3
print("This line has an indentation error.")
^
IndentationError: expected an indented block
"""

0 comments on commit 5e5159f

Please sign in to comment.