mardi 3 février 2015

Random function with break command in Python



  1. Write a function that accepts 3 numbers and calculates the average of the 3 numbers and raises the average to the second power (returns the average squared).




  2. Write a loop that finds 3 random uniform numbers (0 to 1); sends the 3 numbers to the function and stops the loop when the value of the function is greater than 0.5625




I tried to figure out this 2 things but I am confused a little bit.



import random

a = random.random ()
b = random.random ()
c = random.random ()

def avenum(x1,x2,x3): # the average of the 3 numbers
z = (x1+x2+x3)/3.0
return z

y = avenum(a,b,c)

print 'the average of the 3 numbers = ',y


def avesec(x1,x2,x3): # the average of the second power
d = ((x1**2)+(x2**2)+(x3**2))/3.0
return d

y1 = avesec(a,b,c)

print 'the average of the second power = ',y1




Aucun commentaire:

Enregistrer un commentaire