Search code examples
python-3.xwindowsmodulefloating-pointtypeerror

Why do I get this TypeError using the randint function in Python?


In a main.py file that I got from my university, which is part of an assignment, I get this error message when trying to run the main.py file:

enter image description here

That should not happen. I did write correct code in the places that I should. I should not be getting errors in places that I am not allowed to change. And its a problem only I am having! Other people from my class are not having this problem, even using the same code!

My Python version is: Python 3.12.1

Would really appreciate any help!


Solution

  • random.randint should be called with integer bounds, but 1e15 is a float.

    Try instead:

    randint(0, 10**15)
    

    Since you're calling it multiple times, it might be better to use random.choices instead:

     Ai = random.choices(range(10**15 + 1), k=len(elems))
    

    If you're not allowed to modify the code in main.py, then you should clarify with the instructor which Python version(s) should be used for this assignment, because the code in main.py will not work on Python 3.12 due to the removal of type coercion in randrange:

    Changed in version 3.12: Automatic conversion of non-integer types is no longer supported. Calls such as randrange(10.0) and randrange(Fraction(10, 1)) now raise a TypeError.

    Actually you should probably notify the instructor: mention that some guy on the internet says the code in main.py needs fixing for Python 3.12, and link them to this Q&A.