[Python Programming Basics to Advanced] : Nested if-else statement | Lab 06 P-1

preview_player
Показать описание
This Python programming playlist is designed to take beginners with zero programming experience to an expert level. The course covers installation, basic syntax, practical scenarios, and efficient logic building. The course material includes PDF handouts, review questions, and covers a wide range of topics, from data types to advanced functions like Lambda and Recursive functions, Generators, and JSON data parsing.

In last lesson we studied about if and if-else statements. In this lesson we will study about Nested if-else statement in Python. Nested if-else means an if statement inside another if or else statement.

Complete Playlist:

If you have the basic programming knowledge and interested to learn Object-Oriented Programming in Python, check out this playlist:

Lab Manual 06 can be downloaded from here:

Review Questions:
1- Here is another version of the insurance problem:
s=input('Are you married? (m/u):')
g=input('Your gender? (m/f):')
a=int(input('Your age? :'))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>30):
print('Eligible')
else:
print('Not Eligible')
if(g=='f' and a >25):
print('Eligible')
else:
print('Not Eligible')
When I gave these inputs: unmarried, male and 27, the output was "Not Eligible" but printed twice.
Use the debugging tool and identify the reason for 2-times display.

2- Write a program that asks the user to enter his or her month and day of birth (as in the last task discussed in video). Then your program should print the user’s zodiac sign as part of an appropriate output message. The detail of zodiac sign is as under:

Capricorn December 22 to January 19
Aquarius January 20 to February 18
Pisces February 19 to March 20
Aries March 21 to April 19
Taurus April 20 to May 20
Gemini May 21 to June 20
Cancer June 21 to July 22
Leo July 23 to August 22
Virgo August 23 to September 22
Libra September 23 to October 22
Scorpio October 23 to November 21
Sagittarius November 22 to December 21

#PythonProgramming #python #pythontutorial
Рекомендации по теме
Комментарии
Автор

1-
In the first question, in the first else block, there are two independent if blocks, they will both be executed no matter what the inputs are.

2-
m=eval(input('Enter month number: '))
d=eval(input('Enter Day: '))
if (m==1):
if (d<=19):
x='Capricorn'
else:
x='Aquarius'
if (m==2):
if (d<=18):
x='Aquarius'
else:
x='Pisces'
if (m==3):
if (d<=20):
x='Pisces'
else:
x='Aries'
if (m==4):
if (d<=19):
x='Aries'
else:
x='Taurus'
if (m==5):
if (d<=20):
x='Taurus'
else:
x='Gemini'
if (m==6):
if (d<=20):
x='Gemini'
else:
x='Cancer'
if (m==7):
if (d<=22):
x='Cancer'
else:
x='Leo'
if (m==8):
if (d<=22):
x='Leo'
else:
x='Virgo'
if (m==9):
if (d<=22):
x='Virgo'
else:
x='Libra'
if (m==10):
if (d<=22):
x='Libra'
else:
x='Scorpio'
if (m==11):
if (d<=21):
x='Scorpio'
else:
x='Saggitarius'
if (m==12):
if (d<=21):
x='Saggitarius'
else:
x='Capricorn'
print(f"Your Zodiac Sign is {x}.")

fourcloversan
Автор

Q1:
The reason for twice result is that there are two independent IF conditions, to display the output once, the correct one is as follow:
s=input("are you mariiied [m/u]: ")
g=input("write your gender[m/f]: ")
a=int(input("write your age: "))
if(s=="m"):
print("eligible")
else:
if(g=="m" and a>30):
print("eligible")
if(g=="f" and a>25):
print("eligible")
else:
print("not eligible")
Q2:
m=eval(input("enter a month: "))
d=eval(input("enter a day: "))
if(m==1):
if(d<=19):
x="capricon"
else:
x="aquarius"
if(m==2):
if(d<=18):
x="aquarius"
else:
x="pisces"
if(m==3):
if(d<=20):
x="pisces"
else:
x="aries"
if(m==4):
if(d<=19):
x="aries"
else:
x="taurus"
if(m==5):
if(d<=20):
x="taurus"
else:
x="gemini"
if(m==6):
if(d<=20):
x="gemini"
else:
x="cancer"
if(m==7):
if(d<=22):
x="cancer"
else:
x="Leo"
if(m==8):
if(d<=22):
x="Leo"
else:
x="virgo"
if(m==9):
if(d<=22):
x="virgo"
else:
x="libra"
if(m==10):
if(d<=22):
x="libra"
else:
x="scorpio"
if(m==11):
if(d<=21):
x="scorpio"
else:
x="sagittarius"
if(m==12):
if(d<=21):
x="sagittarius"
else:
x="capricon"
print(f"the zodiac sign is {x}")

shahwarsadaqat
Автор

Task1:
In else block there are two independent else statements and both executed at same time, that's why "Not Eligible'" printed twice. correct one is given:
s=input('Are you married:m/u')
g=input('Enter gender m/f:')
a=int(input('Enter your age:'))
if(s=='m'):
print('Elligible')
else:
if(g=='m'and a>30):
print('Elligible')
else:
if(g=='f'and a>25):
print('Elligible')
else:
print('Not elligible')

Task2:
m=input('Enter your birth month:')
d=eval(input('Enter your date of birth:'))

if(m=='january'):
if(d<=19):
x='capricon'
else:
x='aquarius'
if(m=='feburary'):
if(d<=18):
x='aquarius'
else:
x='pisces'
if(m=='march'):
if(d<=20):
x='pisces'
else:
x='aries'
if(m=='april'):
if(d<=19):
x='aries'
else:
x='taurus'
if(m=='may'):
if(d<=20):
x='taurus'
else:
x='gemini'
if(m=='june'):
if(d<=20):
x='gemini'
else:
x='cancer'
if(m=='july'):
if(d<=22):
x='cancer'
else:
x='leo'
if(m=='august'):
if(d<=22):
x='leo'
else:
x='virgo'
if(m=='september'):
if(d<=23):
x='virgo'
else:
x='libra'
if(m=='october'):
if(d<=22):
x='libra'
else:
x='scorpio'
if(m=='november'):
if(d<=21):
x='scorpio'
else:
x='sagittarius'
if(m=='december'):
if(d<=22):
x='sagittaurius'
else:
x='capricon'
print(f'Your zodic sign is {x}.')

abdulrehmankhalid
Автор

1)
This is because the "if(g=='m' and a>30):
" and "if(g=='f' and a >25):" statements are two independent statements. If the (s=='m') condition in the first If statement is false, then the interpreter jumps to the else block, and reads the two if-else statements individually and executes the statements inside according to the validity of the conditions. In the given case (unmarried, male and 27), conditions in both the if statements are false, ultimately, the interpreter executes the print statements in both the else blocks separately hence the two 'Not eligible' outputs.

The correct program will be,

s=input('Are you married? (m/u):')

g=input('Your gender? (m/f):')
a=int(input('Your age? :'))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>30):
print('Eligible')
else:
if(g=='f' and a >25):
print('Eligible')
else:
print('Not Eligible')


2)
d=eval(input('\nEnter your date of birth (1-31):\t'))
m=eval(input('Enter your month of birth (1-12):\t'))
if (m<=0 or m>12 or d<=0
or (d>31 and (m==1 or m==3 or m==5 or m==7 or m==8 or m==10 or m==12))
or (d>30 and (m==4 or m==6 or m==9 or m==11))
or (d>29 and m==2)):
print('\ninvalid date\n')
else:
if (m==12 and d>=22) or (m==1 and d<=19):
zod='Capricon'
else:
if (m==1 and d>=20) or (m==2 and d<=18):
zod='Aquarius'
else:
if (m==2 and d>=19) or (m==3 and d<=20):
zod='Pisces'
else:
if (m==3 and d>=21) or (m==4 and d<=19):
zod='Aries'
else:
if (m==4 and d>=20) or (m==5 and d<=20):
zod='Taurus'
else:
if (m==5 and d>=21) or (m==6 and d<=20):
zod='Gemini'
else:
if (m==6 and d>=21) or (m==7 and d<=22):
zod='Cancer'
else:
if (m==7 and d>=23) or (m==8 and d<=22):
zod='Leo'
else:
if (m==8 and d>=23) or (m==9 and d<=22):
zod='Virgo'
else:
if (m==9 and d>=23) or (m==10 and d<=22):
zod='Libra'
else:
if (m==10 and d>=23) or (m==11 and d<=21):
zod='Scorpio'
else:
if (m==11 and d>=22) or (m==12 and d<=21):
zod='Sagittarius'

print(f'\nYour Zodiac is {zod}.\n')

muhammadalihaider
Автор

Q1:
The reason for displaying output 2 times is that there are two else conditions at the last of the code that are independent and display the output independently. If both the IF conditions are not satisfied after first else condition then it displays "Not Eligible" two times because of two independent else conditions. So, there is no need of second last else condition of "Not Eligible".
The correct one is as under:
s=input('Are you married? (m/u):')
g=input('Your gender? (m/f):')
a=int(input('Your age? :'))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>30):
print('Eligible')
else:
if(g=='f' and a >25):
print('Eligible')
else:
print('Not Eligible')

Q2:
m=eval(input('Enter month number: '))
d=eval(input('Enter Day: '))

if (m==1):
if (d<=19):
x='Capricorn'
else:
x='Aquarius'

if (m==2):
if (d<=18):
x='Aquarius'
else:
x='Pisces'

if (m==3):
if (d<=20):
x='Pisces'
else:
x='Aries'

if (m==4):
if (d<=19):
x='Aries'
else:
x='Taurus'

if (m==5):
if (d<=20):
x='Taurus'
else:
x='Gemini'

if (m==6):
if (d<=20):
x='Gemini'
else:
x='Cancer'

if (m==7):
if (d<=22):
x='Cancer'
else:
x='Leo'

if (m==8):
if (d<=22):
x='Leo'
else:
x='Virgo'

if (m==9):
if (d<=22):
x='Virgo'
else:
x='Libra'

if (m==10):
if (d<=22):
x='Libra'
else:
x='Scorpio'

if (m==11):
if (d<=21):
x='Scorpio'
else:
x='Saggitarius'

if (m==12):
if (d<=21):
x='Saggitarius'
else:
x='Capricorn'

print(f"Your Zodiac Sign is {x}.")

asmaabhatti
Автор

Answer 1:
The the reason for twice times the "Not Eligible " statement is because the last four if:else and if:else conditions have the same indentation. The last if :else condition must have separate indentation.

Answer2:
x = eval(input('Month : '))
y = eval(input('Day: '))
if (x==1 and 1<=y<=19) or (x==12 and 22<=y<=31) :
print('Capricon')
elif x==1 and 20<=y<=31 or (x==2 and 1<=y<=18):
print('Aquarius')
elif(x==2 and 19<=y<=29) or (x==3 and 1<=y<=20):
print('Pisces')
elif(x==3 and 21<=y<=31)or (x==4 and 1<=y<=19):
print('Aries')
elif(x==4 and 20<=y<=30) or (x==5 and 1<=y<=20):
print('Taurus')
elif(x==5 and 21<=y<=31) or (x==6 and 1<=y<=20):
print('Gemini')
elif (x==6 and 21<=y<=30) or (x==7 and 1<=y<=22):
print('Cancer')
elif(x==7 and 23<=y<=31) or (x==8 and 1<=y<=22):
print('Leo')
elif(x==8 and 23<=y<=31) or (x==9 and 1<=y<=22):
print('Virgo')
elif(x==9 and 23<=y<=30) or (x==10 and 1<=y<=22):
print('Libra')
elif(x==10 and 23<=y<=31) or (x==11 and 1<=y<=21):
print('Scorpio')
elif(x==11 and 22<=y<=30) or (x==12 and 1<=y<=21):
print('Sagittarius')

ahmedimran
Автор

Question 01:
The reason for displaying output 2 times is that there are two else conditions at the last of the code that are independent and display the output independently. If both the 'if' conditions are not satisfied after first else condition then it displays "Not Eligible" two times because of two independent else conditions. So, we don't need to write second last else condition of "Not Eligible".
The correct one is as under:
s=input('Are you married? (m/u):')
g=input('Your gender? (m/f):')
a=int(input('Your age? :'))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>30):
print('Eligible')
else:
if(g=='f' and a >25):
print('Eligible')
else:
print('Not Eligible')

Question 02:
m=eval(input('Enter month number: '))
d=eval(input('Enter Day: '))

if (m==1):
if (d<=19):
x='Capricorn'
else:
x='Aquarius'

if (m==2):
if (d<=18):
x='Aquarius'
else:
x='Pisces'

if (m==3):
if (d<=20):
x='Pisces'
else:
x='Aries'

if (m==4):
if (d<=19):
x='Aries'
else:
x='Taurus'

if (m==5):
if (d<=20):
x='Taurus'
else:
x='Gemini'

if (m==6):
if (d<=20):
x='Gemini'
else:
x='Cancer'

if (m==7):
if (d<=22):
x='Cancer'
else:
x='Leo'

if (m==8):
if (d<=22):
x='Leo'
else:
x='Virgo'

if (m==9):
if (d<=22):
x='Virgo'
else:
x='Libra'

if (m==10):
if (d<=22):
x='Libra'
else:
x='Scorpio'

if (m==11):
if (d<=21):
x='Scorpio'
else:
x='Saggitarius'

if (m==12):
if (d<=21):
x='Saggitarius'
else:
x='Capricorn'

print(f"Your Zodiac Sign is {x}.")

noorulhuda
Автор

Q 1
Because last if and else statement is executing saperately. The correct caode is
s=input('Are you married? (m/u):')
g=input('Your gender? (m/f):')
a=int(input('Your age? :'))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>=30):
print('Eligible')
else:
print('Not Eligible')
if(g=='f' and a >=25):
print('Eligible')
else:
print('Not Eligible')
Q2
x = input('Enter your birth month = ')
y = int(input('Enter your birth date = '))
if x=='january':
if y<=19:
z = 'Capricorn'
else:
z = 'Aquarius'
if x=='february':
if y<=18:
z = 'Aquarius'
else:
z = 'Pisces'
if x=='march':
if y<=20:
z = 'Pisces'
else:
z = 'Aries'
if x == 'april':
if y<=19:
z = 'Aries'
else:
z = 'Taurus'
if x == 'may':
if y<=20:
z = 'Taurus'
else:
z = 'Gemini'
if x == 'june':
if y<=20:
z = 'Gemini'
else:
z = 'Cancer'
if x == 'july':
if y<=22:
z = 'Cancer'
else:
z = 'Leo'
if x == 'august':
if y<=22:
z = 'Leo'
else:
z = 'Virgo'
if x == 'september':
if y<=22:
z = 'Virgo'
else:
z = 'Libra'
if x == 'octuber':
if y<=22:
z = 'Libra'
else:
z = 'Scorpio'
if x == 'november':
if y<=21:
z = 'Scorpio'
else:
z = 'Sagittarius'
if x == 'december':
if y<=21:
z = 'Sagittarius'
else:
z = 'Capricorn'

print(z)

aliraza-zlft
Автор

Q:1
Given the code for Q#1 will give "Not Eligible" two times because the last two "if" conditions are independent and they will print their results separately. So we will re write in such a way that last condition will be executed only if second last is false.
The following program is the corrected one:
s=input('Are you married? (m/u):')
g=input('Your gender? (m/f):')
a=int(input('Your age? :'))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>30):
print('Eligible')
else:
if(g=='f' and a>25):
print('Eligible')
else:
print('Not Eligible')

Q:2

M=input("Enter the month: ")
D=int(input("Enter your Date of birth: "))
if (M=='December'):
if D<=21:
Y='Sagittarius'
else:
Y='Capricon'
if (M=='January'):
if D<=19:
Y='Capricon'
else:
Y='Aquarius'
if (M=='Feburary'):
if D<=18:
Y='Aquarius'
else:
Y='Pisces'
if (M=='March'):
if D<=20:
Y='Pisces'
else:
Y='Aries'
if (M=='April'):
if D<=19:
Y='Aries'
else:
Y='Taurus'
if (M=='May'):
if D<=20:
Y='Taurus'
else:
Y='Gemini'
if (M=='June'):
if D<=20:
Y='Gemini'
else:
Y='Cancer'
if (M=='July'):
if D<=22:
Y='Cancer'
else:
Y='Leo'
if (M=='August'):
if D<=22:
Y='Leo'
else:
Y='Virgo'
if (M=='September'):
if D<=22:
Y='Virgo'
else:
Y='Libra'
if (M=='October'):
if D<=22:
Y='Libra'
else:
Y='Scorpio'
if (M=='November'):
if D<=21:
Y='Scorpio'
else:
Y='Sagittarius'
print(f"Your zodiac sign is {Y}.")

irfanshehzada
Автор

Question--1
there are two else conditions which display their output if (if) condition is not met, in first case 'not eligible' is displayed as age is not met and in second else is triggered due to gender.
this issue can be resolved by removing 2nd last else statement.
Question--2
l=eval(input("Enter the month"))
m=eval(input("Enter the day"))
if (l==2):
if m<=18:
print('Your star is Aquarious')
elif m>=19:
print('Your star is Pisces')
if (l==1):
if m<=19:
print('Your star is Capricorn')
elif m>=20:
print('Your star is Aquarious')
if (l==3):
if m<=20:
print('Your star is Pisces')
elif m>=21:
print('Your star is Aries')
if (l==4):
if m<=19:
print('Your star is Aries')
elif m>=20:
print('Your star is Taurus')
if (l==5):
if m<=20:
print('Your star is Taurus')
elif m>=21:
print('Your star is Gemini')
if (l==6):
if m<=20:
print('Your star is Gemini')
elif m>=21:
print('Your star is Cancer')
if (l==7):
if m<=22:
print('Your star is Cancer')
elif m>=23:
print('Your star is Leo')
if (l==8):
if m<=22:
print('Your star is Leo')
elif m>=23:
print('Your star is Virgo')
if (l==9):
if m<=22:
print('Your star is Virgo')
elif m>=23:
print('Your star is Libra')
if (l==10):
if m<=22:
print('Your star is Libra')
elif m>=23:
print('Your star is Scorpio')
if (l==11):
if m<=21:
print('Your star is Scorpio')
elif m>=22:
print('Your star is Sagittarius')
if (l==12):
if m<=21:
print('Your star is Sagittarius')
elif m>=22:
print('Your star is Capricorn')

AliHamza-zwvt
Автор

Question No.01
There are two independent if statements in else statement and both of themm will excute simultaneously.
Question No.02
d=eval(input('day:'))
m=eval(input('month:'))
if (m==1):
if (d<=19):
print("Your Zodiac sign is Capricorn")
else:
print("Your Zodiac sign is Aquarius")
# print("Your Zodiac sign is")
if (m==2):
if(d<=18):
print("Your Zodiac sign is Aquarius")
else:
print("Your Zodiac sign is Pisces")
if (m==3):
if (d<=20):
print("Your Zodiac sign is Pisces")
else:
print("Your Zodiac sign is Aries")
if (m==4):
if (d<=19):
print("Your Zodiac sign is Aries")
else:
print("Your Zodiac sign is Taurus")
if(m==5):
if(d<=20):
print("Your Zodiac sign is Taurus")
else:
print("Your Zodiac sign is Gemini")
if(m==6):
if (d<=20):
print("Your Zodiac sign is Gemini")
else:
print("Your Zodiac sign is Cancer")
if(m==7):
if(d<=22):
print("Your Zodiac sign is Cancer")
else:
print("Your Zodiac sign is Leo")
if(m==8):
if(d<=22):
print("Your Zodiac sign is Leo")
else:
print("Your Zodiac sign is Virgo")
if(m==9):
if(d<=22):
print("Your Zodiac sign is Virgo")
else:
print("Your Zodiac sign is Libra")
if(m==10):
if(d<=22):
print("Your Zodiac sign is Libra")
else:
print("Your Zodiac sign is Scorpio")
if(m==11):
if(d<=21):
print("Your Zodiac sign is Scorpio")
else:
print("Your Zodiac sign is Sagittarius")
if(m==12):
if(d<=21):
print("Your Zodiac sign is Sagittarius")
else:
print("Your Zodiac sign is Capricorn")

mujtabashabbir
Автор

Que # 1:
The reason for displaying output two times is that last two else conditions are independent and display output independently. If both the (if) conditions are not satisfied after first else condition then it displays "Not Eligible" two times because of two independent else conditions. So, there is no need of second last else condition of "Not Eligible". The following program is the corrected one:

s=input('Are you married? (m/u): ')
g=input('Your gender? (m/f): ')
a=int(input('Your age?: '))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>30):
print('Eligible')
if(g=='f' and a>25):
print('Eligible')
else:
print('Not Eligible')



Que # 2:
m=input('Enter your month of birth (lower case): ')
d=int(input('Enter your date of birth: '))
if(m=='december'):
if(d<=21):
e='Sagittarius'
else:
e='Capricon'
if(m=='january'):
if(d<=19):
e='Capricon'
else:
e='Aquarius'
if(m=='february'):
if(d<=18):
e='Aquarius'
else:
e='Pisces'
if(m=='march'):
if(d<=20):
e='Pisces'
else:
e='Aries'
if(m=='april'):
if(d<=19):
e='Aries'
else:
e='Taurus'
if(m=='may'):
if(d<=20):
e='Taurus'
else:
e='Gemini'
if(m=='june'):
if(d<=20):
e='Gemini'
else:
e='Cancer'
if(m=='july'):
if(d<=22):
e='Cancer'
else:
e='Leo'
if(m=='august'):
if(d<=22):
e='Leo'
else:
e='Virgo'
if(m=='september'):
if(d<=22):
e='Virgo'
else:
e='Libra'
if(m=='october'):
if(d<=22):
e='Libra'
else:
e='Scorpio'
if(m=='november'):
if(d<=21):
e='Scorpio'
else:
e='Sagittarius'
print(f'Your zodiac sign is {e}.')

ahmadlatif
Автор

#Q1) :-
The Reason for getting is twice is that the last 2 if statements are independent there is an unnecessary print statement. By removing the unnecessary statement and indenting the last if-else the program can be corrected. The correct one will be



a=int(input('Your age? :'))
if(s=='m'):
    print('Eligible')
else:
    if(g=='m' and a>30):
        print('Eligible')
    else:
        if(g=='f' and a >25):
            print('Eligible')
        else:



#Q2) :-


d=eval(input('Enter Day: '))

if (m==1):
    if (d<=19):
        x='Capricorn'
    else:
        x='Aquarius'

if (m==2):
    if (d<=18):
        x='Aquarius'
    else:
        x='Pisces'

if (m==3):
    if (d<=20):
        x='Pisces'
    else:
        x='Aeries'

if (m==4):
    if (d<=19):
        x='Aeries'
    else:
        x='Taurus'

if (m==5):
    if (d<=20):
        x='Taurus'
    else:
        x='Gemini'

if (m==6):
    if (d<=20):
        x='Gemini'
    else:
        x='Cancer'

if (m==7):
    if (d<=22):
        x='Cancer'
    else:
        x='Leo'

if (m==8):
    if (d<=22):
        x='Leo'
    else:
        x='Virgo'

if (m==9):
    if (d<=22):
        x='Virgo'
    else:
        x='Libra'

if (m==10):
    if (d<=22):
        x='Libra'
    else:
        x='Scorpio'

if (m==11):
    if (d<=21):
        x='Scorpio'
    else:
        x='Saggitarius'

if (m==12):
    if (d<=21):
        x='Saggitarius'
    else:
        x='Capricorn'


abdul-hadi
Автор

Question 1:
The reason of problem of getting "NOT ELIGIBLE" twice is that there are two print statements of "NOT ELIGIBLE". By removing the unnecessary print statement, we can have our answer correctly as follows
s=input('Are you married? (m/u):')
g=input('Your gender? (m/f):')
a=int(input('Your age? :'))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>30):
print('Eligible')

if(g=='f' and a>25):
print('Eligible')
else:
print('Not Eligible')
Question 2
m=eval(input("Enter month: "))
d=eval(input("enter day: "))

if m==1:
if(d<=19):
x="capricorn"
else:
x="aquarias"
if m==2:
if(d<=18):
x="aquarias"
else:
x="pisces"
if m==3:
if(d<=20):
x="pisces"
else:
x="aries"
if m==4:
if(d<=19):
x="aries"
else:
x="tauras"
if m==5:
if(d<=20):
x="tauras"
else:
x="gemini"
if m==6:
if(d<=20):
x="gemini"
else:
x="cancer"
if m==7:
if(d<=22):
x="cancer"
else:
x="leo"
if m==8:
if(d<=22):
x="leo"
else:
x="virgo"
if m==9:
if(d<=22):
x="virgo"
else:
x="libra"
if m==10:
if(d<=22):
x="libra"
else:
x="scorpio"
if m==11:
if(d<=21):
x="scorpio"
else:
x="saggitarius"
if m==12:
if(d<=21):
x="saggitarius"
else:
x="capricorn"
print(f'your zodaic sign is {x}')

talhakamboh
Автор

Q1):
The reason for displaying output two times is that last two else conditions are independent and display output independently. If both the (if) conditions are not satisfied after first else condition then it displays "Not Eligible" two times because of two independent else conditions. So, there is no need of second last else condition of "Not Eligible". The following program is the corrected one:

s=input('Are you married? (m/u): ')
g=input('Your gender? (m/f): ')
a=int(input('Your age?: '))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>30):
print('Eligible')
if(g=='f' and a>25):
print('Eligible')
else:
print('Not Eligible')
​Q2​):
m=input('Enter your month of birth (lower case): ')
d=int(input('Enter your date of birth: '))
if(m=='december'):
if(d<=21):
a='Sagittarius'
else:
a='Capricon'
if(m=='january'):
if(d<=19):
a='Capricon'
else:
a='Aquarius'
if(m=='february'):
if(d<=18):
a='Aquarius'
else:
a='Pisces'
if(m=='march'):
if(d<=20):
a='Pisces'
else:
a='Aries'
if(m=='april'):
if(d<=19):
a='Aries'
else:
a='Taurus'
if(m=='may'):
if(d<=20):
a='Taurus'
else:
a='Gemini'
if(m=='june'):
if(d<=20):
a='Gemini'
else:
a='Cancer'
if(m=='july'):
if(d<=22):
a='Cancer'
else:
a='Leo'
if(m=='august'):
if(d<=22):
a='Leo'
else:
a='Virgo'
if(m=='september'):
if(d<=22):
a='Virgo'
else:
a='Libra'
if(m=='october'):
if(d<=22):
a='Libra'
else:
a='Scorpio'
if(m=='november'):
if(d<=21):
a='Scorpio'
else:
a='Sagittarius'
print(f'Your zodiac sign is {a}.')

waleedraza
Автор

Task#1
The program is showing two times 'Not Eligible' because we have used two independent if statements and if any of two statement is false the program would print 'Not Eligible' and if both are false the program would print it two time. So to overcome this error will use single a dependent if statement as mentioned below:


a=int(input('Your age? :'))
if(s=='m'):
    print('Eligible')
else:
    if(g=='m' and a>30):
        print('Eligible')
    else:
        if(g=='f' and a>25):
            print('Eligible')
        else:

Task#2


if(m==1):
    if(d<=19):
        s='Capricorn'
    else:
        s='Aquarius'
if(m==2):
    if(d<=18):
        s='Aquarius'
    else:
        s='Pisces'
if(m==3):
    if(d<=20):
        s='Pisces'
    else:
        s='Aries'
if(m==4):
    if(d<=19):
        s='Aries'
    else:
        s='Taurus'
if(m==5):
    if(d<=20):
        s='Taurus'
    else:
        s='Gemini'
if(m==6):
    if(d<=20):
        s='Gemini'
    else:
        s='Cancer'
if(m==7):
    if(d<=22):
        s='Cancer'
    else:
        s='Leo'
if(m==8):
    if(d<=22):
        s='Leo'
    else:
        s='Virgo'
if(m==9):
    if(d<=22):
        s='Virgo'
    else:
        s='Libra'
if(m==10):
    if(d<=22):
        s='Libra'
    else:
        s='Scorpio'
if(m==11):
    if(d<=21):
        s='Scorpio'
    else:
        s='Sagittarius'
if(m==12):
    if(d<=21):
        s='Sagittarius'
    else:
        s='Capricorn'

saifullahafzal
Автор

Q#1:The reason of getting Two times 'Not Eligible' is the two independent if-else statements in the else block and they give output independently. As if the both 'if' statements are not satisfied, in the else block, then it will display the output 'Not Eligible ' two times, so there is need of writing the second last else statement in the else block, the correct code is:
s=input('Are you married? (m/u):')
g=input('Your gender? (m/f):')
a=int(input('Your age? :'))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>30):
print('Eligible')
if(g=='f' and a>25):
print('Eligible')
else:
print('Not Eligible')

#Q2:-
print('Chek Your Stare Name')
x=eval(input('Enter Your Date of birth:'))
y=input('Enter your month of birth:')
if (x>=22 and y=='december') or (x<=19 and y=='january'):
sign='Capricon'
elif (x>=20 and y=='january') or (x<=18 and y=='february'):
sign='Aquarius'
elif (x>=19 and y=='february') or (x<=20 and y=='march'):
sign='Pisces'
elif (x>=21 and y=='march') or (x<=19 and y=='april'):
sign='Aries'
elif (x>=20 and y=='april') or (x<=20 and y=='may'):
sign='Taurus'
elif (x>=21 and y=='may') or (x<=20 and y=='june'):
sign='Gemini'
elif (x>=21 and y=='june') or (x<=22 and y=='july'):
sign='Cancer'
elif (x>=23 and y=='july') or (x<=22 and y=='august'):
sign='Leo'
elif (x>=23 and y=='august') or (x<=22 and y=='september'):
sign='Virgo'
elif (x>=23 and y=='september') or (x<=22 and y=='october'):
sign='Libra'
elif (x>=23 and y=='october') or (x<=21 and y=='november'):
sign='Scorpio'
else:
sign='Sagittarius'
print(f'The entered date has Zodiac sign: {sign}')

zohaibkhalid
Автор

ans no 1:
The Reason for getting is twice is that the last 2 if statements are independent there is an unnecessary print statement. By removing the unnecessary statement and indenting the last if-else the program can be corrected. The correct one will be

s=input('Are you married? (m/u):')
g=input('Your gender? (m/f):')
a=int(input('Your age? :'))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>30):
print('Eligible')
else:
if(g=='f' and a>25):
print('Eligible')
else:
print('Not Eligible')



ANS#2
m=eval(input('Enter month number: '))
d=eval(input('Enter Day: '))

if (m==1):
if (d<=19):
x='Capricorn'
else:
x='Aquarius'

if (m==2):
if (d<=18):
x='Aquarius'
else:
x='Pisces'

if (m==3):
if (d<=20):
x='Pisces'
else:
x='Aeries'

if (m==4):
if (d<=19):
x='Aeries'
else:
x='Taurus'

if (m==5):
if (d<=20):
x='Taurus'
else:
x='Gemini'

if (m==6):
if (d<=20):
x='Gemini'
else:
x='Cancer'

if (m==7):
if (d<=22):
x='Cancer'
else:
x='Leo'

if (m==8):
if (d<=22):
x='Leo'
else:
x='Virgo'

if (m==9):
if (d<=22):
x='Virgo'
else:
x='Libra'

if (m==10):
if (d<=22):
x='Libra'
else:
x='Scorpio'

if (m==11):
if (d<=21):
x='Scorpio'
else:
x='Saggitarius'

if (m==12):
if (d<=21):
x='Saggitarius'
else:
x='Capricorn'

print(f'Your Zodiac Sign is {x}')

huzaifasarwar
Автор

Assalam-o-Alaikum sir,
Task no 1:
We are getting 'Not Eligible' statement two times because the last two if-else conditions are displaying their result seperately.We can correct it by removing the print("Not Eligible")using after second if condition.
so the following code is given below:
s=input('Are you married? (m/u):')
g=input('Your gender? (m/f):')
a=int(input('Your age? :'))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>30):
print('Eligible')
else:
if(g=='f' and a>25):
print('Eligible')
else:
print('Not Eligible')
Task no:2
m=input('Enter your brithday month:')
d=eval(input('Enter your brithday :'))
if(m=='December'):
if(d<=21):
x='Sagittarius'
else:
x='Capricorn'
if(m=='January'):
if(d<=19):
x='Capricorn'
else:
x='Aquarius'
if(m=='February'):
if(d<=18):
x='Aquarius'
else:
x='Pisces'
if(m=='March'):
if(d<=20):
x='Pisces'
else:
x='Aries'
if(m=='April'):
if(d<=19):
x='Aries'
else:
x='Taurus'
if(m=='May'):
if(d<=20):
x='Taurus'
else:
x='Gemini'
if(m=='June'):
if(d<=20):
x='Gemini'
else:
x='Cancer'
if(m=='July'):
if(d<=22):
x='Cancer'
else:
x='Leo'
if(m=='August'):
if(d<=22):
x='Leo'
else:
x='Virgo'
if(m=='September'):
if(d<=22):
x='Virgo'
else:
x='Libra'
if(m=='October'):
if(d<=22):
x='Libra'
else:
x='Scorpio'
if(m=='November'):
if(d<=21):
x='Scorpio'
else:
x='Sagittarius'
print(f'Your Zodaic Sign is:{x}')

zainzakir
Автор

#Q1:-
The reason of getting Two times 'Not Eligible' is the two independent if-else statements in the else block and they give output independently. As if the both 'if' statements are not satisfied, in the else block, then it will display the output 'Not Eligible ' two times, so there is need of writing the second last else statement in the else block, the correct code is:
s=input('Are you married? (m/u):')
g=input('Your gender? (m/f):')
a=int(input('Your age? :'))
if(s=='m'):
print('Eligible')
else:
if(g=='m' and a>30):
print('Eligible')
if(g=='f' and a>25):
print('Eligible')
else:
print('Not Eligible')

#Q2:-
print('Chek Your Stare Name')
x=eval(input('Enter Your Date of birth:'))
y=input('Enter your month of birth:')
if (x>=22 and y=='december') or (x<=19 and y=='january'):
sign='Capricon'
elif (x>=20 and y=='january') or (x<=18 and y=='february'):
sign='Aquarius'
elif (x>=19 and y=='february') or (x<=20 and y=='march'):
sign='Pisces'
elif (x>=21 and y=='march') or (x<=19 and y=='april'):
sign='Aries'
elif (x>=20 and y=='april') or (x<=20 and y=='may'):
sign='Taurus'
elif (x>=21 and y=='may') or (x<=20 and y=='june'):
sign='Gemini'
elif (x>=21 and y=='june') or (x<=22 and y=='july'):
sign='Cancer'
elif (x>=23 and y=='july') or (x<=22 and y=='august'):
sign='Leo'
elif (x>=23 and y=='august') or (x<=22 and y=='september'):
sign='Virgo'
elif (x>=23 and y=='september') or (x<=22 and y=='october'):
sign='Libra'
elif (x>=23 and y=='october') or (x<=21 and y=='november'):
sign='Scorpio'
else:
sign='Sagittarius'
print(f'The entered date has Zodiac sign: {sign}')

AsadAli-owie