Examples of if elif else in Python

By kedinn 2013/09/30

I leave basic exercises them in Python if Dahran is counted in all the exercise I use ifs and functions that to us a value returns, boolean, enter data by keyboard. They remember is not simply to copy and ready if not to reason as they work, I hope that it serves to them they do not stop Following to us! until the next one.

IFs practice of 8 Problems

1. To make a program that simulates the process of credit evaluation of a bank. One is due to validate that the minimum age of the person is 18 years, that their income are majors to their debits, and that at the end of the month the person can pay the quota of her loan. The quota is defined as the amount requested between the number of months in which it is desired to pay this loan. To use two functions at least.


def _Cuotas (loan, months):
    return quota def _Prestamo ():
    prestamo=input (“Amount of loan: ?)
    meses=input (“Whichever months of payment: ?)
    print “Amount of payment per month: ?, _Cuotas (loan, months) edad=input (“Enters age: ?) if edad>=18:
    ingresos=input (“Amount of income: ?)
    egresos=input (“Amount of Debits?)
    if ingresos>egresos:
        _Prestamo ()
        print “Its income is not major that its debits? else:
    print “is not adult?


2. To create a function that returns the absolute value of a number if this one is multiple of 3 and of 7. The head of the function must of being:
def valorAbsoluto (I number):
#instrucciones to calculate the absolute value
return result

def valorAbsoluto (I number):
    if numero%3==0 and numero%7==0:
    if numero<0:
        return numero*-1
    elif numero>=0:
        return I number
        return “is not multiple of 3 and 7? numero=input (“Enters I number: ?) print “absolute value: ?, valorAbsoluto (I number)

3. To request a number and to indicate if he is positive or negative. Using at least one function that returns a boolean value.

def Numero (num):
    if num>0:
        return “Positive?
    elif num<0:
        return “Negative? num=input (“Enters I number: ?) print Numero (num)

4. To create a called function xmayor, to which two numbers go to him (the function receives them as parameters) and it says to us through a message if first he were multiple of the second.
ejm. User enters 9
User enters 3
Program says: 9 if he is multiple of 3

def xmayor (num1, num2):
    if num1%num2==0:
        print num1,? is multipli of “, num2
        print num1,? is not multiple of “, num2 num1=input (“First I number: ?) num2=input (“Secondly I number: ?) print xmayor (num1, num2)

5. To create a called function notes, which within her request a note from 0 to 10 and show of the form: “Insufficient?, “Sufficient?, “Well?. Following the following model.
From 0 to 3 he will be “insufficient?,
of 4 to 6 “Sufficient?
from 6 to 10 “Good?.

def notes (note):
    if nota>=0 and nota<=3:
        return “Insufficient?
    elif nota>=4 and nota<=6:
        return “Sufficient?
    elif nota>=6 and nota<=10:
        return “Well? nota=input (“Enters note: ?) print notes (note)

6. To create a called function numbers, to which him pass as parameter an integral number between 0 and 10, and the function will show this number of the form: “zero?, “one?, “two?, “three?.

def numbers (I number):
    if numero==0:
        return “zero?
    elif numero==1:
        return “zero, one, two?
    elif numero==2:
        return “zero, one, two, three?
    elif numero==3:
        return “zero, one, two, three, four?
    elif numero==4:
        return “zero, one, two, three, four, five?
    elif numero==5:
        return “zero, one, two, three, four, five?
    elif numero==6:
        return “zero, one, two, three, four, five, six?
    elif numero==7:
        return “zero, one, two, three, four, five, six, seven?
    elif numero==8:
        return “zero, one, two, three, four, five, six, seven, eight?
    elif numero==9:
        return “zero, one, two, three, four, five, six, seven, eight, nine?
    elif numero==10:
        return “zero, one, two, three, four, five, six, seven, eight, nine, ten? numero=input (“Enters I number: ?) if numero>=0 and numero<=10:
    print numbers (I number) else:
    print “Not this within 0 - 10?

7. To create a program that simulates a dispenser candy machine. That is to say, that waits for the amount of money that accepts (always smaller to 5) and that it allows the user to choose the products that are available later to print the returned one according to the cost from the product. To create three functions:
def to initiate ()
def imprimirProductos ()
def calcularVuelto (amount, precioProducto)

def to initiate ():
    dulce=raw_input (“Chooses letter: ?)
    if dulce== " a?:
        print “returned His: ?, calcularVuelto (amount, precioProducto)
    elif dulce== " b?:
        print “returned His: ?, calcularVuelto (amount, precioProducto)
    elif dulce== " c?:
        print “returned His: ?, calcularVuelto (amount, precioProducto)
    elif dulce== " d?:
        print “returned His: ?, calcularVuelto (amount, precioProducto) def imprimirProductos ():
    print “a) pacifier: 2.5 \ nb) rubbers: 1.4 \ nc) caramel: 4 \ nd) topline: 1.2? def calcularVuelto (amount, precioProducto):
    return I mount-precioProducto print imprimirProductos () monto=input (“Enters amount: ?) if monto<5:
    print to initiate () else:
    print “Amount must be smaller to 5?

8. To implement the functions to add (num1, num2) that the sum of numbers returns both

to remain (num1, num2) that returns the subtraction of numbers both

to multiply (num1, num2) that returns the multiplication of numbers both

to divide (num1, num2) that returns the division of numbers both

to calculate (x, and, z) that prints the result of the equation

  • The program will request 3 numbers, these will be passed as parameters to its function to calculate, which will use the shown equation next and will show

operation python

  • Its program will only be able to use the implemented functions to solve the operations. Thus for example   in order to solve (x + y) it will have to be called the function to add of the way to add (x, y). Thus it will be for all the other operations.
  • The user enters the first value of x, soon the one of and finally the one of z.
  • Ejm.
  • User enters 1   2   3

  • Program: The result of the equation is 0.36
def to add (num1, num2):
    return num1+num2 def to remain (num1, num2):
    return num1-num2 def to multiply (num1, num2):
    return num1*num2 def to divide (num1, num2):
    return num1/num2 def to calculate (x, and, z):
    to resultado=dividir (to multiply (to add (x, y), to remain (and, z)), to add (x ** 2, and ** 2))** 2
    return result x=input (“Enters x: ?) x=x*1.0 y=input (“Enters and: ?) y=y*1.0 z=input (“Enters z: ?) z=z*1.0 print to calculate (x, and, z)