NAME - KIRTI SINGH
ROLL – 28072
PYTHON ASSIGNMENT
MID TERM
PART B
B1. A
Errors in the program and corrected syntax:
1. The condition in the while loop should not be enclosed in square brackets [ ]. Square
brackets create a list in Python, but in this context, it's not necessary.
2. The @ symbol used for incrementing a is incorrect. It should be a += 4 to increment a by 4.
3. The variable A is referenced in the print statement but it hasn't been defined. It should be
lowercase a.
a=9
while a <= 25:
b=9+a
a += 4
c=b+a
print(c)
print(a)
B1. B
water_fountain_price = 390
pet_jar_price = 1200
chair_price = 760
water_fountains_quantity = 7
pet_jars_quantity = 19
chairs_quantity = 5
total_cost_before_discount = (water_fountain_price * water_fountains_quantity) + (pet_jar_price *
pet_jars_quantity) + (chair_price * chairs_quantity)
normal_discount = total_cost_before_discount * 0.02
total_cost_after_discount = total_cost_before_discount - normal_discount
if total_cost_after_discount > 30000:
additional_discount = total_cost_after_discount * 0.04
total_cost_after_discount -= additional_discount
else:
additional_discount = 0
amount_reimbursed = additional_discount
print("Total amount spent by Daniel at the billing counter:", total_cost_after_discount)
print("Amount reimbursed to Daniel:", amount_reimbursed)
B2.
numbers = []
while True:
num = input("Enter a positive number (or type 'done' to finish): ")
if [Link]() == 'done':
break
num = int(num)
if num < 0:
print("Negative number detected. Exiting input loop.")
break
[Link](num)
if numbers:
print("Sum of the positive numbers:", sum(numbers))
print("Maximum value among the positive numbers:", max(numbers))
else:
print("No positive numbers were entered.")
B2.
def main():
numbers = []
while True:
num = input("Enter a positive number (or type 'done' to finish): ")
if [Link]() == 'done':
break
num = float(num)
if num < 0:
print("Negative number detected. Ignoring it.")
else:
[Link](num)
if numbers:
total_sum = sum(numbers)
max_value = max(numbers)
print("Sum of the positive numbers:", total_sum)
print("Maximum value among the positive numbers:", max_value)
else:
print("No positive numbers were entered.")
if __name__ == "__main__":
main()
C1 a.
import math
def triangle_area(base, height):
return 0.5 * base * height
def rectangle_area(length, width):
return length * width
def circle_area(radius):
return [Link] * radius**2
def main():
print("Choose the shape to calculate the area:")
print("1. Triangle")
print("2. Rectangle")
print("3. Circle")
choice = int(input("Enter your choice (1/2/3): "))
if choice == 1:
base = float(input("Enter the base length of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = triangle_area(base, height)
print("Area of the triangle:", area)
elif choice == 2:
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = rectangle_area(length, width)
print("Area of the rectangle:", area)
elif choice == 3:
radius = float(input("Enter the radius of the circle: "))
area = circle_area(radius)
print("Area of the circle:", area)
else:
print("Invalid choice. Please enter 1, 2, or 3.")
if __name__ == "__main__":
main()
C1. B
def main():
print("Welcome to the IoT restaurant at The Leela Palace!")
print("Select your cuisine:")
print("1. South Indian")
print("2. Pancakes")
cuisine_choice = input("Enter your choice (1/2): ")
if cuisine_choice == '1':
print("Select your dish:")
print("1. Dosa")
print("2. Idli")
print("3. Kichidi")
dish_choice = input("Enter your choice (1/2/3): ")
if dish_choice == '1':
dish = "Dosa"
elif dish_choice == '2':
dish = "Idli"
elif dish_choice == '3':
dish = "Kichidi"
else:
print("Invalid choice.")
return
print(f"Dear Guest: You Chose South Indian and you prefer {dish}. It's ordered. Thanks.")
elif cuisine_choice == '2':
print("Select your dish:")
print("1. Oats")
print("2. Honey")
print("3. Apple")
dish_choice = input("Enter your choice (1/2/3): ")
if dish_choice == '1':
dish = "Oats"
elif dish_choice == '2':
dish = "Honey"
elif dish_choice == '3':
dish = "Apple"
else:
print("Invalid choice.")
return
print(f"Dear Guest: You Chose Pancakes and you prefer {dish}. It's ordered. Thanks.")
else:
print("Invalid choice.")
if __name__ == "__main__":
main()
END TERM
B1.
Errors in the program and corrected syntax:
Line 1: str is a keyword in Python and should not be used as a variable name. Changed def vowel(str):
to def vowel(string):.
Line 5: string is not defined; it should be str. Changed for letter in string: to for letter in str:.
Line 7: print is misspelled as pint. Changed pint("The letter ", letter , "is not an vowel") to print("The
letter ", letter , "is not a vowel").
Line 10: for letter on str: is incorrect syntax. It should be for letter in str: to iterate over each
character in the string.
Line 15: input function is nested inside print function, which is unnecessary. Removed print from
choice = int(input(print("Enter your choice - 1 for Vowel, 2 for length"))).
Line 19: elif choice = 2: uses assignment operator instead of comparison operator. Changed elif
choice = 2: to elif choice == 2:
B2
Errors in the program and corrected syntax:
Line 4: Typo in the loop definition. It should be for i in range(1, num + 1): instead of for i n
range(1,num+1):.
Line 5: Typo in the append function. It should be append instead of appand. Changed
[Link](val) to [Link](val).
Line 6: Incorrect syntax for accessing elements of a list. It should be amountlist[k] instead of
amountlist(k).
Line 8: Print function is not capitalized correctly. It should be print instead of Print.
B3.
Line 7: There's a space before the minus sign in a = - 5. It should be a -= 5 to decrement a by 5.
Line 9: The multiplication symbol x is used instead of *. It should be b *= 2 to double the value of b.
Line 11: A is referenced in the expression c = c + i + A, but A is not defined. It should be lowercase a.
Line 12: pint function is misspelled. It should be print.
B4.
start = int(input("Starting number: "))
end = int(input("Ending number: "))
divisible_numbers = []
for num in range(start, end + 1):
if num % 2 == 0 and num % 4 == 0 and num % 6 == 0:
divisible_numbers.append(num)
print("Numbers divisible by 2, 4, and 6:", end=" ")
for num in divisible_numbers:
print(num, end=" ")
if divisible_numbers:
sum_of_numbers = sum(divisible_numbers)
product_of_numbers = 1
for num in divisible_numbers:
product_of_numbers *= num
print("\nSum of the divisible numbers:", sum_of_numbers)
print("Product of the divisible numbers:", product_of_numbers)
else:
print("\nNo numbers found divisible by 2, 4, and 6 in the given range.")
C1. A
room_rent_per_night = 2800
initial_stay_days = 9
advance_payment = 14000
additional_days = 13 - initial_stay_days
additional_rent_per_night = 400
discount_percentage = 6
total_room_rent_madhu = (initial_stay_days * room_rent_per_night) + (additional_days *
(room_rent_per_night + additional_rent_per_night))
total_room_rent_madhu = total_room_rent_madhu - ((total_room_rent_madhu *
discount_percentage) / 100)
total_room_rent_prakash = total_room_rent_madhu
total_amount_spent = total_room_rent_madhu + total_room_rent_prakash
amount_more_given_by_madhu = total_amount_spent / 2 - advance_payment
print("Total amount spent by both Madhu and Prakash:", total_amount_spent)
print("Amount more given by Madhu than Prakash:", amount_more_given_by_madhu)
C1. B
def calculate_marks(subject_marks):
total_marks = sum(subject_marks)
min_mark = min(subject_marks)
max_mark = max(subject_marks)
average_mark = total_marks / len(subject_marks)
return total_marks, min_mark, max_mark, average_mark
subject_marks = {'Maths': 85, 'Science': 90, 'English': 78, 'History': 88, 'Geography': 82}
total_marks, min_mark, max_mark, average_mark = calculate_marks(subject_marks.values())
with open('marks_calculations.txt', 'w') as file:
[Link](f"Total Marks: {total_marks}\n")
[Link](f"Minimum Mark: {min_mark}\n")
[Link](f"Maximum Mark: {max_mark}\n")
[Link](f"Average Mark: {average_mark}\n")
print("Calculations have been written to marks_calculations.txt file.")
C2
sentence = input("Enter a sentence: ")
letters = ""
digits = ""
for char in a sentence:
if [Link]():
letters += char
elif [Link]():
digits += char
with open('[Link]', 'w') as letters_file:
letters_file.write(letters)
with open('[Link]', 'w') as digits_file:
digits_file.write(digits)
print("Letters have been written to [Link] file.")
print("Digits have been written to [Link] file.")