Hi,

Sorry if this is asked before, but I'm having trouble figuring this out (tried already for a couple of hours).

This is what is going on:
Kids make a test with 50 question. These test are checked manually on paper and afterwards need to be put into a website. What needs to be entered on the website is what question they got good and what questions they got wrong. This all needs to be done by hand, meaning you need to click on a radio-button to set it to "answered wrong". This clicking is very annoying because the alignment of the page is bad, some childeren make a lot of mistakes, there are many childeren, etc.

You can also control the webpage with tabs and set the radio-buttons with arrow, so what I want to do is that I enter into a Tktinker textbox:

2
9
12
18
19
20
45

which are the mistakes they made and let the pc do the keystrokes. I got already how to make keystrokes and the Tktinker, but I can't get the rest.

You see, the keystroke module will need this to work good:

{TAB}{TAB}
{RIGHT}
{TAB}{TAB}
{TAB}{TAB}
etc. to 50

Where two tabs means, go to next question and right means that it sets radiobutton.

So I made a script that does a find() on 1 to 50 in the string from the text box, but than it will also take (for example) the 2 from 12 and the 1.

I just can't get the '18' without also getting the '1' and '8', you know what I mean?

Please some help on how to do this. Thanks

Recommended Answers

All 12 Replies

not sure what you mean but have you tried using int() to convert a string?

I don't really understand either. If you could post an example of your code, and where it goes wrong I could help.
And maybe try index() instead of find?

for example:

l = range(1,51)
try:
  print l[l.index(18)]
except: print '18 not found'

You will need the try, except statements in case 18 isn't in the list. If try, except wasn't incorporated ans 18 wasn't in the list, you would get an Index error.

And to pull the data out of a string as the title of the post implies I use locale.atoi()
Example:

import locale
numberString = "18"
locale.atoi(numberString)

To get a float instead of an intiger use locale.atof(numberString)

Thank you for helping.

I tried a new code, but it also not works.

My code:

for i in range(1,26):
            t = 0
            for x in range(0,len(thetext)):
                if thetext[x] == '\n' and thetext[x-1] == str(i) and thetext[x-2] == '\n':
                    print 'one'
                    t = 1
                    break
                if (thetext[x-1:x-2]) == str(i):
                    print 'two'
                    t = 1
                    break
            if t == 0:
                print "{TAB}{TAB} " + str(i)

thetext is

"""
1
5
16
"""

My output is

one
{TAB}{TAB} 2
{TAB}{TAB} 3
{TAB}{TAB} 4
one
{TAB}{TAB} 6
{TAB}{TAB} 7
{TAB}{TAB} 8
{TAB}{TAB} 9
{TAB}{TAB} 10
{TAB}{TAB} 11
{TAB}{TAB} 12
{TAB}{TAB} 13
{TAB}{TAB} 14
{TAB}{TAB} 15
{TAB}{TAB} 16
{TAB}{TAB} 17
{TAB}{TAB} 18
{TAB}{TAB} 19
{TAB}{TAB} 20
{TAB}{TAB} 21
{TAB}{TAB} 22
{TAB}{TAB} 23
{TAB}{TAB} 24
{TAB}{TAB} 25

I also need 16 to have that "one". It sounds so easy, but I have no idea how to get it work...

You don't have a newline on the last line of the test data so "\n" is never found". You can add a newline to the data or test for EOF and if -1 is not a newline, then add one.

def test_func(thetext):
        for i in range(1,26):
            t = 0
            for x in range(0,len(thetext)):
                if thetext[x] == '\n' and thetext[x-1] == str(i) and thetext[x-2] == '\n':
                    print 'one'
                    t = 1
                    break
                if (thetext[x-1:x-2]) == str(i):
                    print 'two'
                    t = 1
                    break
            if t == 0:
                print "{TAB}{TAB} " + str(i)

test_list = [ "1\n",
              "5\n",
              "16\n" ]

for ltr in test_list:
   test_func(ltr) 
#
"""     My Results
one
{TAB}{TAB} 2
{TAB}{TAB} 3
{TAB}{TAB} 4
{TAB}{TAB} 5
{TAB}{TAB} 6
{TAB}{TAB} 7
{TAB}{TAB} 8
{TAB}{TAB} 9
{TAB}{TAB} 10
{TAB}{TAB} 11
{TAB}{TAB} 12
{TAB}{TAB} 13
{TAB}{TAB} 14
{TAB}{TAB} 15
{TAB}{TAB} 16
{TAB}{TAB} 17
{TAB}{TAB} 18
{TAB}{TAB} 19
{TAB}{TAB} 20
{TAB}{TAB} 21
{TAB}{TAB} 22
{TAB}{TAB} 23
{TAB}{TAB} 24
{TAB}{TAB} 25
{TAB}{TAB} 1
{TAB}{TAB} 2
{TAB}{TAB} 3
{TAB}{TAB} 4
one
{TAB}{TAB} 6
{TAB}{TAB} 7
{TAB}{TAB} 8
{TAB}{TAB} 9
{TAB}{TAB} 10
{TAB}{TAB} 11
{TAB}{TAB} 12
{TAB}{TAB} 13
{TAB}{TAB} 14
{TAB}{TAB} 15
{TAB}{TAB} 16
{TAB}{TAB} 17
{TAB}{TAB} 18
{TAB}{TAB} 19
{TAB}{TAB} 20
{TAB}{TAB} 21
{TAB}{TAB} 22
{TAB}{TAB} 23
{TAB}{TAB} 24
{TAB}{TAB} 25
{TAB}{TAB} 1
{TAB}{TAB} 2
{TAB}{TAB} 3
{TAB}{TAB} 4
{TAB}{TAB} 5
{TAB}{TAB} 6
{TAB}{TAB} 7
{TAB}{TAB} 8
{TAB}{TAB} 9
{TAB}{TAB} 10
{TAB}{TAB} 11
{TAB}{TAB} 12
{TAB}{TAB} 13
{TAB}{TAB} 14
{TAB}{TAB} 15
two
{TAB}{TAB} 17
{TAB}{TAB} 18
{TAB}{TAB} 19
{TAB}{TAB} 20
{TAB}{TAB} 21
{TAB}{TAB} 22
{TAB}{TAB} 23
{TAB}{TAB} 24
{TAB}{TAB} 25
"""

Thanks, but as I said, my code does not work (with "thetext" coming from Tkinter).
No matter how much enters I put behind it.

My code is just bad, is there no other easy way doing this?

I tried again today but can't figure it out. Vegaseat always has nice simple examples, isn't there one for this problem?

In case people don't want to read the rest of the topic, this is the problem:

I have this string (comes from text input Tkinter), exactly like this, so there are enters behind the numbers:

2
6
18
22

and want to create a string which counts from 1 to 26 and when a number in the string is the same as the counting it puts in a word (or sumthing else). like so:

1
gotcha
3
4
5
gotcha
7
8
9
10
11
12
13
14
15
16
17
gotcha
19
20
21
gotcha
23
24
25
26

help? :(

Okay I think I got it.

s = """
2
6
18
22
"""
s = s.split()
i = 1
while i<=26:
  try:
    if s.index(str(i)):
      print "gotcha"
    i+=1
  except:
    print str(i)
    i+=1
    continue

My results:

1
3
4
5
gotcha
7
8
9
10
11
12
13
14
15
16
17
gotcha
19
20
21
gotcha
23
24
25
26

I just noticed 2 wasn't shown. Maybe I don't have it....

see below for working code

...
I just noticed 2 wasn't shown. Maybe I don't have it....

A little simpler, and you might just have it ...

s = """
2
6
18
22
"""
s = s.split()
i = 1
while i <= 26:
  if str(i) in s:
    print "gotcha"
  else:
    print str(i)
  i += 1

I was almost right lol.
I forgot about the for,in....

And why wasn't the 2 shown or caught in my code?

Thank you very much for the help Tech B and vegaseat. :)

Glad to be of assistance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.