Python Tutorial for Beginners 10 - Python elif and nested if Statements

preview_player
Показать описание
In this Video I am going to show How to use Python IF...ELIF...ELSE Statements and nested IF statements in python. In python One conditional can also be nested within another.
The if statement evaluates a Boolean condition, and if it is True, performs the indented statements(or a block or code); but if the test is False, it does nothing. The if statement can have an optional else part which will be performed if the test result is False.
If you if condition expression contains logical operators then,
In (A and B), if A is false, then so is the expression, and there is no need to evaluate B
In (A or B), if A is true, then so is the expression, and there is no need to evaluate B

★★★Top Online Courses From ProgrammingKnowledge ★★★

★★★ Online Courses to learn ★★★

★★★ Follow ★★★

DISCLAIMER: This video and description contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!

python nested if else
python if statement multiple conditions
python if statement multiple lines
python 3 if else statement
python if elif
finally python
Python IF...ELIF...ELSE Statements
Рекомендации по теме
Комментарии
Автор

the best one out there till now this playlist has been of great help, thank you,
cheers!!

somudutta
Автор

after this video i wrote this code

gender=input('enter you gender [m/f]')
if gender=='m':
    age=int(input('enter your age: '))
    if age>21:
        print('eligible for marriage')
    else:
        print('not eligible for marriage')
elif gender=='f':
    age=int(input('enter your age: '))
    if age>18:
        print('eligible for marriage')
    else:
        print('not eligible for marriage')
else:
    print('enter valid gender')

manohar_reddy_anugu
Автор

n=input("enter the name: ")
if n==['mark', 'tom', 'rose', 'shane']:
can we use this method to prevent the number or elif statements?

sherwinsherry
Автор

Now, you are in a condition to code one of the most basic tool which we build while learning any programming language.









The tool is calculator!
Build a calculator, in which different type of operations can be performed!

rudramishra
Автор

you have used input function for if and elif, and then you have used nested statement without input function.so, what is the difference?thanks..

mahamanoumar
Автор

What if you want it to recognize the name as both 'tom' AND 'Tom'----the one having an upper case letter and the one not?

johnpauledrittbinay
Автор

Took me a while, but it was fun, thanks! :)


age=int(input('What is your age?'))

if age<0:
print('That\'s impossible. Stop lying.')
realAge=int(input('At least give me a realistic answer...'))
if realAge>0:
print('Thanks, I guess I will take that.')
elif age<18:
print('You are not an adult.')
else:
print('You are an adult.')
gender=input('What is your gender?[M/F]')
if gender=='M':
print('You are a man.')
elif gender=='F':
print('You are a woman.')
else:
print('Please type either M or F to spefify your gender.')


enter=input('Press enter to terminate program.')

gsekhon
Автор

ur codes shows invalid syntax everytime especially in this if else condition

ShivaniSingh-jcpg
Автор

Hey, why are you writing comma(, ) in print statement any specific reason?

muralidhara
Автор

from flask import Flask, request, render_template
app=Flask(__name__)

@app.route("/")
def index():
return render_template("index.html")

@app.route("/register", methods=["POST"])
def register():
if not request.form.get("name") or not request.form.get("surname"):
return
return it doesn't go through to this line.what am I doing wrong

Chibvunza
Автор

U guys I need help!! I have a competition tomorrow so any input ASAP would b help!!!

So I have python 2.7.10
I copied and pasted his code and I got:

>>>
Name? mark

Traceback (most recent call last):
File "C:/Python27/ElifStatements2.py", line 1, in <module>
name = input("Name? ")
File "<string>", line 1, in <module>
NameError: name 'mark' is not defined


when i placed raw_input instead of input, it worked, only problem is it gave me this:


name is?mark
('the name entered is', 'mark')
>>>


----how can I get rid of the parenthesis and the single quotations that surrounds mark and the first argument?

johnpauledrittbinay
Автор

I'm getting the same problem that everyone else is getting (see below). I have 3.5.1
Tried many things and unable to figure it out

rileywilkins
Автор

I am trying to evaluate whether a number is even or odd by using the following code

a=int(input("number"))
if (a/2)==0:
print("The number is even")

else:
print("The number is odd")

But its showing me syntax error. Can someone me out?

rohan
Автор

Hi,
i tried running the code:

a = input("name")
if name=="Divya":
print("The name is", a)
elif name!="Divya":
print("the name is not", a)
else:
Print("name is inavlid")

But it is failing. can you please tell why?

divyasinghal
Автор

this is what I wrote (copied)

name=input("name?")
if name=="mark":
   print("the name entered is", name)
elif name=="john":
    print("the name entered is", name)
elif name=="july":
    print("the name entered is", name)
elif name=="tom":
    print("the name entered is", name)
else:
    print("the name entered is not valid")

 result in the shell:

name?tom

Traceback (most recent call last):
  File "C:/Python27/test.py", line 1, in <module>
    name=input("name?")
  File "<string>", line 1, in <module>
NameError: name 'tom' is not defined
>>>
Anyone please help??? why and how to fix, where is the problem
thanks

sammirza
Автор

# list of tallest tower sin the world
x=input("enter tower name : ")
if x=="tokyo" or "tokyo skytree" or "skytree":
print("1st tallest and situated in japan")
elif x=="canton" :
print("2nd tallest and situated in China")
elif x=="CN" or "CN tower":
print("3rd tallest and situated in Canada")
elif x=="Ostankino" :
print("4th tallest and situated in Russia")
elif x=="Oriental" or "Pearl" or "Oriental Pearl" :
print("5th tallest and situated in China")
elif x=="Milad" :
print("6th tallest and situated in Iran")
elif x=="Kuala" or "Lumpur" or "Kuala Lumper":
print("7th tallest and situated in Malaysia")
else:
print("not world's tallest tower")
I MADE THIS PROGRAM USING NESTED IF BUT ITS NOT WORKING... EVERYTIME I'M RUNNING, IT'S SHOWING- 1ST TALLEST AND SITUATED IN JAPAN... how can I rectify this program...do let me know..

chitralalawat
Автор

If only the video was in English, sigh!

rexfarell