def next_term(num):
if num<=0:
print("Zero or negative numbers are not even, nor Odd.","Enter number >",num)
else:
print(int(num))
while num!=1: #number is even
if num%2==0:
num=num/2
else:
num=(num*3)+1 #number is odd
print(int(num))
def main():
nani=int(input("Please enter number: "))
next_term(nani)
if __name__=="__main__":
main()

- 2,217
- 7
- 19
- 33
2 Answers
All you need is a few adjustments, all the adjustments are commented:
import turtle # Import turtle
size = 20 # Set the size of each number
def next_term(num):
if num<=0:
print("Zero or negative numbers are not even, nor Odd.","Enter number >",num)
else:
print(int(num))
while num!=1:
if num%2==0:
num=num/2
else:
num=(num*3)+1
turtle.write(int(num),font=("Arial",size,"normal")) # Instead of print(), use turtle.write()
turtle.forward(len(str(int(num)))*size) # After writing the number, go forward by the amount of digits in the number multiplied by a unit
def main():
nani=int(input("Please enter number: "))
next_term(nani)
if __name__=="__main__":
main()
Output:
>>>
Please enter number: 10
10
>>>
Here is a cleaner approach:
import turtle
size = 20
def collatz(num):
while num != 1:
if num % 2:
num = num*3+1
else:
num = num // 2
turtle.write(num,font=("Arial",size,"normal"))
turtle.forward(len(str(num))*size)
def main():
nani = int(input("Please enter number: "))
collatz(nani)
if __name__=="__main__":
main()

- 26,798
- 7
- 36
- 58
You need to be more specific in your question as there are lots of ways to plot Collatz numbers. For example, the code below makes a line plot of the various numbers an input value goes through on its way to 1
:
from turtle import Screen, Turtle
def collatz(number):
yield number
while number != 1:
if number % 2 == 0: # number is even
number //= 2
else:
number = number * 3 + 1 # number is odd
yield number
def graph_terms(generator):
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
terms = [n for n in generator] # exhaust generator as we need len() and max()
x_max, y_max = len(terms), max(terms)
x_step = screen.window_width() / 2 / x_max
y_step = screen.window_height() / 2 / y_max
turtle.setheading(90)
turtle.forward(y_max * y_step)
turtle.backward(y_max * y_step)
turtle.setheading(0)
for _ in range(2):
turtle.pendown()
turtle.forward(x_max * x_step)
turtle.backward(x_max * x_step)
turtle.penup()
turtle.color('red')
turtle.sety(y_step)
turtle.home()
turtle.color('black')
for step, term in enumerate(terms):
turtle.goto(step * x_step, term * y_step)
turtle.pendown()
turtle.dot()
screen.exitonclick()
def main():
number = int(input("Please enter number: "))
try:
generator = collatz(number)
graph_terms(generator)
except StopIteration:
pass
if __name__ == "__main__":
main()
Here's the graph for the number 25
:
In my code, I rework your Collatz number function to be a generator which seems like what it wants to be. However, for graphing purposes, I exhaust the generator immediately as I need the length and the maximum of the sequence so the generator doesn't pay off in this situation.
If this is the style of line graph you're looking for, then finishing off this minimalist code remains as an exercise for you: it needs to be centered on the screen; it needs numbers printed on its axes; etc.

- 40,441
- 5
- 32
- 81