Python tutorial- conditional statements


Conditional statements

To make decision, python uses conditional statements that comprise of if  else and elif keywords.
For example, we modify the User-Domain.py program to validate the user input:

youremail=raw_input('Enter your e-mail address:')
if not youremail.find('@')==-1:
    username=youremail.split('@')[0]
    domain=youremail.split('@')[1]
    print 'User name:%-15s Domain: %s' % (username,domain)
else:
    print 'You entered an invalid e-mail address.'

If you want to evaluate more than one condition, you can use many elif keywords as possible.
For example, let’s create a program that can print out the grade of a student based on the following conditions:

90>= score <=100 -- Grade A
80>= score <90 -- Grade B
70>= score <80 -- Grade C
60>= score <70 -- Grade D
50>= score <60 -- Grade E
score <50 -- Grade F
 
The code will be:
score=float(raw_input('Enter your total score:'))
print score
if score>=90 and score<=100:
    print 'Grade A'
elif score>=80 and score<90:
    print 'Grade B'
elif score>=70 and score<80:
    print 'Grade C'
elif score>=60 and score<=70:
    print 'Grade D'
elif score>=50 and score<60:
    print 'Grade E'
else:
    print 'Grade F'
 
Note: we use float() built-function to convert the user input value to a float number.
 
You also can put if…else statements in if…else statements to create a nested if…else statement.
For example, the following program will ask the user to input the start time and stop time of using internet service. The amount of money to be charged will be based on the following conditions:
-IF the number of hours =0 then the system will check the number of minutes:
      -IF 0<the number of minutes<20 – amount=500 riel
      -IF the number of minutes>=20 and <40—amount=1000 riel
      -IF >=40—amount= 1500 riel       -Else amount=0
-IF the number of hours >0 –amount=number of hours * 1500+ amount of money based on  number of minutes

  InternetFee.py:
#Let the user input start and stop times
timestart=raw_input('Enter start time(hh:mm):')
timestop=raw_input('Enter stop time(hh:mm):')
#Make sure the use inputs in correct format
if timestart.find(':')!=-1 and timestop.find(':')!=-1:
    timestartlist=timestart.split(':')
    timestoplist=timestop.split(':')
else:
    print 'Invalid input\n'
#Make sure the stop time>=start time and calculate duration accordingly
if int(timestoplist[0])-int(timestartlist[0])==0:
    if int(timestoplist[1])-int(timestartlist[1])>=0:
        no_hours=int(timestoplist[0])-int(timestartlist[0])
        no_minutes=int(timestoplist[1])-int(timestartlist[1])
    else:
        print 'Stop time must be greater than or equals start time.\n'
       
elif int(timestoplist[0])-int(timestartlist[0])>0:
    if int(timestoplist[1])-int(timestartlist[1])>=0:
        no_hours=int(timestoplist[0])-int(timestartlist[0])
        no_minutes=int(timestoplist[1])-int(timestartlist[1])
    else:
       
        no_hours=int(timestoplist[0])-int(timestartlist[0])-1
        no_minutes=int(timestoplist[1])-int(timestartlist[1])+60   
 
else:
    print 'Stop time must be greater than or equals start time.\n'
   
#calculate the amount of money to be charged based on conditions provided
 
if no_hours==0:
    if no_minutes>0 and no_minutes<20:
        amount=500
    elif no_minutes>=20 and no_minutes<40:
        amount=1000
    elif no_minutes>=40:
        amount=1500
    else:
        amount=0
elif no_hours>0:
    if no_minutes>0 and no_minutes<20:
        amount=no_hours*1500+500
    elif no_minutes>=20 and no_minutes<40:
        amount=no_hours*1500+1000
    elif no_minutes>=40:
        amount=no_hours*1500+1500
    else:
        amount=no_hours*1500
#print the results
print 'Service consumption duration:%3d hours+%3d minutes' %(no_hours,no_minutes), print '\nAmount charged: %3d' % amount,' riel'


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.