User input in a turtle window can be obtained using the two graphic input methods that the turtle module provides:
It’s very important that we write a dynamic program and it can be achieved by writing a program that will accept input from a user. For instance, if we want to draw a circle with a varied radius, it’s better we get a radius of a circle from a user rather than statically mentioning it in the source code.
In this article, we will learn how to take text and number input in the turtle windows from a user.
Two input methods
As soon as the interpreter interprets a program, a dialog box window for the input of a string will pop up. It will ask the user to enter a string data type.
The parameter title is the title of the dialog window and the prompt is a set of text mostly describing what information to input.
from turtle import * name = textinput("Name", "what is your name:")
Pop up a dialog window for the input of a number.
numinput(title, prompt, default=None, minval=None, maxval=None)
The parameters you need to pass:
- title – title of the dialog window,
- prompt – text mostly describing what numerical information to input.
- default– default value.
- minval – minimum value for input.
- maxval – maximum value for the input.
The number input must be in the range of minval-maxval if these are given.
from turtle import * name = numinput("Age","How old are you:",18, minval=15, maxval=20)
1. Concentric Circles
Question: The concentric circle activity demonstrates how to accept number input from a user. Write a program that asks a user for 3 radius values and then draws three circles with those radii centered on the canvas.
from turtle import * pensize(5) shape("turtle") speed(5) def draw_circle(radius): ''' function sets turtle to the bottom position of a circle based on the radius and draws the circle. Finally goes to the center ''' right(90) forward(radius) left(90) pendown() circle(radius) penup() setposition(0,0) penup() first_radius = numinput("Radius","What is the first circle's radius?: ",20,minval=10,maxval=200) second_radius = numinput("Radius","What is the second circle's radius?: ",20,minval=10,maxval=200) third_radius = numinput("Radius","What is the third circle's radius?: ",20,minval=10,maxval=200) draw_circle(first_radius) draw_circle(second_radius) draw_circle(third_radius)
2. The Shapes
An activity on shapes will demonstrate how to accept a text from a user. Write a program that asks the user to enter the shape they want to see on the turtle window. Try with three shapes ie circle, square, and octagon. If the user enters a circle as input then the turtle should draw a circle, accordingly if the user enters the square, the turtle should draw a square and the same with the octagon.
from turtle import * shape("turtle") pensize(5) def square(): for i in range(4): forward(100) left(90) def cir(): circle(100) def octagon(): for i in range(8): forward(50) left(45) shape = textinput("shape", "What shape you want to see(square, circle, octagon):") if shape == "circle": cir() elif shape == "square": square() elif shape == "octagon": octagon()
The program has a function for each shape and it will be drawn with the help of for loop. While drawing a shape based on user input, you should know how to use conditional statements (if, elif, and else).