KC43
KC43

Reputation: 79

Input fill color Python turtle not executing

I am new to this, so forgive me if the solution is simple -- I just can't figure it out. I am to have the user input number of sides, length of sides, outline color, and fill color -- all of which is working, except the fill color. Below is the code I have.

import turtle 
wn = turtle.Screen()
kyle = turtle.Turtle()

kyle.begin_fill()
sides = int(input("Enter the number of sides of the polygon "))
len = int(input("Enter the length of the sides of the polygon "))
extcolor = input("Enter the color you would like the polygon to be ")
intcolor = input("Enter the fill color you would like the polygon to be ")

kyle.color(extcolor)
kyle.fill(intcolor)

kyle.end_fill()

for i in range(int(sides)):
    kyle.forward (int(len))
    kyle.left (int(360)/int(sides))

Upvotes: 0

Views: 973

Answers (4)

Middle Schooler
Middle Schooler

Reputation: 3

I realize this is a late answer, but from what I understand: One: You may be formatting the color incorrectly, as in: The input may be "Teal", but in color code, there is only "teal". So, I would suggest you use RGB.

Therefore, the code will work with the following: I tried to make this as compact as possible.

import turtle

Win = turtle.Screen()
T = turtle.Turtle()
turtle.colormode(255)
T.pensize(2)
# Assign the colors:
R = int(input("Line: Enter the Red Value from 0 to 255: "))
G = int(input("Line: Enter the Green Value from 0 to 255: "))
B = int(input("Line: Enter the Blue Value from 0 to 255: "))
R2 = int(input("Fill: Enter the Red Value from 0 to 255: "))
G2 = int(input("Fill: Enter the Green Value from 0 to 255: "))
B2 = int(input("Fill: Enter the Blue Value from 0 to 255: "))
T.color((R, G, B), (R2, G2, B2))
T.begin_fill()
Sides = int(input("Enter the number of sides in the Polygon: "))
Length = int(input("Enter the length of the sides of the Polygon: "))
for i in range(Sides):
    T.forward(Length)
    T.left(360 / Sides)
T.end_fill()

And this should work as such: Input:

Line: Enter the Red Value from 0 to 255: 0
Line: Enter the Green Value from 0 to 255: 255
Line: Enter the Blue Value from 0 to 255: 0
Fill: Enter the Red Value from 0 to 255: 255
Fill: Enter the Green Value from 0 to 255: 0
Fill: Enter the Blue Value from 0 to 255: 0
Enter the number of sides in the Polygon: 5
Enter the length of the sides of the Polygon: 100

Output: Link: (I can't add the photo directly as I don't have the reputation yet.)

Output of the program.

Upvotes: 0

Red
Red

Reputation: 27567

A few things to note:

  • The Screen wasn't necessary because you haven't passed any attributes to it, so it was the equivalent of not calling it.
  • Since len is a built in method, it is a bad practice to name any variable len, so I changed it to size.
  • It is easy to tell where to begin_fill() and end_fill(), in this case, simply sandwich all the turtle's moves with them!
  • 360 is already an integer, no need to convert it.
  • There is no need to convert the sides and size to integers twice.
  • You may want to let the user first input the value, then create the turtle so th turtle window wouldn't bother the user.

import turtle 

sides = int(input("Enter the number of sides of the polygon "))
size = int(input("Enter the length of the sides of the polygon "))
extcolor = input("Enter the color you would like the polygon to be ")
intcolor = input("Enter the fill color you would like the polygon to be ")

kyle = turtle.Turtle()
kyle.color(extcolor,intcolor) # First argument line color, second fill color
kyle.begin_fill() # Begin fill
for i in range(sides):
    kyle.forward(size)
    kyle.left (360/sides)
kyle.end_fill() # End fill

Output:

Enter the number of sides of the polygon 5
Enter the length of the sides of the polygon 100
Enter the color you would like the polygon to be red
Enter the fill color you would like the polygon to be green
>>> 

turtle-graphics

Upvotes: 0

cdlane
cdlane

Reputation: 41872

Some notes on your code: you don't need to invoke int() twice on each of your inputs; you don't need to invoke int() at all on a number like 360; don't use Python built-in function names like len as your variable names, as it redefines the original function; The turtle color() method can change both the pen color and the fill color -- if you want just one or the other, use pencolor() and fillcolor(); if you put your first turtle commands after the last input() calls, console users won't have to click back to the console window.

Your code with the above changes (and a few others):

from turtle import Screen, Turtle

sides = int(input("Enter the number of sides of the polygon: "))
length = int(input("Enter the length of the sides of the polygon: "))
extcolor = input("Enter the color you'd like the polygon to be: ")
intcolor = input("Enter the fill color you'd like the polygon to be: ")

screen = Screen()

turtle = Turtle()
turtle.color(extcolor, intcolor)

turtle.begin_fill()

for _ in range(sides):
    turtle.forward(length)
    turtle.left(360 / sides)

turtle.end_fill()

turtle.hideturtle()

screen.exitonclick()

Upvotes: 1

Atharva Kadlag
Atharva Kadlag

Reputation: 302

import turtle 
wn = turtle.Screen()
kyle = turtle.Turtle()

kyle.begin_fill()
sides = int(input("Enter the number of sides of the polygon "))
len = int(input("Enter the length of the sides of the polygon "))
extcolor = input("Enter the color you would like the polygon to be ")
intcolor = input("Enter the fill color you would like the polygon to be ")

kyle.color(extcolor)
kyle.fillcolor(intcolor)

for i in range(int(sides)):
    kyle.forward (int(len))
    kyle.left (int(360)/int(sides))

kyle.end_fill()

its fillcolor and not fill. and you also stopped filling before actually tracing the polygon

Upvotes: 0

Related Questions