Вы можете попробовать это:
from random import randrange
from random import choice
from graphics import color_rgb
def randomShape():
x = randrange (0,400)
y = randrange (0,400)
x2 = randrange(0,400)
y2 = randrange(0,400)
radius = randrange (0,100)
red = randrange (192, 208)
blue = randrange(100,140)
green = randrange(150,175)
shape = ['cirle;','rectangle;']
randomShape = choice(shape)
if randomShape == 'cirle;':
print(randomShape,x,",",y,";",radius,";",red,",",blue,",",green)
elif randomShape != 'cirle;':
print(randomShape,x,",",y,";",x2,",",y2,";",red,",",blue,",",green)
return randomShape
def getColor( colorString ):
tokens = colorString.split(',')
if len( tokens ) == 3:
return color_rgb( int( tokens[0] ), int(tokens[1]), int( tokens[2] ))
elif len( colorString ) > 0:
return colorString.strip()
else:
return 'white'
def main():
kFile = input("Enter the drawing file name to create:")
q = int(input("Enter the number of shapes to make:"))
with open(kFile, "w") as file:
for x in range(q):
file.write(randomShape())
main()
В приведенном выше коде вы должны вернуть значение randomShape
из randomShape()
и записать вывод в файл. И еще одна вещь, на которую следует обратить внимание, это то, что вы должны рассмотреть возможность использования режима добавления, например open(kFile, "a")
, чтобы продолжать добавлять к существующему файлу и не перезаписывать его при каждом запуске функции.