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()
Zelow
  • 33
  • 4
  • 1
    Depends, what exactly is the function turtleTurn(), what does it do and why is it called twice? – Sebastian Hätälä Jul 03 '20 at 22:58
  • Does this answer your question? [How to print instances of a class using print()?](https://stackoverflow.com/questions/1535327/how-to-print-instances-of-a-class-using-print) –  Jul 03 '20 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. – Zelow Jul 03 '20 at 23:11

4 Answers4

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)
Dennis Sparrow
  • 938
  • 6
  • 13
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".

Loai
  • 40
  • 8
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']}
wjandrea
  • 28,235
  • 9
  • 60
  • 81
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.

Panwen Wang
  • 3,573
  • 1
  • 18
  • 39