簡體   English   中英

在python中將float轉換為十六進制

[英]Converting float to hexadecimal in python

所以,我正在嘗試將這個Scratch程序轉換為python程序: http//scratch.mit.edu/projects/1963078/#editor 我目前正在使用模式3.這就是我所擁有的:

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.現在的問題是,當它使用那條長行生成顏色時,它返回一個十進制數,當我需要獲取十六進制代碼時。 關於我如何轉換它以得到像刮刮項目的結果的任何提示? 謝謝!

沒有任何標准方法可以將浮點/十進制數轉換為十六進制(通常十六進制僅用於正整數)...所以你必須決定如何將小數轉換為正整數。

但是,一旦得到正整數,就可以使用以下方法將其轉換為RGB十六進制顏色(例如#AABBCC ):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM