Home > Software engineering >  I am not able to get how many numbers are less than the limit
I am not able to get how many numbers are less than the limit

Time:09-06

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)

CodePudding user response:

  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.

CodePudding user response:

If your only problem is that you cant get your "limit" value to print at the end of the random numbers, can you not just append it to your print statement as a string? Please let me know if I am not understanding your goal.

print(str(b) " values are less than " str(limit))
  • Related