Advertisement
Guest User

math

a guest
Mar 6th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.35 KB | None | 0 0
  1. -- Math Program test
  2.  
  3. function genRandomNum() --# Random Function
  4.     num1 = math.random(10)
  5.     num2 = math.random(10)
  6.     sign = math.random(4)
  7.     print("Question: "..questionOn) --# Prints current question the user is on
  8.     if sign == 1 then --# Loop for getting random sign
  9.         answer = num1 + num2
  10.         print("Whats "..num1.." + "..num2.." ?")
  11.     elseif sign == 2 then
  12.         answer = num1 - num2
  13.         print("Whats "..num1.." - "..num2.." ?")
  14.     elseif sign == 3 then
  15.         answer = num1 / num2
  16.         print("Whats "..num1.." / "..num2.." ?")
  17.     elseif sign == 4 then
  18.         answer = num1 * num2
  19.         print("Whats "..num1.." * "..num2.." ?")
  20.     end
  21.    
  22. end
  23.  
  24. r = 0
  25. w = 0
  26.  
  27. write("How many question do you want to be asked? ")
  28. numQuestion = tonumber(io.read()) --# Reads how many questions the user wants
  29. term.clear()
  30.  
  31. for x = 1, numQuestion do --# Loops until reaches the users amount of questions
  32.     questionOn = x
  33.     term.clear()
  34.     term.setCursorPos(1,1)
  35.     genRandomNum()
  36.     term.setCursorPos(1, 3)
  37.    
  38.     write("Answer: ")
  39.    
  40.     userAnswer = tonumber(io.read())
  41.  
  42.     if userAnswer == answer then
  43.         r = r + 1 --# Adds to questions correct
  44.     else
  45.         w = w + 1 --# Adds to questions incorrect
  46.     end
  47. end
  48. --[[ Prints final results when program has finished ]]--
  49. term.clear()
  50. term.setCursorPos(1,1)
  51. print("Total Questions: "..numQuestion)
  52. print("You got "..r.." Correct!")
  53. print("You got "..w.." Wrong. ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement