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

Scenario Based Python Programs With Answer Key

The document presents various Python programming scenarios focused on algorithmic thinking and problem-solving techniques across multiple units. It includes examples such as ATM withdrawal checks, electricity bill calculations, student pass/fail evaluations, and functions for banking operations. Each scenario is designed to illustrate practical applications of Python programming concepts.

Uploaded by

eirikrozar
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)
34 views5 pages

Scenario Based Python Programs With Answer Key

The document presents various Python programming scenarios focused on algorithmic thinking and problem-solving techniques across multiple units. It includes examples such as ATM withdrawal checks, electricity bill calculations, student pass/fail evaluations, and functions for banking operations. Each scenario is designed to illustrate practical applications of Python programming concepts.

Uploaded by

eirikrozar
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

Algorithmic Thinking and Problem Solving

Scenario Based Python Programs


UNIT II – UNIT V (With Answer Key)
UNIT II – Problem Solving Techniques & Algorithmic Thinking
Scenario: ATM Withdrawal – Check whether the withdrawal amount can be processed
based on balance.
balance=int(input("Enter balance: "))
withdraw=int(input("Enter amount: "))
if withdraw<=balance:
print("Transaction Successful")
else:
print("Insufficient Balance")

Scenario: Electricity bill calculation based on unit slabs.


units=int(input("Enter units: "))
bill=0
if units<=100:
bill=units*1
elif units<=200:
bill=100*1+(units-100)*2
else:
bill=100*1+100*2+(units-200)*3
print("Bill Amount:",bill)

Scenario: Student pass/fail based on average of 5 subjects.


marks=list(map(int,input().split()))
avg=sum(marks)/5
print("Pass" if avg>=50 else "Fail")

Scenario: Traffic signal action display.


signal=input("Enter signal: ").lower()
if signal=="red":
print("Stop")
elif signal=="yellow":
print("Ready")
elif signal=="green":
print("Go")

Scenario: Library fine calculation based on number of late days.


days=int(input("Late days: "))
if days<=5:
fine=days*1
elif days<=10:
fine=5*1+(days-5)*2
else:
fine=5*1+5*2+(days-10)*5
print("Fine:",fine)
UNIT III – Introduction to Python Programming
Scenario: Restaurant billing system using menu.
print("[Link] [Link] [Link]")
ch=int(input("Choice: "))
qty=int(input("Quantity: "))
price={1:30,2:40,3:35}
print("Total Bill:",price[ch]*qty)

Scenario: Employee salary calculation.


basic=int(input("Basic Pay: "))
hra=basic*0.2
da=basic*0.1
print("Gross Salary:",basic+hra+da)

Scenario: Login validation system.


user=input("Username: ")
pwd=input("Password: ")
if user=="admin" and pwd=="1234":
print("Login Successful")
else:
print("Invalid Login")

Scenario: Attendance eligibility check.


attendance=int(input("Attendance %: "))
if attendance>=75:
print("Eligible for Exam")
else:
print("Not Eligible")

Scenario: Multiplication table generation.


n=int(input("Enter number: "))
for i in range(1,11):
print(n,"x",i,"=",n*i)
UNIT IV – Python Data Types
Scenario: Student marks analysis using list.
marks=list(map(int,input().split()))
print("Highest:",max(marks))
print("Lowest:",min(marks))

Scenario: Remove duplicate voter IDs using set.


voters=list(map(int,input().split()))
print(set(voters))

Scenario: Shopping cart total calculation.


prices=list(map(int,input().split()))
print("Total:",sum(prices))

Scenario: Word count in a sentence.


s=input()
print("Words:",len([Link]()))

Scenario: Student database using dictionary.


student={"name":"Ravi","marks":85}
print(student)
UNIT V – Functions
Scenario: Bank account system using functions.
balance=0
def deposit(amount):
global balance
balance+=amount
def withdraw(amount):
global balance
if amount<=balance:
balance-=amount
deposit(500)
withdraw(200)
print(balance)

Scenario: Student grade calculation using function.


def grade(mark):
if mark>=90: return "A"
elif mark>=75: return "B"
else: return "C"
print(grade(82))

Scenario: Electricity bill using function.


def bill(units):
if units<=100:
return units*1
elif units<=200:
return 100*1+(units-100)*2
else:
return 100*1+100*2+(units-200)*3
print(bill(250))

Scenario: Recursive factorial function.


def fact(n):
return 1 if n==0 else n*fact(n-1)
print(fact(5))

Scenario: Global vs Local variable demonstration.


x=10
def show():
x=5
print("Inside:",x)
show()
print("Outside:",x)

Common questions

Powered by AI

Understanding global vs local variable scopes in Python is crucial for avoiding unintended side-effects and maintaining predictable behavior. The document illustrates this by showing how variable 'x' inside and outside a function have separate identities unless explicitly declared global, highlighting the need for careful scope management .

The recursive factorial function effectively demonstrates recursion by defining a problem solution (factorial of n) in terms of the solution to its sub-problems (factorial of n-1). It showcases recursive base conditions and iterative calls, converting complex mathematical problems into simpler repetitive tasks .

The scenario uses a simple average calculation across five subjects to determine pass or fail status. By leveraging averages, the problem is broken down into manageable arithmetic operations, streamlining decision-making using conditional statements, illustrating straightforward yet efficient algorithmic thinking .

The restaurant billing system scenario exemplifies modular programming by separating the menu choice from quantity and calculation, allowing individual blocks to function independently. This modularity enables easier updates and maintenance, as each component (menu selection, quantity input, and pricing) can be modified without affecting others drastically .

The electricity bill calculation is tiered based on unit slabs. For usage up to 100 units, the rate is 1 per unit; for 101-200 units, the rate is 2 per unit beyond the first 100 units; and for units above 200, the rate is 3 per unit beyond the first 200 units .

Python data types like lists, sets, and dictionaries offer diverse functionalities: lists handle ordered collections with duplicates, sets manage unique items ideal for eliminating duplicates, and dictionaries provide key-value pairing useful for mapping relationships, each aiding in efficient data manipulation and retrieval .

The traffic signal action display logic relies on direct string input, thus any typing error or unrecognized input results in no action. In dynamic real-world scenarios, such logic must handle variability and errors robustly, possibly through error-handling mechanisms or interfaces that limit input variability .

The ATM withdrawal scenario checks if the withdrawal amount requested is less than or equal to the current balance. If true, it processes the transaction successfully; otherwise, it indicates insufficient balance . This demonstrates decision-making based on conditional checks of available resources.

A login validation system must address security considerations such as preventing brute force attacks, handling input errors, and securing password storage. Furthermore, it should implement features like account lockout after multiple failed attempts and encrypt sensitive information to enhance user security .

Using functions for operations like bank deposit and withdrawal encapsulates repetitive tasks into reusable blocks, leading to cleaner and modular code. It enhances code maintainability and reduces errors as changes can be made within functions without affecting the entire code base .

You might also like