locked
Newb Question: "Or" command, case sensitivity or aliases RRS feed

  • Question

  • So I am writing my first program.  I can't figure out how to make input not case sensitive, or at least put in different aliases.  This is what I have so far :

    TextWindow.WriteLine("Please enter your selection:")
    selection = TextWindow.Read()
    TextWindow.WriteLine("You selected " +selection)
    TextWindow.WriteLine("is this correct. Y or N")
    confirmation = TextWindow.Read()
    If confirmation = "N" Or "No" Or "NO" Or "no" Or "n" Then 
      GOTO START
      EndIf
      If confirmation = "Y" Or "y" Or "yes" Or "Yes" Or "YES" Then
    TextWindow.WriteLine("Very well, I will select a restraunt in " +selection)
    Endif

    It doesn't seem to be picking up the other versions of input I want to be a match.

    Any suggestions?

    Thanks,

    Jeff

    Friday, May 25, 2012 5:16 AM

Answers

  • Hello ElChefeJefe!

    I've got 2 suggestions for you:

    You can use Text.ConvertToUpperCase() or Text.ConvertToLowerCase() to force the entire letters within a string to become either capital or minuscule.

    The other one is to use TextWindow.ReadKey() rather than the common TextWindow.Read() if you just need a single key character as input. This way, you spare the user the obligation to push an additional <Enter> key all the time!

    I have a subroutine snippet which I use on some of my codes when I wish a 'Yes' confirmation from a user. It returns OK = "True" if the user hit <Y> or <Enter>, otherwise it returns OK = "False". Perhaps you may wish to modify it to better fit your own code.  :-D

    Sub AskConfirm
      
      TextWindow.WriteLine("Hit <Y> or <Enter> to confirm it,")
      TextWindow.WriteLine("or any other key to cancel it!")
      
      key = Text.ConvertToUpperCase( TextWindow.ReadKey() )
      
      If key = "Y" Or key = Text.GetCharacter(13) Then
        OK = "True"
      ElseIf key = Text.GetCharacter(27) Then
        Program.End()
      Else
        OK = "False"
      EndIf
      
    EndSub
    Friday, May 25, 2012 6:15 AM
    Answerer
  • Another way;

    start:

    TextWindow.WriteLine("Please enter your selection:")
    selection = TextWindow.Read()
    TextWindow.WriteLine("You selected " +selection)
    TextWindow.WriteLine("is this correct. Y or N")
    confirmation = TextWindow.Read()
    CN= Text.GetCharacterCode(text.ConvertToLowerCase(confirmation)) '<---------
    '  Text.GetCharacterCode  returns first letter's  code number     exam;  not ....n(110)   niet...n(110)  yes....y(121)  yeh!...y(121) 

    If CN= 110 Then   ' n =110    '<---------
      GOTO START
    elseIf CN=121 Then          ' y=121     '<---------
    TextWindow.WriteLine("Very well, I will select a restraunt in " +selection)
    Endif

    *First step; convert to lowercase 

    *second step; gets CharacterCode 



    Friday, May 25, 2012 6:31 AM
    Answerer
  • Taking a longer glance on your code, I've spotted you are a little astray on how to use the conditional operators correctly (both relational & logical ones). Almost all operators demand 2 operands.

    You can't simply compare a variable to many things at once. If you need to compare something to many values, you have to re-type it for each one!

    So the line -> If confirmation = "N" Or "No" Or "NO" Or "no" Or "n" Then
    should be  -> If confirmation = "N" Or confirmation = "No" Or confirmation = "NO" Or confirmation = "no" Or confirmation = "n" Then

    Of course, if you follow my advice on using confirmation = Text.ConvertToUpperCase( TextWindow.ReadKey() ), for the line above you'd just need to write -> If confirmation = "N" Then

    You can also take a look in this thread here (scroll all the way down), where I've already explained, with more details, the same thing for Alex Wetton.

    Laterz!!!

    Friday, May 25, 2012 6:47 AM
    Answerer

All replies

  • Hello ElChefeJefe!

    I've got 2 suggestions for you:

    You can use Text.ConvertToUpperCase() or Text.ConvertToLowerCase() to force the entire letters within a string to become either capital or minuscule.

    The other one is to use TextWindow.ReadKey() rather than the common TextWindow.Read() if you just need a single key character as input. This way, you spare the user the obligation to push an additional <Enter> key all the time!

    I have a subroutine snippet which I use on some of my codes when I wish a 'Yes' confirmation from a user. It returns OK = "True" if the user hit <Y> or <Enter>, otherwise it returns OK = "False". Perhaps you may wish to modify it to better fit your own code.  :-D

    Sub AskConfirm
      
      TextWindow.WriteLine("Hit <Y> or <Enter> to confirm it,")
      TextWindow.WriteLine("or any other key to cancel it!")
      
      key = Text.ConvertToUpperCase( TextWindow.ReadKey() )
      
      If key = "Y" Or key = Text.GetCharacter(13) Then
        OK = "True"
      ElseIf key = Text.GetCharacter(27) Then
        Program.End()
      Else
        OK = "False"
      EndIf
      
    EndSub
    Friday, May 25, 2012 6:15 AM
    Answerer
  • Another way;

    start:

    TextWindow.WriteLine("Please enter your selection:")
    selection = TextWindow.Read()
    TextWindow.WriteLine("You selected " +selection)
    TextWindow.WriteLine("is this correct. Y or N")
    confirmation = TextWindow.Read()
    CN= Text.GetCharacterCode(text.ConvertToLowerCase(confirmation)) '<---------
    '  Text.GetCharacterCode  returns first letter's  code number     exam;  not ....n(110)   niet...n(110)  yes....y(121)  yeh!...y(121) 

    If CN= 110 Then   ' n =110    '<---------
      GOTO START
    elseIf CN=121 Then          ' y=121     '<---------
    TextWindow.WriteLine("Very well, I will select a restraunt in " +selection)
    Endif

    *First step; convert to lowercase 

    *second step; gets CharacterCode 



    Friday, May 25, 2012 6:31 AM
    Answerer
  • Taking a longer glance on your code, I've spotted you are a little astray on how to use the conditional operators correctly (both relational & logical ones). Almost all operators demand 2 operands.

    You can't simply compare a variable to many things at once. If you need to compare something to many values, you have to re-type it for each one!

    So the line -> If confirmation = "N" Or "No" Or "NO" Or "no" Or "n" Then
    should be  -> If confirmation = "N" Or confirmation = "No" Or confirmation = "NO" Or confirmation = "no" Or confirmation = "n" Then

    Of course, if you follow my advice on using confirmation = Text.ConvertToUpperCase( TextWindow.ReadKey() ), for the line above you'd just need to write -> If confirmation = "N" Then

    You can also take a look in this thread here (scroll all the way down), where I've already explained, with more details, the same thing for Alex Wetton.

    Laterz!!!

    Friday, May 25, 2012 6:47 AM
    Answerer
  • Thanks everyone, this helps a lot.
    Friday, May 25, 2012 1:26 PM