lesson_9_ex_2_17_18

def ex1(x,y,z): #returns (x/y) ** z if y is not equalt to 0
   k = -1
   if y != 0:
      x = float(x)
      k = (x/y)**z
   else: 
      print 'Error'
   return k

def ex2(s,n): #returns the nth character of a given string, or -1 does not correspond to a character
    k =- 1
    if n > len(s) or n < 1:
     return -1
    else:
     return s[n-1],n 

def ex3(): #returns the product of x and y the first time that is positive along with the number of user's attempts
    x = float(raw_input('give 1st number:'))
    y = float(raw_input('give 2nd number:'))
    counter = 0
    while (x * y) >= 0:
        print 'error - give again numbers'
        print
        x = float(raw_input('give again 1st number:'))
        y = float(raw_input('give again 2nd number:'))
        counter += 1
    return (x*y) , counter   

#############################################################

n1 = ex1(4,2,3)  
n2 = ex1(5,0,10)
print n1 , n2
###########
x1,x2 = ex2('python',3)
print ex2('PAOK',13)
print x1,x2
###########
k = ex3()
print 'the returned product equals to %f and user made %d attempts. '  %(k[0] , k[1])

 

Posted in Uncategorized