1

I'm new to python and coding in general. In the code below how do I get "turn" to print "june" or "july" instead of <turtle.Turtle object at 0x0405D730>

def main(): wn = turtle.Screen() #Creates a screen #Define your turtles here june = turtle.Turtle() july = turtle.Turtle() june.shape('turtle') july.shape('turtle') june.color('blue') july.color('red') july.goto(0, 50) #move second turtle to a different starting location turtleTurn() turn = turtleTurn() if turn == 0: turn = june else: turn = july while isInScreen(wn, turn) and sameSpot(june, july): if turn == june: turn = july else: turn = june turtleMove(turn) if isInScreen(wn, turn) == False: print("and the winning is ", turn) wn.exitonclick() main() 
3
  • 1
    Depends, what exactly is the function turtleTurn(), what does it do and why is it called twice? Commented Jul 3, 2020 at 22:58
  • Does this answer your question? How to print instances of a class using print()? Commented Jul 3, 2020 at 23:02
  • shatala, that is just me being stupid. Dmitry that might help but I'm so new I don't understand what I'm reading. Commented Jul 3, 2020 at 23:11

4 Answers 4

1

You can just store a name in each Turtle (after creating it) and refer to the name attribute to print it:

june.name = 'june' july.name = 'july' ... print("and the winning is ", turn.name) 
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this in many ways, and one of them is this :

my_var = "sss" my_var_name = [ k for k,v in locals().items() if v == my_var][0] print("Variable name is : ", my_var_name) 

Here we create a virable "my_var", when we declare a virable he is saved in local virables and u can access them by using "locals().items()" which return all of them. And by the for you iterate on them and when u find the v == to the virable u get it in "my_var_name".

Comments

0

You can't get the variable name itself, at least not easily. See the explanation here: Getting the name of a variable as a string -- kindall's answer

Instead you can compare the object identity and print its name manually:

print("and the winning is", 'july' if turn is july else 'june') 

Though for what it's worth, the most generic solution is to use a dict. See How do I create a variable number of variables? For example, starting with something like this:

turtles = {month: turtle.Turtle() for month in ['june', 'july']} 

Comments

0

You just need to wrap june and july with the value wrapper from python-varname:

from varname import Wrapper def main(): wn = turtle.Screen() #Creates a screen #Define your turtles here june = Wrapper(turtle.Turtle()) # <- then, use june.value to access turtle july = Wrapper(turtle.Turtle()) # <- and june/july.name to access the name june.value.shape('turtle') july.value.shape('turtle') june.value.color('blue') july.value.color('red') july.value.goto(0, 50) #move second turtle to a different starting location turtleTurn() turn = turtleTurn() if turn == 0: turn = june else: turn = july while isInScreen(wn, turn.value) and sameSpot(june.value, july.value): if turn.value == june.value: turn = july else: turn = june turtleMove(turn.value) if isInScreen(wn, turn.value) == False: print("and the winning is ", turn.name) wn.exitonclick() main() 

The package is hosted at https://github.com/pwwang/python-varname.

I am the author of the package. Let me know if you have any questions using it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.