Examples of if elif else in Python
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): cuota=prestamo/meses 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 () else: 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 else: 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 else: 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â€?: precioProducto=2.5 print â€œreturned His: â€?, calcularVuelto (amount, precioProducto) elif dulce== " bâ€?: precioProducto=1.4 print â€œreturned His: â€?, calcularVuelto (amount, precioProducto) elif dulce== " câ€?: precioProducto=4 print â€œreturned His: â€?, calcularVuelto (amount, precioProducto) elif dulce== " dâ€?: precioProducto=1.2 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 mountprecioProducto 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
 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 num1num2 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)