In: Computer Science
Use Turtle graphics to draw your first name or your nickname if over 6 letters.
Just initials of your first & last name is OK if you add another feature such as color.
My initials are J.S but I don't know how to color them.
Here is my code:
Python program to print nick name if a name is greater than 6 character or initials if the color if given """ import turtle as T fName, lName = input('Enter name: ').split() color = input('Enter Color: ') T.fillcolor(color) if color is '': if len(fName)+len(lName) > 6: T.write(fName, font=("Arial",32,"normal")) else: T.write(fName + " " + lName,font=("Arial",32,"normal")) else: T.write(fName[0] + " " + lName[0], font=("Arial",32,"normal"))
Answer :
I have edited your code and now it is working fine. We should understand the difference between fill color() and color() function of turtle class.
fill color() - It is used to color the entire body of the given shape.
color() - It is used basically to color the text.
So here we have to use color() instead of fill color(), also I had move this line to the else part as if the user doesn't enter the initial so in that case, we don't have to color the text.
Python code: Given below is fully commented code.
import turtle as T #import library
fName, lName = input('Enter name: ').split() #taking first name and
last name input from user
color = input('Enter Color: ') #taking color input from user
if color is '': #if no color is selected then
if len(fName)+len(lName) > 6: #if lenght of full name exceed 6
then print only first name
T.write(fName, font=("Arial",32,"normal"))
else: #print full name if lenght is less than 6
T.write(fName + " " + lName,font=("Arial",32,"normal"))
else: #if color is entered then
T.color(color) #apply color to text
T.write(fName[0] + " " + lName[0], font=("Arial",32,"normal"))
#print the inital of first and last name
Given below is the screenshot of code and its output for better understanding of indentation of python code and inputs:
Code:
Output:
HOPE YOU GOT IT !!