Python Forum

Full Version: using input() to control python turtles
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I wrote the code below to try to get the turtle below to move the amount input, but I cannot seem to figure out why defining c will not work. How can I get the input to work? I keep getting an error message "ExternalError: TypeError: Cannot read property 'instance' of undefined on line 13." Line 13 is turtle.forward(int©).
import turtle
myTurtle = turtle.Turtle()
myTurtle.forward(100)
myTurtle.right(90)
myTurtle.penup()
myTurtle.forward(100)
myTurtle.right(90)
myTurtle.pendown()
myTurtle.pencolor("green")
myTurtle.right(90)
myTurtle.forward(100)
c=input(int())
turtle.forward(int(c))
myTurtle.back(100)
try

c = input()
input takes a string argument which is the prompt.
turtle has a built in popup to add text and integers found here:
turtle docs
an example is:
from turtle import *

ta= getscreen()
forward(100)
right(90)
penup()
forward(100)
right(90)
pendown()
pencolor("green")
right(90)
forward(100)
fwd= numinput('forward Movement','Enter integer',
               default=1,minval=1,maxval=100)

forward(fwd)
right(100)
(Jan-09-2020, 09:30 PM)joe_momma Wrote: [ -> ]turtle has a built in popup to add text and integers found here: turtle docs an example is:
 from turtle import * ta= getscreen() forward(100) right(90) penup() forward(100) right(90) pendown() pencolor("green") right(90) forward(100) fwd= numinput('forward Movement','Enter integer', default=1,minval=1,maxval=100) forward(fwd) right(100) 

Hmmm...this still isn't working. I think it is because I am using Pycharm. The input() function in general is not working. However, I will now look for similar help sites specific to turtles
I was able to run it from thonny, MU, idle, and from command line sorry not using pycharm you could try a different import:
from turtle import getscreen,forward,right,penup,numinput,fd,pencolor,pendown
or try:
from turtle import Turtle, TurtleScreen
myTurtle = Turtle()

myTurtle.forward(100)
myTurtle.right(90)
myTurtle.penup()
myTurtle.forward(100)
myTurtle.right(90)
myTurtle.pendown()
myTurtle.pencolor("green")
myTurtle.right(90)
myTurtle.forward(100)
fwd= TurtleScreen.numinput(myTurtle,'forward','Enter integer',
                                  default=1,minval=1,maxval=100)

myTurtle.forward(fwd)
myTurtle.back(100)