Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"non-integer arg 1 for randrange()" in python libary

I use a randomizer to make random number from 5 to 10. Can't be so hard? I have used it previously on code (+2000 lines of code, too much for here) and no coding errors occurred.

My code is simply easter egg to my game but it broke all my code:

...
def slowp(t):       
    for l in t:
        sys.stdout.write(l)
        sys.stdout.flush()
        x=random.randint(0.1,0.9)
        time.sleep(x)
    print("")

if act=="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>":
    slowp("Hey, that is hello world made in brainfuck!")
...

act is a string whose value is provided by the user simply with act=str(input("type here.")). It is directly done before this part.

Error message:

Traceback (most recent call last):
  File "startgame.py", line 2084, in <module>
    slowp("Hey, that is hello world made in brainfuck!")
  File "startgame.py", line 140, in slowp
    x=random.randint(0.1,0.9)
  File "/usr/lib/python3.4/random.py", line 216, in randint
    return self.randrange(a, b+1)
  File "/usr/lib/python3.4/random.py", line 180, in randrange
    raise ValueError("non-integer arg 1 for randrange()")
ValueError: non-integer arg 1 for randrange()

What is the actual problem?

like image 874
Jabutosama Avatar asked Dec 06 '22 23:12

Jabutosama


1 Answers

You are trying to pass floating point values to random.randint(). That function only takes integers.

You need to use the random.uniform() function instead; it'll produce a uniformly random value between the lower and upper bound (inclusive):

Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.

Demo:

>>> import random
>>> random.uniform(0.1, 0.9)
0.6793304134926453
like image 62
Martijn Pieters Avatar answered Dec 10 '22 10:12

Martijn Pieters