I hope you will have fun with what we're going to write.
But it doesn't matter if you don't... it will still illustrate various things that will make you a better computer programmer.
This is an early tutorial in a series which aims to help new programmers learn a language, "BASIC".
BASIC may be very old-fashioned... but it is also very user friendly. And the things you can learn while programming in BASIC are things you will still be using in modern "fancy" languages... and to write programs 20 years from now. (Unless all programming is done by AI by then!)
Oh. Umm. When I made that offhand remark a moment ago, it sparked a thought.
I asked search.brave.com "what would the code be for at multiplication test program in BBC BASIC" (complete with that typo). It responded...
10 REM Multiplication Test Program 20 MODE 1 30 PRINT "Welcome to the Multiplication Test!" 40 PRINT "You will be asked to solve 10 multiplication problems." 50 PRINT 60 FOR I% = 1 TO 10 70 RANDOMIZE TIME 80 A% = RND(10) : B% = RND(10) 90 PRINT "Question " ; I% ; ": What is " ; A% ; " * " ; B% ; "?" 100 INPUT "Your answer: " C% 110 IF C% = A% * B% THEN 120 PRINT "Correct!" 130 ELSE 140 PRINT "Incorrect. The correct answer is " ; A% * B% 150 ENDIF 160 PRINT 170 NEXT I% 180 PRINT "Test complete. Thank you for participating!" 190 END
... complete with...
Explanation... Line 10: A comment to describe the program. Line 20: Sets the screen mode to a text mode. Line 30-50: Prints a welcome message and instructions. Line 60: Starts a loop to ask 10 questions. Line 70: Seeds the random number generator with the current time to ensure different numbers each time the program runs. Line 80: Generates two random numbers between 0 and 9. Line 90: Prints the question. Line 100: Prompts the user for their answer. Line 110-150: Checks if the user's answer is correct and prints the appropriate message. Line 160: Prints a blank line for readability. Line 170: Ends the loop. Line 180: Prints a completion message. Line 190: Ends the program.
Apart from the fact that the BBC these tutorials are built around doesn't need line numbers, I really can find no fault in that. (It doesn't need line numbers, but will tolerate them.)
I had to REM line 70 out because the dialect of BASIC we are using for these tutorials initializes the random number generator a different way.
But other than that, it ran, and in most ways, I would call it a good program.
Shall we all just go home?
Oh good. You are still reading.
Never start writing code before you have a clear idea, in plain English, of what the user is going to see, and why.
OUR maths test is going to do the following...
It will ask 4 questions like "What is 5 times 3?". The numbers in the questions will not be less than 2, nor more than 9. (If we write the program well, it will be easy to change how many questions are asked.)
It will wait for us to type the answers.
It will tell is if we were right or wrong.
And after the number of questions it is set up for, it will stop.
Read that again, and try to imagine what the program will look like... but first let me mention...
If you say x=3, then you will put 3 in a variable called x.
If you say x= RND(3), then you will put one of the following into x: 1, 2, or 3.
x= RND(28) will give rise to 1-8, inclusive, so...
x=RND(8)+1 would give us the 2-9, inclusive, we want.
Now look at the in-English description of the program, and see if you can figure out the code to make that happen.
Always try to do it yourself first. Then when you see the right answer, you learn about mistakes you might have made, and also you are more likely to absorb the right answer.
Always build your program up in stages. SOMETHING should work at the end of each stage.
For our mathematics test, we'll start like this...
QsToAsk%=4 REPEAT PRINT "What is x times y?" INPUT UsersAnswer% QsToAsk%=QsToAsk%-1 UNTIL QsToAsk%<1 PRINT "Goodbye" PRINT
Enter that, get it to run. It WILL run, when you do a perfect copy!
It doesn't do "everything" yet, but I hope you can see that it has moved us closer to what we said we would write. You have to type a number and press the enter key each time it presents the question, even if the program does nothing with the number... for now!
Just after the REPEAT, add...
Num0%=RND(8)+1 Num1%=RND(8)+1
(Why is the first one "NumZERO"? That's a better way to "number" things. Trust me. Most programmers would agree- count from zero for computery things. Start with zero, not the usual 1. But if it makes you crazy, call them Num1% and Num2%).
Putting "%" at the end of a variable's name means that it will only hold numbers. If you answer INPUT Num0% with, say, "fishcakes", the system will mostly ignore you, and just put zero in Num0%
... and change the PRINT "What is x times y?" to...
PRINT "What is ";Num0%;" times ";Num1%;
... and get that much "working".
Notice that the question mark for the end of the question is NOT coming from the PRINT statement. It IS coming from the INPUT statement.
Now add the following, just after the INPUT. Run the program until it works after you enter each, before you make another change.
IF UsersAnswer%= Num0%*Num1% THEN PRINT "Right answer! (^_^)" IF UsersAnswer%><Num0%*Num1% THEN PRINT ":-(... NOT right answer" PRINT
Notice the "><" for "does not equal". You can also use < for "less than" or > for "greater than".
A word about the equals sign: USUALLY, as it has in several places here, e.g. Num0%=RND(8)+1, it means "becomes". That's when it is being used to change what's in a variable.
Once in a while, the equals sign will be part of a "condition", something that is either true or false. IF x=5 THEN... is asking whether x equals 5, i.e. is the variable holding 5 at the present time. In an IF statement, the equals sign has it's old meaning.
It's...
REPEAT Make a small change Test the change Make the latest new bit work UNTIL Program finished
... if you want to get programs finished sooner, in the long run.
The trick is mastering the art of building the program up through small changes. It must "work" (for the things put in so far) at every step. Successfully thinking where to start, and what changes to make first takes practice.
What we've done gives us quite a nice little program. Are we done?
A true geek would say "The program is NEVER done! It can always be improved"!
Take a break. There was a lot in the above. (Give it a quick re-read, why don't you?) And then come back, and we will add...
What's the fun of a test, if there's no score?!!!
Here's our program so far...
REM Partly finished test of multiplication fluency REM for https://wywtk.com/prgm/bas/bas2-mathtest.htm REM vers 22 Feb 2025 REM started 22 Feb 25 QsToAsk%=4 REPEAT Num0%=RND(8)+1 Num1%=RND(8)+1 PRINT "What is ";Num0%;" times ";Num1%; INPUT UsersAnswer% IF UsersAnswer%= Num0%*Num1% THEN PRINT "Right answer! (^_^)" IF UsersAnswer%><Num0%*Num1% THEN PRINT ":-(... NOT right answer" QsToAsk%=QsToAsk%-1 UNTIL QsToAsk%<1 PRINT PRINT "Goodbye"
Just after QsToAsk%=4 add...
QsInTest%=QsToAsk% Score%=0
Just after UNTIL QsToAsk%<1 add...
PRINT "Your score was ";Score%;" out of ";QsInTest%;
.. and make sure that "works". It won't keep the score yet, but this illustrates doing a bit at a time.
I hope you can see that we're nearly finished? "All" we need to do is to add Score%=Score%+1 to what happens when UsersAnswer%= Num0%*Num1%!... but that's a little fiddley. Look closely at what is below... it is a mixture of things we had before, and some rearranging, and some new stuff...
INPUT UsersAnswer% IF UsersAnswer%= Num0%*Num1% THEN PRINT "Right answer! (^_^)" Score%=Score%+1 ENDIF IF UsersAnswer%><Num0%*Num1% THEN PRINT ":-(... NOT right answer" QsToAsk%=QsToAsk%-1 UNTIL QsToAsk%<1
All of the trouble stems from us wanting TWO things to happen when UsersAnswer%= Num0%*Num1%
First we must start a new line after the THEN.
We need, obviously, to add the Score%=Score%+1.
And we need to use ENDIF to show where the things to do when UsersAnswer% does kin fact equal Num0%*Num1%.
There's no rocket science in that. Try to grasp the general form of an IF statement with multiple things to happen when the condition is true...
IF {condition} THEN {first thing you want to happen} {next thing you want to happen} . . . {last thing you want to happen} ENDIF
Many things in programming will have "general forms" to remember. For novices, many, many problems in their code boil down to not conforming to a "general form".
So, that's it, once you get the typos and tiny deviations from "general form" ironed out.
Or is it??? What would YOU put in a game/ learning aid like this?
Copying other people's programs is a way to learn, but it isn't nearly as much fun as writing a program that does something that you thought would be cool.
Have an idea for the game/ exercise? Add the necessary code!
How about TIMING how long the user takes to finish the exercise? The following gives you what you need to add that. It doesn't give enough help, of course... but it gives you what you need!.... Try the following...
PRINT TIME WAIT (100) PRINT TIME
Good luck, if you try to add the "how long you took" feature!
And then there's...
How about having the program keep track of which pairs of digits the user gets right, and presenting (mostly) the ones he/ she gets wrong? That can be done... but you don't know several things... yet... that you'd need for that.
So I hope these pages are helping you learn!
Please get in touch if you discover flaws in this page. Please mention the page's URL. (wywtk.com/prgm/bas/bas2-mathtest.htm).
If you found this of interest, please mention in forums, give it a Facebook "like", Google "Plus", or whatever. If you want more of this stuff, help!? There's not much point in me writing these things, if no one feels they are of any use.
|
Unlike the clever Google search engine, this one merely looks for the words you type, so....
* Spell them properly.
* Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....
Please also note that I have four other sites, and that this search will not include them. They have their own search buttons.
My SheepdogSoftware.co.uk site, where you'll find my main homepage. It has links for other areas, such as education, programming, investing.
Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org.
Why is there a script or hidden graphic on this page? I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Neither my webpages nor my programs incorporate spyware, but if the page has Google ads, they also involve scripts. Why do I mention the script? Be sure you know all you need to about spyware.
....... P a g e . . . E n d s .....