2011-11-20 57 views
1

我正在創建一個圖形程序,根據用戶指定的網格大小繪製100 x 100個正方形。用戶還輸入4種顏色用於要着色的方格(例如,如果他們輸入紅色,綠色,藍色,黃色,方格將按該順序着色,重複該顏色)。來自變量的循環顏色對於graphics.py

是否可以循環用戶給出的變量的顏色?

這是我到目前爲止有:

def main(): 
    print ("Please enter four comma seperated colours e.g.: 'red,green,blue,yellow'\n\ 
    Allowed colours are: red, green, blue, yellow and cyan") 
    col1, col2, col3, col4 = input("Enter your four colours: ").split(',') 
    win = GraphWin ("Squares", 500, 500) 
    colours = [col1, col2, col3, col4] 
    drawSquare (win, col1, col2, col3, col4, colours) 
    win.getMouse() 
    win.close() 


def drawSquare(win, col1, col2, col3, col4, colours): 
    for i in range (4): 
     for j in range (len(colours)): 
      colour = colours[j] 
      x = 50 + (i * 50) 
      circle = Circle (Point (x,50), 20) 
      circle.setFill(colour) 
      circle.draw(win) 

我想我應該以某種方式使用列表,但不能制定出究竟是如何做到這一點。

+0

您的drawSquare函數是使用class Circle?這不是一個約束嗎? – joaquin

回答

0

如果你想選擇4種給定用戶變量的顏色,你可以使用模數。

color_index = user_var % 4 

那麼你可以得到你的顏色爲:

mycolor = colours[color_index] 

另外,如果你想在用戶每次需要它,並對其循環給不同的顏色時,可用的顏色用盡,你可以使用itertools.cycle迭代器:

In [21]: colours = ['r','b','y','c'] 
In [22]: from itertools import cycle 
In [23]: color = cycle(colours) 

In [25]: color.next() 
Out[25]: 'r' 

In [27]: color.next() 
Out[27]: 'b' 

In [28]: color.next() 
Out[28]: 'y' 

In [29]: color.next() 
Out[29]: 'c' 

In [30]: color.next() 
Out[30]: 'r' 

In [31]: color.next() 
Out[31]: 'b' 

這樣你可以得到:

enter image description here

,代碼:

from graphics import GraphWin, Rectangle, Point 
from itertools import cycle 

def main(): 
    print (("Please enter four comma seperated colours e.g.:" 
      "'red,green,blue'\n" 
      "Allowed colours are: red, green, blue, yellow and cyan")) 
    colours = input("Enter your four colours: ").split(',') 
    print ("Please enter gridsize e.g.: '100'") 
    gsize = int(input("Enter gridsize: ")) 
    win_size = 250 
    win = GraphWin("Squares", win_size, win_size) 
    drawSquares(win, gsize, win_size, colours) 
    win.getMouse() 
    win.close() 

def drawSquares(win, gsize, winsize, colours): 
    side = winsize/gsize 
    color = cycle(colours) 
    for row in range(gsize): 
     y1 = row * side 
     y2 = y1 + side 
     for column in range(gsize): 
      x1 = column * side 
      x2 = x1 + side 
      rect = Rectangle(Point(x1, y1), Point(x2, y2)) 
      rect.setFill(color.next()) 
      rect.draw(win) 

main() 
0

我不知道你的問題是什麼,但你CAL可變COL1-COL4,你從來沒有真正使用。此外,循環範圍(len(iterable))是愚蠢的。這是一個簡化版本。

def main(): 
    print ("Please enter four comma seperated colours e.g.: 'red,green,blue,yellow'\n\ 
    Allowed colours are: red, green, blue, yellow and cyan") 
    colours = input("Enter your four colours: ").split(',') 
    win = GraphWin ("Squares", 500, 500) 
    drawSquare (win, colours) 
    win.getMouse() 
    win.close() 


def drawSquare(win, colours): 
    for i in range (4): 
     for colour in colours: 
      x = 50 + (i * 50) 
      circle = Circle (Point (x,50), 20) 
      circle.setFill(colour) 
      circle.draw(win) 

「是否可以循環用戶給出的變量的顏色?」

是的。你已經這麼做了。

+0

感謝您指出我正在創建我沒有使用的變量。如果我想循環四次以上,是否有可能再次從列表開始處開始?例如第五個圓圈將從輸入的第一個顏色開始? – user1056548

+0

@ user1056548您目前正在循環所有顏色4次。爲了改變這種情況,只需在'範圍內(4)'(使用用戶輸入或取決於屏幕/網格大小?)更改'4'即可。 – John