0

This is the code for the program I'm trying to get random coordinates for to draw the random shape now the random color works but when i run the program i get this error:

(Traceback (most recent call last):
  File "/home/04danielbrew/Desktop/Python/Random Coloured Square.py", line 9, in <module>
    randomh=random.randint(1,1080)
AttributeError: 'builtin_function_or_method' object has no attribute 'randint)

And thanks in advance and I don't want the most advanced code as I am only doing GCSE computer science.

from tkinter import*
from random import*
####Set veriables
colors=["mintcream","orangered","gold","coral","red", "orange", "yellow", "green", "blue","violet","midnightblue"]
canvas_height=1080
canvas_width=1920
canvas_colour='black'
line_width=1
randomh=random.randint(1,1080)
randomw=random.randint(1,1920)
randomh2=random.randint(1,1080)
randomw2=random.randint(1,1920)
####Define function
def square(self):
     canvas.create_line(randomh,randomw,randomh2,randomw2, width=line_width,          fill=random.choice(colors))
     canvas.create_line(300,100,400,100, width=line_width, fill=random.choice(colors))
     canvas.create_line(400,100,400,200, width=line_width, fill=random.choice(colors))
     canvas.create_line(400,200,300,200, width=line_width, fill=random.choice(colors))
###Main program
window=Tk()
window.title("Daniel Random Amazballs lines")
canvas=Canvas(bg=canvas_colour,height=canvas_height,width=canvas_width, highlightthickness=0)
canvas.pack()
###Click in window to start
window.bind("<Button-1>",square)
window.mainloop()
Majlik
  • 1,082
  • 1
  • 10
  • 20
Daniel Brew
  • 1
  • 1
  • 2
  • 1
    This would help you >>> http://stackoverflow.com/questions/14985798/python-random-function – Syed Mauze Rehan Dec 15 '14 at 09:59
  • 1
    by `from random import *` you introduce a `random` function and a `randint` function in the namespace. Your `random` does not refer to the module, but to the `random()` function. – Jasper Dec 15 '14 at 10:01

2 Answers2

3

As a best practice, and for Pythonic code, don't from <module> import *.

In this case, you could use from random import randint, choice instead.

Then your calls would look something like: randomh = randint(1,1080) and choice(colors)

The point is that from <module> import * is almost always the wrong way - it pollutes your top-level namespace with everything from <module>.

Explicit is better than implicit

This isn't about advanced code, this is about learning to write code that clearly expresses your intent.

gomad
  • 1,029
  • 7
  • 16
  • still doesnt work the error i am getting is 'Exception in Tkinter callback Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 1487, in __call__ return self.func(*args) File "/home/04danielbrew/Desktop/Python/Random Coloured Square 0.2.py", line 16, in square canvas.create_line(randomh,randomw,randomh2,randomw2, width=line_width, fill=random.choice(colors)) NameError: name 'random' is not defined' and a black screen where the lines show up so i just don't get the lines – Daniel Brew Dec 16 '14 at 11:33
  • it still doesnt work getting same error and thanks for all the help much apreciated – Daniel Brew Dec 17 '14 at 13:18
  • @DanielBrew - looks like you're using random.choice(). You'll need to import that as well. Or, you can say `import random` and then specify random.randint() and random.choice() explicitly, which is **also** clear. – gomad Dec 17 '14 at 13:20
1

Since you are importing everything from random you don't need to call random.randint(), you just have to call randint() direcly.

like so:

randomh = randint(1,1080)
randomw = randint(1,1920)
randomh2 = randint(1,1080)
randomw2 = randint(1,1920)

As gomad said, it's not a good practice importing everything from a module, especially if you are using only a single method.

Also, random is supposed to be used with just import. See the difference in:

import random
random.choice([1,2,3,4])

and

from random import choice
choice([1,2,3,4])

The first one is clear what it is doing, it chooses one element in the array. While the second is unclear, does it chose the array as something? Does it present the choices to the user?

f.rodrigues
  • 3,499
  • 6
  • 26
  • 62