Problems importing Turtle (Python 3.5) - python

I'm trying to use Turtle, but for some reason, it won't let me import it. I've tried from turtle import * (and that works) but if I try print(dir(turtle)) or to use any functions, I get an error saying turtle is not defined.
from turtle import Turtle doesn't work but print(dir(Turtle)) after using from turtle import * does work. However, prefixing commands with Turtle. ie Turtle.color("red") doesn't work.
Also, the demonstrations in the turtledemo folder work. I'd really appreciate any help

This imports turtle into the main namespace and is used as follows:
import turtle
turtle.something()
Thus, you now have an identifier in your main namespace, named turtle.
This imports all visible identifiers from turtle into the main namespace
and is used differently:
from turtle import *
something()
In this scenario, turtle is not in the main namespace. Its contents
are. Thus, dir(turtle) will fail, because that identifier isn't there.
Are you sure that from turtle import Turtle doesn't work? It worked
for me.

If you import all from turtle then you have to use the Turtle object directly, like this:
from turtle import *
print(dir(Turtle))
and not like this:
from turtle import *
print(dir(turtle))

Related

import syntax in Python

Is there any difference between coding:
import turtle
and coding
from turtle import *
(Using module turtle as an example, I suppose the answer will be true for all modules.)
My understanding was that the * imports all methods in the module, so why use the longer syntax?

AttributeError: 'Turtle' object has no attribute 'shapesize' on line 14

I'm trying to make a turtle game on Repl.it and I don't know why this error keeps coming up. Here is my code:
import turtle
wn = turtle.Screen()
wn.bgcolor("white")
wn.setup(width=800, height=800)
wn.tracer(0)
# Set up the ground
ground = turtle.Turtle()
ground.color("white")
ground.shape("square")
ground.speed(0)
ground.shapesize(stretch_wid=200, stretch_len=20)
ground.speed(0)
ground.color("black")
ground.penup()
ground.goto(0, -400)
ground.direction = "stop"
It seems that the implementation of the turtle library on repl.it is somewhat limited and not all commands are available. You can either run it locally to use all commands or remove incompatible commands.
Source: https://repl.it/talk/ask/Turtle-python-resizing/7312
There is actually a way to do this.
Whenever you create a new repl instead of creating a "python with turtle" repl just create a normal python one. Then import the turtle module and it should work. When importing it on a basic python file it imports everything.
Whenever you do import turtle it is also a good idea to add from turtle import * that way you import everything.
I also had this problem
You can use Python with pygame instead of python with turtle because for some reason, it works

Using the wrong module

I'm programming a game with pygame, but when I try to use the python's math module, my program tries to use pygame.math instead. How can I solve this issue?
Example:
import pygame, math
from pygame import *
pygame.init()
a = math.sin(90)
# some code that uses a
Error message:
AttributeError: module 'pygame.math' has no attribute 'sin'
The error might be because of from pygame import *, but my code doesn't work without it.
Solved in the commments:
Use the code as following, as it will prevent errors:
import pygame
import math
pygame.init()
a = math.sin(90)
This way it imports as separate modules. You had it saying:
Import Pygame and Pygame Math
Initiate Pygame
define variable "a"
I use pygame and math in my code but I have them as separate imports:
import pygame
import math
And everything works as expected without error

importing pictures to python

I have two pictures "gif" and want to insert them as small pictures and add them to Python code. I want the picture to stay in an accurate location as well decrease it size. When i try to run this code the shell shows an error "screen isn't defined"
import turtle
import time
from tkinter import *
screen=turtle.Turtle()
screen=turtle.getscreen()
screen.register_shape("health.gif")
screen.penup()
screen.shape("health.gif")
screen.goto(x+50,y+150)
I don't know why you're getting that error... but you have a mixup with your variable names... you create a variable you call screen that is turtle.Turtle() but then you overwrite that variable by doing turtle.getscreen()
Doing:
import turtle
t=turtle.Turtle()
screen=t.getscreen()
screen.register_shape("health.gif")
t.penup()
t.shape("health.gif")
Draws a health.gif image (if that happens to be in your working directory)

Python: Making Background Image with Turtles

I am trying to insert a .gif file as a background image in my turtle world for a separate turtle to travel on, but I can't get it to work. I'm new to python and any help would be great.
Here's the current code:
from turtle import *
from Tkinter import *
def test():
turtle.bgpic("warehouse1.gif")
fd(100)
goto(50, 100)
from turtle import * makes available all names in the turtle module so you could use bare bgpic() in this case:
#!/usr/bin/env python
from turtle import *
def test():
speed(1) # set the slowest speed to see the turtle movements
bgpic('warehouse1.gif')
fd(100)
goto(50, 100)
mainloop()
test()
Note: In general you should not use wildcard imports (*) outside a Python interactive shell. See Idioms and Anti-Idioms in Python.

Categories

Resources