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

Python Assingnment 2

The document is an assignment on problem solving using conditional statements in Python. It contains 3 questions. Question 1 involves calculating bus fare based on a passenger's age. Question 2 calculates library late fees based on number of days returning a book. Question 3 checks voter eligibility and Aadhar card renewal needs based on a person's age, gender, and birth year. The student provides inputs, algorithms, flowcharts, Python code and test cases to solve each problem.

Uploaded by

Naveen Agarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Python Assingnment 2

The document is an assignment on problem solving using conditional statements in Python. It contains 3 questions. Question 1 involves calculating bus fare based on a passenger's age. Question 2 calculates library late fees based on number of days returning a book. Question 3 checks voter eligibility and Aadhar card renewal needs based on a person's age, gender, and birth year. The student provides inputs, algorithms, flowcharts, Python code and test cases to solve each problem.

Uploaded by

Naveen Agarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Name – Naveen Agarwal

Roll no – 39

Section – S8

Assignment 2 : Problem solving using alternate statements

Aim – Use of conditional statement for solving the problem.

Q1. In a bus, a total of X passengers were travelling. Passengers can be children aged less
than 12 years, senior citizens (above 65), or normal citizens. The ticket fare is categorised
as follows:
i. For normal citizens, it is Rs. xyz.
ii. For children, it is 50% less than what normal citizens fare.
iii. For senior citizens, it is 30% less than what normal citizens fare.
Calculate the total bus fare for the passenger given the age of the passenger.

a. Identify the inputs required to solve the problem.


Ans. The inputs required are the age of the passenger (X), and the fare price of the bus
(NP).

b. Devise a solution and represent the same using flowchart and pseudocode.
Ans. Algorithm –
Flowchart –

c. Develop a program to calculate the total bus fare for the passenger given the age of
the passenger.
Ans. Code-

#Problem 1 - Calculate the fare of passengers


#input - Age of passenger and fare price of bus
#output - The fare price of that passenger

X=int(input('enter the age of the passenger '))


NP=int(input("enter the fare price of bus "))
#checking whether the person is child or senior citizen.
if (X<=12):
FP = NP-(50/100)*NP
elif (X>=65):
FP = NP-(30/100)*NP
else:
FP = NP
# Displaying the result
print (‘the total price of the passenger is ‘,FP)
# End

Output –

Q2. In the library system, the duration for returning books is ten days. If books are returned by the
students within the given duration have no charges. The book returned after the duration will be
fined Rs. Z per day. The book returned after the 20 days of the duration will be accounted for at Rs 3
times more than Z (3 x Z) per day. Accept the duration, the student having the book and calculate the
total amount paid to the library by the student. Identify the inputs required to solve the problem.

Ans. The input required are number of days in returning the book (n) and per day charge of late
returning the book (z).

a. Devise a solution and represent the same using flowchart and pseudocode.
Ans. Algorithm –
Flowchart –

b. Develop a program to calculate the total amount paid to the library by the student.

Ans. Code –

# Problem 2 calculate the charge of renting a book.


# Input – number of days and per day charge of book.
# Output – total charge of returning the book.

n = int(input('number of days in returning the book '))


Z = int(input('charge of late returning the book per day '))
# using conditional statements
total = 0
if n <= 10:
total = 0
elif n <= 20 and n > 10:
total = (n - 10) * Z
else:
total = (n - 20) * 3 * Z
# displaying the output
print('the total price is ',total)
# end
Output-

c. Test the program for the following: A student borrowed three books from the library on
February 23, 2023, and the due date is March 4, 2023. He has returned one book within the
duration. The second book was returned five days after the duration period. The third
returned after 30 days of the duration period.

Ans. First book – number of days < 10

Second book – number of days = 15

Third book – number of days = 30


Hence, the total price is 0+15+90= 105.

Q3. Extend the problem 2 of previous assignment (Assignment 1) as follows:

Assume the population of a city consists of X persons of different age groups. X includes both
males and females. The department can obtain peoples date of birth from the corporation from
which the age can be calculated. (Note: Consider X to be less than or equal to 6)

Assume the election commission of India wants to count the eligible male and female voters
separately for a constituency while preparing for the forthcoming election.

Also, the public welfare department at the same time announces that the Aadhar card has to be
renewed when the kid reaches the age 5 and the biometrics needs to be updated again when
they attain the age of 15 years.

a. Identify the inputs required to solve the problem.

Ans. Current year (Y) , birth year and gender of a person.

b. Devise a solution and represent the same using flowchart and pseudocode.

Ans. Algorithm –
Flowchart –
c. Develop a program to compute the age of every person. Apply the conditional construct
to check whether the person is eligible to vote and also count the number of female and
male voters and display the same.

Ans. Code-
#Problem 3: checking the eligibility of person to vote
#Input:gender and birthyear of 5 persons.
#Output:number of female and male voter and number of aadhar card
renewed.

Y=int(input('current year '))


male=0
female=0
R=0
#first person
Print(‘for the first person’)
p1=int(input('enter 1 for M and 2 for female '))
ap1=int(input('enter the birthyear of person1 '))
ageperson1=Y-ap1
print('the age of person1 is ',ageperson1)
if (p1==1 and ageperson1>=18):
male=1
print('person1 is male and eligible to vote')
if (ageperson1<18 and ageperson1>=5):
print('you are not eligible to vote and biometric needs to be
updated ')
if (ageperson1<=5):
R=1
print('aadhar card has to be renewed')
elif (p1==2 and ageperson1>=18):
female=1
print('person1 is female and eligible to vote')

#second person
print ("now for second person")
p2=int(input('enter 1 for M and 2 for female '))
ap2=int(input('enter the birthyear of person2 '))
ageperson2=Y-ap2
print('the age of person2 is ',ageperson2)
if (p2==1 and ageperson2>=18):
male=male+1
print('person2 is male and eligible to vote')
if (ageperson2<18 and ageperson2>=5):
print ('you are not eligible to vote and biometric needs to be
updated')
if (ageperson2<=5):
R=R+1
print('aadhar card has to be renewed')
elif (p2==2 and ageperson2>=18):
female=female+1
print('person2 is female and eligible to vote')

#third person
print ('now for the third person')
p3=int(input('enter 1 for M and 2 for female '))
ap3=int(input('enter the birthyear of person3 '))
ageperson3=Y-ap3
print('the age of person3 is ',ageperson3)
if (p3==1 and ageperson3>=18):
male=male+1
print('person3 is male and eligible to vote')
if (ageperson3<18 and ageperson3>=5):
print('you are not eligible to vote and biometric needs to be
updated ')
if (ageperson3<=5):
R=R+1
print('aadhar card has to be renewed')
elif (p3==2 and ageperson3>=18):
female=female+1
print('person3 is female and eligible to vote')

#fourth person
print ('now for the fourth person')
p4=int(input('enter 1 for M and 2 for female '))
ap4=int(input('enter the birthyear of person4 '))
ageperson4=Y-ap4
print('the age of person4 is ',ageperson4)
if (p4==1 and ageperson4>=18):
male=male+1
print('person4 is male and eligible to vote')
if (ageperson4<18 and ageperson4>=5):
print('you are not eligible to vote and biometric needs to be
updated ')
if (ageperson4<=5):
R=R+1
print('aadhar card has to be renewed')
elif (p4==2 and ageperson4>=18):
female=female+1
print('person4 is female and eligible to vote')

#fifth person
print ('now for the fifth person')
p5=int(input('enter 1 for M and 2 for female '))
ap5=int(input('enter the birthyear of person5 '))
ageperson5=Y-ap5
print('the age of person5 is ',ageperson5)
if (p5==1 and ageperson5>=18):
male=male+1
print('person5 is male and eligible to vote')
if (ageperson5<18 and ageperson5>=5):
print('you are not eligible to vote and biometric needs to be
updated ')
if (ageperson5<=5):
R=R+1
print('aadhar card has to be renewed')
elif (p5==2 and ageperson5>=18):
female=female+1
print('person5 is female and eligible to vote')

print ('the total number of female voters is ',female)


print ('the total number of male voters is ',male)
print ('the total number of renewals needed is ',R)
Output :

d. Extend Qn.3.c to display whether the Aadhar card has to be renewed or not after
computing the age. Also, count the number of persons need to renew the card.

Ans. Output –
Learning outcome –

● I am able to apply computational thinking to devise solutions to the given problems.

● I learn how to code python programs using conditional statements to solve problems.

You might also like