Doing simple math for children.

Hello!

I saw that there were a few differente ways to do math within bash scripts.

expr and bc are possibilities. But which one to use when?

I want to make an simple bash script for children to do math.
The script must ask only questions like 11= till 1010= ...
No + or - or / only * questions.

When the good answer is given to a questions, the next question must be shown.

When the wrong answer is given to a question, the same question must be repeated till the good answer is given.

The output must be logged to a file. the questions begins from 11 till 110, and the same for 21 till 210, 3, 4, 5, 6, 7, 8, 9 etc.....
How can I accomplish this, or can you help me out with it?

10* Many, many thanks in advance :slight_smile:

How much have you tried??
Did you create a script already?

regards,
Usha

I have not done anything, I was experimenting on different scripts with:

  • bc
  • expr
  • asking questions like:
trap
trap 'echo thanks for the math' EXIT
MATH=$((9))
echo '3*3='
while read -p 'answer: '  answer 1
do
  if [ "answer1" = "GETAL" ]
  then
    echo 'Right!'
    exit
  fi
  echo 'wrong!'
done

So I am still experimenting to see what happens. I am not so good in bash, that why I ask some help.

This will give you something to start with. Modify as required:

#!/bin/bash
i=1
while [ $i -le 10 ]; do
    j=1
    while [ $j -le 10 ]; do
        echo "$i * $j = $((i*j))"
        ((j+=1))
    done
    ((i+=1))
done

Regards,
Mark.

Yes what I see is the whole list. Thanks! That is great.

How can we now change this, so that one question will be asked one by one.

It's like 3*3 = .... the question must be filled in. If the correct answer is given than the next question will be asked, either the same question will be repeated till the correct answer is given.

Will the command trap be used in here? Or is something more need to be done..

The thing is, with what you're trying to do, you'll have to check each input (if you want it to repeat until they get it right at least). Which, unless there's some math library I don't know about out there to check answers, means you'll have to put in the correct answers (like a switch statement or something, or at least that's what comes to mind first for me).

I'm looking at it right now and I'll let you know if I come up with something.

edit:
I think this might be what you're looking for...

 
#!/bin/bash
i=1
while [ $i -le 10 ]; do
    j=1
    while [ $j -le 10 ]; do
      echo "$i * $j = "  
      read answer1
      if ["$answer1" -eq "$((i*j))"]
      then
        echo 'Right!'
        ((j+=1))
      else
        echo 'Wrong!' 
     fi     
    done
    ((i+=1))
done
1 Like

Almost!
But this looks very well devrmike.

What now happens is that I get the next while running the script:

~$   bash test1
1 * 1 = 
1
test1: line 8: [1: command not found
Wrong!

What happens in here? something wrong with
if ["$answer1" -eq "$((i*j))"] ??

I have also the next script which is similar.

#!/bin/bash
trap 'echo Very well, keep practicing' EXIT

for (( i = 1; i <= 10; i++ ))
do
   for (( j = 1; j <= 10; j++ ))
   do
       answer=0
       while (( answer != i * j ))
       do
            read -p "$i * $j = " answer
[[ ${answer,,} == q ]] && exit

   
       done
   done
done

Where in here do I put your answer is wrong or your answer is right as result, right before the next question is asked?

This just compares whatever their input is with the actual answer.

I've checked that and both values come out correct. If you add the bolded statements to it, you'll see that the values come out correctly, so it should just be comparing them.

 
#!/bin/bash
i=1
while [ $i -le 10 ]; do
    j=1
    while [ $j -le 10 ]; do
      echo "$i * $j = "  
      read answer1
      echo "answer1 is... $answer1"
     echo "calculation is... $((i*j))"
      if ["$answer1" -eq "$((i*j))"]
      then
        echo 'Right!'
        ((j+=1))
      else
        echo 'Wrong!' 
     fi     
    done
    ((i+=1))
done

I'm not sure why you're getting that error... pretty awfully non-descript. I'll take a look at your second script and see if we can manage to get that to work how you want it.

That's because you need spaces between your square brackets and contents of the test. In the following example, the first test succeeds, the second test errors:

if [ 1 -eq 1 ]; then echo yes; fi
if [1 -eq 1]; then echo yes; fi

Regards,
Mark.

Yes, still practicing, and making a script for the children.
So far thanks for your information. Together with the above script I have a second script. (see below)

What goes wrong is, when a question is asked wrong, it will repeat this question. So far so good.
But when the second time the answer to the question is given correct, it still repeats!
I think there is something wrong with the "else.......read $getallinks x $getalrechts" part... But what is?

It has to repeat the question with the wrong answer till the correct answer is given.. !

Still learning much with bash scripting.. Thanks to you!

You're creating an infinite loop with this. Once it's wrong, it never exits because you never get a new value.

 
while (( $antwoordleerling != $antwoordjuiste ))
do
echo "Fout! Probeer het nogmaals $getallinks x $getalrechts =" 
read $getallinks x $getalrechts

I think this should fix it and work to your liking.

 
#!/bin/bash
while true
do
getallinks=$((RANDOM/(32767/10)+1))
getalrechts=$((RANDOM/(32767/10)+1))
echo "Hoeveel is $getallinks x $getalrechts ?"
read antwoordleerling
antwoordjuiste=$((getallinks*getalrechts))
if test  $antwoordleerling == $antwoordjuiste 
then
echo "Goed zo! $getallinks x $getalrechts is inderdaad $antwoordjuiste."
else
while (( $antwoordleerling != $antwoordjuiste ))
do
echo "Fout! Probeer het nogmaals $getallinks x $getalrechts =" 
read antwoordleerling
done
fi
done