Python Basics

How to draw tally marks in Python Turtle?

Tally marks are generally used for counting numbers. You can also use it in counting scores of games. While counting numbers, you draw a vertical line for each number and every fifth number is drawn diagonally that passes through the previous four vertical lines. For example, tally marks for the number 9 is

Tally Marks for number 9

Logic for drawing tally marks

We will draw tally marks in the form of square. And taking the side of the square as 30 units. Here, the distance between each vertical line is 10 units. So, four vertical lines will form a square of 30 units side. Every fifth number is drawn diagonally and its length equal to 30x√2

#Python program to draw tally marks in turtle programming
import turtle
import math

tallymarks = turtle.Turtle()
number = int(input("Enter a number: ")) #Asking user to enter a number
tallymarks.right(90)
x = 0
for i in range(1,number+1):
  if(i%5 == 0): #For every fifth number, it will draw diagonal line
    tallymarks.right(135)
    tallymarks.forward(30*math.sqrt(2))
    tallymarks.right(225)
  else: #For other numbers, it will draw vertical line
    tallymarks.penup()
    tallymarks.goto(x*10,0)
    tallymarks.pendown()
    tallymarks.forward(30)
  x = x + 1

Output of the above program

Assuming that user has entered 34. So, we will get tally marks accordingly.

Tally Marks in Python Turtle

Explanation of the above code-

if(i%5 == 0):
  tallymarks.right(135)
  tallymarks.forward(30*math.sqrt(2))
  tallymarks.right(225)

For every fifth number, we will move the turtle 30x√2 units in such a way that it will be drawn as a diagonal.

else:
  tallymarks.penup()
  tallymarks.goto(x*10,0)
  tallymarks.pendown()
  tallymarks.forward(30)
x = x + 1

For other numbers, we will draw a vertical line by moving the turtle to the specific position using goto() function.

Recommended Posts