Skip to first unread message

Guerra

unread,
Jun 16, 2017, 8:08:40 PM6/16/17
to MIT App Inventor Forum

Hello, world

Im trying to create a mechanism to check if an input string contains only the few allowed characters, something like


for i in string:
    if i != "a" or "b" or "c"
        print 
"invalid character"


you know?

I found out that you can split a string into itens in a list (https://groups.google.com/d/msg/mitappinventortest/GbK4eXiaHe0/1gBzQqigAAAJ)


So I tried this version in which I tried to adapt this code to take an input from a textbox and print an output in a second textbox:

All I got was the output invalid character no matter what the input was. Does anyone know why?

Thanks!

Pavitra

unread,
Jun 17, 2017, 5:38:06 AM6/17/17
to MIT App Inventor Forum

Guerra

unread,
Jun 17, 2017, 8:46:24 AM6/17/17
to MIT App Inventor Forum
This doesn't really answer my question, but it does give me an idea to go around the problem. I'll try another approach. Thanks!

Guerra

unread,
Jun 17, 2017, 9:57:21 AM6/17/17
to MIT App Inventor Forum
SOLVED
It's probably more junky and uses more memory (I had to create four global variables bc I'm using them in another part of my code) than if I had written directly in Javascript or something else. But, hey, it works!
Basically, to check if a given string contains ONLY the allowed characters you can add 1 to a counter for every valid character in the string. If the length of the string equals the counter, there are no invalid characters in that string.

Abraham Getzler

unread,
Jun 17, 2017, 1:52:37 PM6/17/17
to MIT App Inventor Forum
Also see http://appinventor.mit.edu/explore/ai2/support/blocks/text.html#contains

for each character c in the string
  if contains("ABCD",c) then
    correct = correct + 1
  end if
end for

ABG
Reply all
Reply to author
Forward
0 new messages