none
WHILE loop problem - how to offer user input whilst in a loop? RRS feed

  • Question

  • Hi,

    Very new to Small Basic but I coded in basic many moons ago. I am helping my daughter with a school project.

    The code below is part of the code we are working on...this works fine but does not give the user the option to say 'ok I entered the wrong voucher code but I want another chance...'

    Without using GOTO I am finding it a little tricky. (GOTO not allowed in the code - school rule).

    First I thought I could nest in an IF THEN ELSE but didn't work then tried to use sub routine but cannot get it to work. I think I need to SUB out the chance to enter the voucher code again just after the voucher code is entered. Any help would be appreciated. 

    This is what we need to get working...

    DO YOU HAVE A CODE? - YES or NO - IF NO then final_price = final_price, IF YES then enter it...IF the code entered is correct then final_price = final_price - voucher_value, IF the code is wrong then DO YOU WANT TO ENTER IT AGAIN, YES or NO, this is then looped until the correct code is entered or the user says NO. 

    As I said i can get this working using GOTO but that's not allowed.

    This is the code that does not give the user the option

    ------------------------------------------------------------------------------------------------------------------------

    code = 100

    'ASK FOR CODE

    Voucher_answer = MessageBox.AskForInput("Do you have a voucher code? Y/N")

    'VALIDATE - ONLY 'Y' or 'N' ACCEPTED

    While Voucher_answer <> "Y" And Voucher_answer <> "N"
      Voucher_answer = MessageBox.AskForInput("Enter 'Y' or 'N' only..")
    EndWhile

    If Voucher_answer = "Y" Then 
      voucher_code = MessageBox.AskForInput(" what is the code ")
      While voucher_code <> code  
        voucher_code = MessageBox.AskForInput(" what is the code ")
      EndWhile
        MessageBox.ShowMessage("code correct " + code)
    Else
      MessageBox.ShowMessage("okay no code")
    EndIf  


    Monday, March 2, 2015 9:57 PM

Answers

  • Ezra...I took a fresh look...read over the advice here and got it to work...to say I am chuffed!!!

    codeloop = "True"
    While codeloop 
      
    Voucher_answer = MessageBox.AskForInput("Do you have a voucher code? Y/N")
    Voucher_answer = Text.ConvertToUpperCase(Voucher_answer)

    If (Voucher_answer = "N" or Voucher_answer = "NO") Then 
      Total_Price=Total_Price
       MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost for " + Amount_Of_Books + " books is £" + Total_Price )
       codeloop = "False"
    Else 
    'Yes runs to here 
      Voucher_code = MessageBox.AskForInput("Please enter valid voucher code")
      If Voucher_code = 4060 Then
        Total_Price=Total_Price-voucher_value
        MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost for " + Amount_Of_Books + " books is £" + Total_Price )
        codeloop = "False"
      Else
        MessageBox.ShowMessage("Code wrong, try again or enter 'N'")
      EndIf
    EndIf

    EndWhile

    MessageBox.ShowMessage("If you see this then the code has run correctly..")


    HEEELPPPPPPP

    • Marked as answer by DrewScot37 Thursday, March 5, 2015 9:07 PM
    • Unmarked as answer by DrewScot37 Thursday, March 5, 2015 9:07 PM
    • Marked as answer by DrewScot37 Thursday, March 5, 2015 9:07 PM
    Thursday, March 5, 2015 9:06 PM

All replies

  • You should setup a condition for the loop to break away:

    keepAsking = "True"
    
    While keepAsking
      ' Ask code block below
      ' ...
      
      if correctAnswer Then
        keepAsking = "False"
      EndIf
    EndWhile


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Tuesday, March 3, 2015 12:42 AM
    Answerer
  • I recommend these resources . . . .

    A list of features for small basic:

    http://smallbasic.com/doc.aspx

    Wiki Portal:

    http://social.technet.microsoft.com/wiki/contents/articles/17553.wiki-small-basic-portal.asp

    Some good articles to begin with . . . .

    General programming practices:

    It's best to avoid using GOTO statements in general since too many can often clutter the logic of a program and make it difficult to debug. Further reasons why is stated in the article below.

    http://social.technet.microsoft.com/wiki/contents/articles/15081.small-basic-programming-tips.aspx

    Advice on how to become a good programmer:

    http://social.technet.microsoft.com/wiki/contents/articles/18192.development-map-for-becoming-a-good-programmer-using-small-basic-and-msdn.aspx

    By the way, the code provided by GoToLoop is a good example. All the code inside a while loop will be repeated and read any commands inputted by the user.
    Tuesday, March 3, 2015 1:22 AM
  • Here's a start. It similar to GoToLoop's example but with more detail.

    The line " TextWindow.Read() " can only be processed if the user presses ENTER.

    code = 100 While "True" 'Main Loop TextWindow.WriteLine("Do you have a voucher code? Press y/n then 'Enter'") answer = TextWindow.Read() TextWindow.Clear() If (answer = "y" Or answer = "Y") Then inputCode = "True" While inputCode = "True" TextWindow.WriteLine("Please, enter your code then press 'Enter'") input = TextWindow.ReadNumber() If (input = code) Then TextWindow.WriteLine("Discount processed . . . .") inputCode = "False" Else TextWindow.WriteLine("Invalid code. Press 'n' then enter to quite or press any button to try again.") response = TextWindow.Read() If (response = "n" Or response = "N") Then inputCode = "False" TextWindow.WriteLine("Okay, your total is . . . .") EndIf EndIf EndWhile ElseIf (answer = "n" Or answer = "N") Then TextWindow.WriteLine("Okay, your total is . . . .") Else TextWindow.WriteLine("Please enter 'y' or 'n' only . . . .") EndIf TextWindow.Pause() TextWindow.Clear() EndWhile

    Tuesday, March 3, 2015 1:52 AM
  • Many thanks to all...such a simple answer. Thanks to Ezra94 for filling in most the blanks..some of the code I did not use but it certainly really helped.

    This is the final code that works a treat...

    ----------------------------------------------------

    code = 4060
    Price_Of_Book = 15.99
    voucher_value = 25.00

    Amount_Of_Books = MessageBox.AskForInput("How many Software Development for Beginners books would you like to order?")

    While Amount_Of_Books <1 Or Amount_Of_Books >80
      Amount_Of_Books = MessageBox.AskForInput ("Try again! Please insert a number between 1 and 80")
    EndWhile

    MessageBox.ShowMessage("You have ordered "  + Amount_Of_Books +  " copies of Software Development for Beginners")

    Total_Price = Price_Of_Book * Amount_Of_Books

    If Amount_Of_Books >=51 And Amount_Of_Books <=80 Then
      MessageBox.ShowMessage("You have recieved a 10% discount as you have ordered between 51 and 80 books")
    ElseIf Amount_Of_Books >=11 And Amount_Of_Books <=50 Then
      MessageBox.ShowMessage("You have recieved a 7.5% discount as you have ordered between 11 and 50 books")
    ElseIf Amount_Of_Books >=5 And Amount_Of_Books <=10 Then
      MessageBox.ShowMessage("You have recieved a 5% discount as you have ordered between 5 and 10 books")  
    EndIf

    If Amount_Of_Books >=51 And Amount_Of_Books <=80 Then 
      Total_Price = Total_Price * 0.9
    ElseIf Amount_Of_Books >=11 And Amount_Of_Books <=50 Then 
      Total_Price = Total_Price * 0.925
    ElseIf Amount_Of_Books >=5 And Amount_Of_Books <=10 Then 
      Total_Price = Total_Price * 0.95
    EndIf

    Total_Price = 0.01 * Math.Round(100*Total_Price)

    answer = MessageBox.AskForInput("Do you have a voucher code? Press Y or N")
    If (answer = "y" Or answer = "Y") Then 
      inputCode = "True"
      
      While inputCode = "True"
        input = MessageBox.AskForInput("Please, enter your code...")
        
        If (input = code) Then 
          MessageBox.ShowMessage("Discount processed...")
          Total_Price=Total_Price-voucher_value
          MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )
          inputCode = "False"
        Else
          response = MessageBox.AskForInput("Invalid code. Press 'N' to quit or any button to try again.") 
          
          If (response = "n" Or response = "N") Then 
            inputCode = "False"
            MessageBox.ShowMessage("Okay, no discount voucher applied_1")
            Total_Price=Total_Price
            MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )
          EndIf
        EndIf
      EndWhile 
      
    ElseIf (answer = "n" Or answer = "N") Then  
      MessageBox.ShowMessage("Okay, no discount voucher applied_2")
      Total_Price=Total_Price
      MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )
    Else 
      EndIf

    Tuesday, March 3, 2015 5:45 PM
  • Apologies but i was too quick to say this was done...

    This is my current code, I missed of the math.floor to check for whole numbers for one thing and then i am caught in a loop at the end of the code...once the price and number of books is displayed then that should be the end but it loops back up again...this is the current code...

    code = 4060
    Price_Of_Book = 15.99
    voucher_value = 25.00

    Amount_Of_Books = MessageBox.AskForInput("How many Software Development for Beginners books would you like to order?")
    While (Math.Floor(Amount_Of_Books) <> Amount_Of_Books) 
      Amount_Of_Books = MessageBox.AskForInput("Whole numbers only, try again")
    EndWhile

    While Amount_Of_Books <1 Or Amount_Of_Books >80
      Amount_Of_Books = MessageBox.AskForInput ("Try again! Please insert a number between 1 and 80")
    EndWhile

    MessageBox.ShowMessage("You have ordered "  + Amount_Of_Books +  " copies of Software Development for Beginners")

    Total_Price = Price_Of_Book * Amount_Of_Books

    If Amount_Of_Books >=51 And Amount_Of_Books <=80 Then
      MessageBox.ShowMessage("You have recieved a 10% discount as you have ordered between 51 and 80 books")
    ElseIf Amount_Of_Books >=11 And Amount_Of_Books <=50 Then
      MessageBox.ShowMessage("You have recieved a 7.5% discount as you have ordered between 11 and 50 books")
    ElseIf Amount_Of_Books >=5 And Amount_Of_Books <=10 Then
      MessageBox.ShowMessage("You have recieved a 5% discount as you have ordered between 5 and 10 books")  
    EndIf

    If Amount_Of_Books >=51 And Amount_Of_Books <=80 Then 
      Total_Price = Total_Price * 0.9
    ElseIf Amount_Of_Books >=11 And Amount_Of_Books <=50 Then 
      Total_Price = Total_Price * 0.925
    ElseIf Amount_Of_Books >=5 And Amount_Of_Books <=10 Then 
      Total_Price = Total_Price * 0.95
    EndIf

    Total_Price = 0.01 * Math.Round(100*Total_Price)

    While "True"
        answer = MessageBox.AskForInput("Do you have a voucher code? Press Y or N")
      If (answer = "y" Or answer = "Y") Then 
        inputCode = "True"
        
        While inputCode = "True"
          input = MessageBox.AskForInput("Please, enter your code...")
          
          If (input = code) Then 
            MessageBox.ShowMessage("Discount processed...")
            Total_Price=Total_Price-voucher_value
            MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )
            inputCode = "False"
          Else
            response = MessageBox.AskForInput("Invalid code. Press 'N' to quit or any button to try again.") 
            
            If (response = "n" Or response = "N") Then 
              inputCode = "False"
              MessageBox.ShowMessage("Okay, no discount voucher applied_1")
              Total_Price=Total_Price
              MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )
            EndIf
          EndIf
        EndWhile 
        
      ElseIf (answer = "n" Or answer = "N") Then  
        MessageBox.ShowMessage("Okay, no discount voucher applied_2")
        Total_Price=Total_Price
        MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )
      Else
        MessageBox.ShowMessage("Please enter Y or N only...") 
      EndIf
      
    EndWhile

    -----------------------------------------

    The last endwhile takes me back up to the question again 'do you have a code' - even when the code has been applied or not....I am working on this now...see if i can fix it.

    Tuesday, March 3, 2015 9:56 PM
  • Try this . . . .

    The piece of code we're discussing below is the last while loop . . . .

    Add these two lines of code

    TextWindow.Pause()
    Program.End()

    to beneath every line that is

    MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )

    So in the end it should like this

    MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )
    TextWindow.Pause()
    Program.End()

    I scanned your code, there should be three sets of the code block above.

     


    Wednesday, March 4, 2015 1:48 AM
  • By the way, since you want the program to end instead of returning the user to 'do you have a code', remove this line of code:

    inputCode = "False"

    Please, don't mistaken the line above with the line below (Keep the line below)

    inputCode = "True"

    The line above is essential for a certain while loop.

    Side Note:

    Are you familiar with sub routines? When your daughter gains enough experience I recommend

    placing the lines below:

    MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )
    TextWindow.Pause()
    Program.End()

    in a subroutine.

    Like so . . . .

    Sub receipt
    MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )
    TextWindow.Pause()
    Program.End()
    EndSub 

    Now, you can replace any code block (there are 3 sets) with those three lines with

    receipt()

    Wednesday, March 4, 2015 2:30 AM
  • One question . . . .

    Small basic doesn't seem to recognize MessageBox.ShowMessage() and MessageBox.AskForInput()

    I'm surprised that these lines ran in your version of small basic.

    I noticed that . . . .

    MessageBox.ShowMessage() operates similar TextWindow.WriteLine()

    MessageBox.AskForInput() operates similar TextWindow.Read() and TextWindow.ReadNumber()

    This is purely based on speculation though since I can't run the message box object in small basic. Are you using an older version of small basic?

    Wednesday, March 4, 2015 2:46 AM
  • Ezra,

    Many thanks for your attention.

    Messagebox.showmessage and .askforinput is an extension that the school uses...it is really cool!

    https://extendsmallbasic.codeplex.com/

    Secondly I know the program.end() command but wanted to use logic to get to the end of the code...for me that is the challenge lol...

    Lastly their are 2 instances of inputcode = "false" - removing the first one gives the code an out after the user enters the code correctly and even if the input is N to the code...but is still loops around when a wrong code is entered and then the loop to re-enter the code.

    Wednesday, March 4, 2015 8:46 AM
  • No, problem. I enjoy helping and it helps me strengthen my understanding of coding.

    To clarify, you want to get out of the loop after the user inputs the code (or not) instead of ending the program?

    If so . . . .

    You'll want to change the condition of this while loop (like I noted below in the code)

    While "True" 'CHANGE THIS LINE answer = MessageBox.AskForInput("Do you have a voucher code? Press Y or N") If (answer = "y" Or answer = "Y") Then inputCode = "True

    Once you find this line, change "True" to

    ask = "True"

    so you have

    While ask = "True" 'NEW LINE OF CODE
        answer = MessageBox.AskForInput("Do you have a voucher code? Press Y or N")
      If (answer = "y" Or answer = "Y") Then 
        inputCode = "True

    Finally, we need to tell the program to end this loop by using ask = "False"

    so, as previously mentioned, place this line

    ask = "False"

    underneath each line that has this message

    MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )

    As a result . . . .

    MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )

    ask = "False"

    TextWindow.Pause()'OPTIONAL


    Note: I marked TextWindow.Pause() as optional since I don't see any TextWindow.Clear() in the code.

    Now, you should be able to get at the end of your program without ending it or returning to the main question. I hope I answered your question. If not, let me know.

    Another note: I like to give most of the credit to GoToLoop, who promptly and concisely, answered your question at the very beginning of the post. I simply reiterated his solution in more specific terms. I encourage you to take another look at the structure of his sample. It's similar to the solution I provided. Input data, then when correct data is inputted, the loop will end and take the user at the end of the program.



    • Edited by Ezra94 Wednesday, March 4, 2015 5:46 PM Edit
    Wednesday, March 4, 2015 5:43 PM
  • Sorry Ezra I think I have broken the code...:(

    I tried your fix and it does not even run to the line to "Do you have a voucher code..."

    I made the changes you asked...this is the revised code that I tried to correct..


    code = 4060
    Price_Of_Book = 15.99
    voucher_value = 25.00

    Amount_Of_Books = MessageBox.AskForInput("How many Software Development for Beginners books would you like to order?")
    While (Math.Floor(Amount_Of_Books) <> Amount_Of_Books) 
      Amount_Of_Books = MessageBox.AskForInput("Whole numbers only, try again")
    EndWhile

    While Amount_Of_Books <1 Or Amount_Of_Books >80
      Amount_Of_Books = MessageBox.AskForInput ("Try again! Please insert a number between 1 and 80")
    EndWhile

    MessageBox.ShowMessage("You have ordered "  + Amount_Of_Books +  " copies of Software Development for Beginners")

    Total_Price = Price_Of_Book * Amount_Of_Books

    If Amount_Of_Books >=51 And Amount_Of_Books <=80 Then
      MessageBox.ShowMessage("You have recieved a 10% discount as you have ordered between 51 and 80 books")
    ElseIf Amount_Of_Books >=11 And Amount_Of_Books <=50 Then
      MessageBox.ShowMessage("You have recieved a 7.5% discount as you have ordered between 11 and 50 books")
    ElseIf Amount_Of_Books >=5 And Amount_Of_Books <=10 Then
      MessageBox.ShowMessage("You have recieved a 5% discount as you have ordered between 5 and 10 books")  
    EndIf

    If Amount_Of_Books >=51 And Amount_Of_Books <=80 Then 
      Total_Price = Total_Price * 0.9
    ElseIf Amount_Of_Books >=11 And Amount_Of_Books <=50 Then 
      Total_Price = Total_Price * 0.925
    ElseIf Amount_Of_Books >=5 And Amount_Of_Books <=10 Then 
      Total_Price = Total_Price * 0.95
    EndIf

    Total_Price = 0.01 * Math.Round(100*Total_Price)


    While ask = "True"
      answer = MessageBox.AskForInput("Do you have a voucher code? Press Y or N")
      If (answer = "y" Or answer = "Y") Then 
        inputCode = "True"
        
        While inputCode = "True"
          input = MessageBox.AskForInput("Please, enter your code...")
          
          If (input = code) Then 
            MessageBox.ShowMessage("Discount processed...")
            Total_Price=Total_Price-voucher_value
            MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )
            ask = "False"  
          Else
            response = MessageBox.AskForInput("Invalid code. Press 'N' to quit or any button to try again.") 
            
            If (response = "n" Or response = "N") Then 
              inputCode = "False"
              MessageBox.ShowMessage("Okay, no discount voucher applied_1")
              Total_Price=Total_Price
              MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )
              ask = "False"  
            EndIf
          EndIf
        EndWhile 
        
      ElseIf (answer = "n" Or answer = "N") Then  
        MessageBox.ShowMessage("Okay, no discount voucher applied_2")
        Total_Price=Total_Price
        MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost is £" + Total_Price )
        ask = "False" 
      Else
        MessageBox.ShowMessage("Please enter Y or N only...") 
      EndIf
      
    EndWhile



    HEEELPPPPPPP

    Thursday, March 5, 2015 7:50 PM
  • Ezra...I took a fresh look...read over the advice here and got it to work...to say I am chuffed!!!

    codeloop = "True"
    While codeloop 
      
    Voucher_answer = MessageBox.AskForInput("Do you have a voucher code? Y/N")
    Voucher_answer = Text.ConvertToUpperCase(Voucher_answer)

    If (Voucher_answer = "N" or Voucher_answer = "NO") Then 
      Total_Price=Total_Price
       MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost for " + Amount_Of_Books + " books is £" + Total_Price )
       codeloop = "False"
    Else 
    'Yes runs to here 
      Voucher_code = MessageBox.AskForInput("Please enter valid voucher code")
      If Voucher_code = 4060 Then
        Total_Price=Total_Price-voucher_value
        MessageBox.ShowMessage("Thank you for purchasing 'Software and Development for Beginners'. Your total cost for " + Amount_Of_Books + " books is £" + Total_Price )
        codeloop = "False"
      Else
        MessageBox.ShowMessage("Code wrong, try again or enter 'N'")
      EndIf
    EndIf

    EndWhile

    MessageBox.ShowMessage("If you see this then the code has run correctly..")


    HEEELPPPPPPP

    • Marked as answer by DrewScot37 Thursday, March 5, 2015 9:07 PM
    • Unmarked as answer by DrewScot37 Thursday, March 5, 2015 9:07 PM
    • Marked as answer by DrewScot37 Thursday, March 5, 2015 9:07 PM
    Thursday, March 5, 2015 9:06 PM
  • Must come back and thank Ezra and GotoLoop for all the help...I am going to be needing continued assistance over the next few years as both my daughters are taking IT / computing science at High School.

    Also going to be doing the Small Basic curriculum myself and see where it takes me. 


    HEEELPPPPPPP

    Friday, March 6, 2015 6:59 PM
  • Solving a persistent bug is the best feeling in the world!

    Learning to program will be worth your time. Programming encourages computational thinking so you'll become a more efficient problem solver. It will give a deeper sense of appreciation of technology. Also, you'll be able to collaborate with and help your daughters while they learn programming.

    From what I learned since starting a year ago from zero experience is be patient, take breaks, there's always a logical explanation behind a bug, and, if a problem becomes too difficult, seek guidance (not the answer but rather ease your way with the hints or samples provided). Looking at other programmer's code (even if it's the same program) allows you to expand your coding skills since everyone writes code a little bit differently.

    If you're interested, I recommend reading these articles:

    Advice:

    http://norvig.com/21-days.html

    http://www.gamecareerguide.com/features/1329/unconventional_tips_for_improving_.php

    About programming:

    http://www.motherjones.com/media/2014/06/computer-science-programming-code-diversity-sexism-education

    http://www.cs4fn.org/computationalthinking/index.php

    http://www.cs.cmu.edu/~CompThink/

    Good luck and have fun programming!


    • Edited by Ezra94 Saturday, March 7, 2015 2:03 AM edit
    Saturday, March 7, 2015 1:37 AM