translated to BaContechnosaurus wrote:shell is so much easier for me ... maybe someone could translate it to perl
...
- what is 1+1|2
what is the square root of 9|3
Code: Select all
'#!/bin/sh
'# technosaurus http://www.murga-linux.com/puppy/viewtopic.php?t=67704
' commented sh lines by apostroph in this 1st BaCon version by L18L
'#initialize variables (just in case)
numright=0
'questions=0
'ask() { ##usage: ask "What is your question?" "correct answer"
FUNCTION ask(STRING q$, STRING a$)
LOCAL response$
'#ask question and wait for an answer feel free to replace read with dialog/xdialog
'read -p "$1 ? " response
INPUT CONCAT$(q$, " ? "), response$
'#put in all lower case
'answer=`echo $2 |tr [:upper:] [:lower:]`
a$ = LCASE$(a$)
'response=`echo $response |tr [:upper:] [:lower:]`
response$ = LCASE$(response$)
'#check ans
'[ "$response" == "$answer" ] && echo correct && numright=$(($numright+1)) || echo incorrect
IF response$ != a$ THEN RETURN "incorrect"
INCR numright
RETURN "correct"
END FUNCTION
'}
'IFS="
'"
OPEN "testfile" FOR READING AS myfile
'for oneline in `cat testfile`; do
WHILE NOT(ENDFILE(myfile)) DO
READLN oneline$ FROM myfile
SPLIT oneline$ BY "|" TO quiz$ SIZE questions
' question=`echo $oneline |cut -d "|" -f 1`
question$ = quiz$[0]
' answer=`echo $oneline |cut -d "|" -f 2`
answer$ = quiz$[1]
' ask "$question" "$answer"
response$ = ask(question$, answer$)
PRINT response$
' questions=$(($questions+1))
'done
WEND
CLOSE FILE myfile
'echo you got $numright out of $questions correct.
PRINT numright, questions FORMAT "you got %1$d out of %2$d Questions correct."
Code: Select all
What is the largest desert in the world|Sahara
what is 1+1|2
what is the square root of 9|3
but bacon source must have a blank line at the end
Edited:
Load above BaCon code into a file, say quiz.bac in directory $HOME/programming/bacon
Get syntax Highlighting in geany by
Document > Set Filetype > Programming Languages > FreeBasic source file
(That is how I use it, there are better ways to do it e.g. NicoEdit see http://bkhome.org/bacon/gettingstarted.htm )