求看错误!代码运行错误

利用pvthon自定义一个实心圆点函数至少设置两个形参:半径颜色color并将半径默认值设为20像素,画笔和填充默认颜色设为红色。另外不管传入的是英文名称,rgb小数值,rgb整数值等不同颜色模式的颜色都能利用turtle库实现不同颜色不同大小的实心圆点绘制。

然后以下是我自己写的代码,不知道为什么我的rgb的整数部分运行不了 画的线是红的 求解答

import turtle as t
def soliDot():
    t.color("red")
    t.colormode(255)
    t.colormode(1.0)
    t.setup(400,500,0,0)
    
    t.fillcolor("black")
    t.begin_fill()
    t.circle(30)
    t.end_fill()
    t.ht()
    t.penup()
    
    t.goto(49,16)
    t.pendown()
    t.color()
    t.begin_fill()
    t.circle(50)
    t.end_fill()
    t.ht()
    t.penup()

    t.goto(70,-20)
    t.pendown()
    t.color(1,0,0)
    t.begin_fill()
    t.circle(30)
    t.end_fill()
    t.ht()
    t.penup()
    
    t.goto(-20,30)
    t.pendown()
    t.color(0,0,255)
    t.begin_fill()
    t.circle(57)
    t.end_fill()
    t.ht()
    t.penup()
    
    t.goto(-50,-12)
    t.pendown()
    t.dot()
    t.ht()
    t.penup()
    
soliDot()
t.done

你的代码中有几个问题,例如在调用t.color()函数时缺少了传递颜色参数,而且colormode()被调用两次但只需要一次。

以下是修复过的代码,其中将函数参数r设为半径,默认值为20,颜色参数color默认为红色。该函数利用传递的颜色参数画一个实心圆点。


import turtle as t

def soliDot(r=20, color="red"):
    t.colormode(255)
    t.color(color)
    t.fillcolor(color)
    t.begin_fill()
    t.circle(r)
    t.end_fill()
    t.ht()

soliDot(30, "black")
soliDot(50)
soliDot(30, (255, 0, 0))
soliDot(57, "#0000FF")
soliDot(color="green")

t.done()

这个代码定义了一个名为soliDot()的函数,可以通过调用该函数来绘制不同大小和颜色的实心圆点。如果未指定半径,则默认值为20。您可以在调用该函数时传递颜色参数,例如字符串“red”、“green”、“blue”、“yellow”等,或者RGB元组,例如(255, 0, 0)代表红色,(0, 255, 0)代表绿色,(0, 0, 255)代表蓝色。您还可以传递十六进制颜色值,例如“#FF0000”代表红色,“#00FF00”代表绿色,“#0000FF”代表蓝色。注意,t.done()应该被调用以保持窗口打开并等待用户关闭。

修改后的代码:


```python
import turtle as t

def soliDot(radius=20, color="red"):
    t.color("red")
    t.colormode(255)
    t.setup(400,500,0,0)
    
    if type(color) == str:
        t.color(color)
    elif type(color) == tuple:
        t.color(color)
    elif type(color) == list:
        t.color(color)
    elif type(color) == float:
        t.color(color)
    elif type(color) == int:
        t.color(color)
    
    t.fillcolor(color)
    t.begin_fill()
    t.circle(radius)
    t.end_fill()
    t.ht()
    t.penup()
    
soliDot()
soliDot(50, "green")
soliDot(30, (255, 0, 0))
soliDot(57, [0, 0, 255])
soliDot(12, 0.5)

t.done()


```
你需要做以下更改:

在函数头部为radiuscolor提供默认值和参数名
删除不必要的代码,例如对colormode的重复设置和对fillcolor和circle颜色的重复设置。
修改函数中的color代码,使用条件语句处理不同类型的颜色输入。判断输入的颜色类型,从而选择正确的t.color方法使用。
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^