Reputation: 33
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()
Upvotes: 1
Views: 428
Reputation: 3825
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.
Upvotes: 0
Reputation: 32944
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']}
Upvotes: 0
Reputation: 40
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".
Upvotes: 1
Reputation: 978
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)
Upvotes: 1