2015-03-02 40 views
0

所以,我試圖將這個Scratch程序轉換爲python程序:http://scratch.mit.edu/projects/1963078/#editor。我目前使用它的圖案3.下面是我有:在python中將float轉換爲十六進制

from turtle import* 
import random 
import math 
turtle = Turtle() 
bgcolor("#000000") 
ht() 
speed(0) 
goto(0,0) 
def draw(): 
    x1 = (random.randint(0,250)) 
    y1 = (random.randint(0,250)) 
    x2 = (random.randint(0,250)) 
    y2 = (random.randint(0,250)) 
    counts = 0 
    while counts < 250: 
     count = 0 
     while count<250: 
      c = (math.tan(math.sqrt(((xcor() - x1)**2) + ((ycor() - y1)**2))))*(math.tan(math.sqrt(((xcor() - x2)**2) + ((ycor() - y2)**2)))) 
      color(c) 
      setx(xcor+1) 
      count += 1 
     counts +=1 
draw() 

我知道有很多人使用Python 2,但我使用3.現在的問題是,當它使用長線生成一個顏色,它返回一個十進制數,當我需要得到一個十六進制代碼。有關如何將其轉換爲像臨時項目那樣的結果的任何提示?謝謝!

+1

你的意思是'十六進制(C)'? – 2015-03-02 18:50:00

+0

爲什麼不單獨生產3種顏色的組件?你可以傳入一個'(r,g,b)'元組或三個獨立的參數。 – 2015-03-02 18:56:05

回答

0

沒有任何將浮點/十進制數轉換爲十六進制的標準方法(通常十六進制僅用於正整數)...因此您必須決定如何將十進制轉換爲正整數。

,但一旦你有一個正整數,你可以轉換成RGB十六進制顏色(例如,#AABBCC)使用:

>>> some_number = 123456 
>>> hex_color = "#%06X" %(some_number,) 
>>> print hex_color 
#01E240 
相關問題