Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am not able to get how many numbers are less than the limit

I am trying to make a function that generates a random number from 0 to max (including the max) repeatedly num times. The function returns the count of how many numbers are less than the limit. I am wondering stuck on how to get the limit value to print at the end of the random numbers.

b = 0
def howManyLessThan(maX,num,limit):
    for i in range(num):
        x = random.randrange(0,maX+1)
        print(x)
    if x < limit:
        b+1
    elif x>limit:
        b+0        
howManyLessThan(6, 10, 5)
print(b)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. Your indentation is incorrect. The if and elif have to be inside the for-loop so indent them one more level.
  2. Check the basic syntax on how to change a variable. b+1 does not do anything. b += 1 increases b by 1.
  3. Rather than using a global variable b, return b from your function, once the loop is complete.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading