else


Definition
The keyword execute if or elif statement return false.

Syntax
else

Parameters
None

Examples
Here, in this example, the IntegerField validated number entered should be in record and before the Field vale is passed on HttpResponseRedirect in views.py and index.html files.
Steps for creating virtual environment, startproject, startapps, & running server:
  • virtualenv myvirtualenv
  • cd  mysite
  • .\Scripts\activate
  • pip install django
  • python .\Scripts\django-admin.py startproject mysite
  • cd mysite
  • python manage.py startapp myapphttp
  • python manage.py runserver 



setting.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles','myapphttp',
)

urls.py

from django.conf.urls import include, url

from myapphttp.views import response

urlpatterns = [
      url(r'^', response),
]

myapphttp/forms.py

from django import forms



def Validate(value):
    pin=[1101,2945,3635,5550,3334,5553,2822,4624,3221,6453,2223,4456,5462]
    if value not in pin:
        raise ValidationError('%s : The pin number is not in our record.'% value)

class IntegerField(forms.Form):
    number=forms.IntegerField(label='Number' ,validators=[Validate])

myapphttp/views.py

from django.shortcuts import render

from .forms import IntegerField

def response(request):
    if request.method == 'POST':
        form=IntegerField(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('/Thanks-for-submitting-your-request/')
    else:
        form = IntegerField()

    return render(request, 'myapphttp\index.html', {'form':form})


myapphttp/index.html

<!DOCTYPE html>
<html>
<title> </title>
<body>

<form action="/pin-not-found/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

</body>
</html>






The pyramid returns with the if statement execution after testing equation of the values of two variable. 
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

def test(request):
    x=range(1,0)
    y=13
    if y in x:
        return Response('y is in x')
    else:
        return Response('y is not in x')

def main():
    config = Configurator()
    config.add_route('index', '/')                        
    config.add_view(test, route_name='index')
    app = config.make_wsgi_app()
    return app

if __name__ == '__main__':
    app = main()
    server = make_server('0.0.0.0', 1000, app)
    print ('Starting up server on http://localhost:1000')
    server.serve_forever()






if statement (string is decimal): 
 ..............else: .............
if statement test whether string object is decimal or not
 x="122cc"
if x.isdecimal():
    print("yes, you're right")
else:
    print("It is not Decimal")

>>> 
it is not Decimal
>>>


elif statement subject to if argument  and input() number  
Here in this sample code the variable assigned the numeric ranges and and if statement is tested on these variables.
a=range(1,18)
b=range(18,40)
c=range(40,99)
print("Please enter your age")
d=input()
if d is a:
    print("You're authorized to work")
elif d is b:
    print ("You are authorized to work")
else:
    print("You can request accommodation")

>>>
Please enter your age

28
You can request accommodation
>>>


else:....subject to if
for loop subject to break where x is equal 8.
 for x in range(1,10):
    print(x)
    if x==8:
        break
    else:
        print(x)

>>> 
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
>>> 


if....(random.random):
else: ......
The method returns with the value:
import random
x=random.Random()
if x.__ge__(2):
                    print("Random value are not acceptable")
else:
     print(x)
>>> 
Random value are not acceptable
>>>

class....:    def....:   if .....: else: ......
class method executed the if statement about accumulated value of value1 and value2.
 import operator
class add_value:
    def __init__(self,a,b):
        self.a=a
        self.b=b
        self.c=a+b
        if self.c.__lt__(20):
            print("The values added are more than required parameters:",self.c)
        else:
            print("Values added: ", self.c)
                  
                
value1=33
value2=33.1

add_value(value1, value2)

>>> 
Values accepted: 66.1
>>>

class.....: def __init__()......:...if ===str.isidentifier(),str.lower(), str.isapha():...else:.....
The function tested whether the string value is single word, or contain non-string values through input().
 class str_methods():
    def __init__(self, a,b,c):

        identifier=str.isidentifier(a)

        if str.isidentifier(a).__eq__(True):
            print("Full name should not contain special characters or numbers")
            
        else:
            print("Your full not name accepted")

        
      
        alpha=str.isalpha(b)
        
        if alpha.__ne__(False):
            print("Full name accepted")

            lower=str.lower(c)
            if lower.__eq__(str.lower(c)):
                 str.upper(c)
                 print("System convert your name to uppercase:", str.upper(c))
            else:
                 print("Your name accepted")

        else:
            print("Full name can't be one word")

                    

input_text=input("Please enter  your full name: ")

str_methods(input_text, input_text,input_text)
    
          

>>> 
Please enter your full name: Adil22
Full name should not contain special characters or numbers
Full name can't be one word
>>>        


Here is how Error method printed the error message.
 import webbrowser

x=input("Please enter web dress:")

if x.__eq__('www.pythonlake.com'):
    webbrowser.open(x)

    webbrowser.Error("Not allowed to access the file")

>>> 
Please enter web dress:www.amazon.com
Not allowed to access the file
>>> 


if...else:webbrowser.open_new() 
webbrowser.open_new()
 import webbrowser

x=input("Please enter your web address: ")
y=str(x)

if y.startswith("y"):
    print("Access denied")

>>> 
Please enter your web address: yellow.com
Access denied


tkinter.Tk()
tkinter.Label().pack()
if..:print()
else: print()
The method return with:
 from tkinter import Tk
import tkinter


a=tkinter.Label(master,text="hello").pack()

if a=="hello":
    print("Yes, tkinter.Label(master,text='hello').pack() is equal to 'hello'.")
    print("No, tkinter.Label(master,text='hello').pack() is not equal to 'hello'.")
    
master.mainloop()

>>> 
No, tkinter.Label(master,text='hello').pack() is not equal to 'hello'.
>>>
              

Comments