[Tutor] Resending question with smaller file

Tim Golden mail at timgolden.me.uk
Mon Feb 13 11:14:13 EST 2017


On 13/02/2017 16:06, Lisa Hasler Waters wrote:
> Hello Python Tutor,
>
> We are trying to use the random function in the Tkinter module in
> PyCharmEDU 3.5. (on Macs running 10.11.6). However, we get a number of
> error messages:
>
> Here's the code:
>
> from tkinter import *
> import random
> tk = Tk()
>
> canvas = Canvas(tk, width=400, height=400)
> canvas.pack()
>
>
> def random_randrange(height):
>     pass
>
>
> def random_randrange(width):
>     pass
>
>
> def random_rectangle(width, height):
>     x1 = random_randrange(width)
>     y1 = random_randrange(height)
>     x2 = x1 + random.randrange(width)
>     y2 = y1 + random.randrange(height)
>     canvas.create_rectangle(x1, y1, x2, y2)
>     random_rectangle(400, 400)
>
> tk.mainloop()
>
> Here are the error messages:
>
> Traceback (most recent call last):
>   File "/Users/lwaters/PycharmProjects/tkinternew/rectangles.py", line
> 2, in <module>
>     import random
>   File "/Users/lwaters/PycharmProjects/tkinternew/random.py", line 17,
> in <module>
>     randomRects(100)
>   File "/Users/lwaters/PycharmProjects/tkinternew/random.py", line 10,
> in randomRects
>     x1 = random.randrange(150)
> AttributeError: module 'random' has no attribute 'randrange'

You've called your own module "random.py"; call it something else

TJG



More information about the Tutor mailing list