Ask coding questions

← Back to all posts
I can't figure out how to do this!
Brendan23 (166)

I can't figure out how to save where the person clicked on to a variable. I know how to print it though. take a look at the repl and scroll to the bottom. that's where the problem is.

Comments
hotnewtop
Coder100 (18207)

You can use a database.

from replit import db

my_var = None

if 'my_var' in db: my_var = db['my_var']


# do magic


# save the variable!
db['my_var'] = my_var
CodeMaster007 (108)

Try this...

with open('do_re_mi.txt', 'w') as f:
f.write('Doe, a deer, a female deer\n')
f.write('Ray, a drop of golden sun\n')

The write() function takes a string and puts that content into the file stream. Although we don't store it, the write() function returns the number of characters it just entered i.e. the length of the input string.

Brendan23 (166)

@CodeMaster007 you saying that to me? I think your on the wrong post.

CodeMaster007 (108)

Yes. I am just using the sound of music as a example. Instead of do_re_mi.txt, you can use coolprogram.txt and so on. Do you understand this? I can explain it if you are lost. @Brendan23

Brendan23 (166)

@CodeMaster007 @Coder100 I want to know how to get the coordinates of the clicked spot, not how to save the coordinates of the clicked spot.

Coder100 (18207)

here's an example working code:

mouse_x = 0
mouse_y = 0

turtle.onscreenclick(lambda x, y: mouse_x = x ; mouse_y = y)

@Brendan23

Brendan23 (166)

@Coder100 1. Yeah, sry.
2. I did use that.
3. I'll try that.

Brendan23 (166)

@Coder100 yes, yes, I'm a noob, but how do I split the two functions, mouse_x = x and mouse_y = y? ; doesn't work.

Coder100 (18207)

@Brendan23
turtle.onscreenclick(lambda x, y: mouse_x, mouse_y = x, y)

Brendan23 (166)

@Coder100

  File "main.py", line 102
    t.onscreenclick(lambda x, y: mouse_x, mouse_y = x, y)
                                                       ^
SyntaxError: positional argument follows keyword argument

hmm...!