繁体   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