einsteinK

Quickchange - The game

Mar 24th, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.91 KB | None | 0 0
  1.  
  2. local currency = "%s USD"
  3. local coins = {2,1,0.5,0.2,0.1,0.05,0.02,0.01}
  4.  
  5. math.randomseed(os.time())
  6.  
  7. local total = math.random(1,10) + math.random(0,99)/100
  8. local given = total + math.random(1,5) + math.random(0,99)/100
  9. print("Total price: "..currency:format(total))
  10. print("Money given by the customer: "..currency:format(given))
  11.  
  12. local solution,try = {},{}
  13. for k,v in pairs(coins) do
  14.     local am = math.floor(given/v)
  15.     solution[v] = am
  16.     given = given - am*v
  17.     given = math.floor(given*100+0.5)/100
  18.     io.write("How much will you give of "..currency:format(v).."? ")
  19.     try[v] = tonumber(io.read()) or error("tonumber: sigh",0)
  20. end
  21.  
  22. print(" Currency | Solution | Entered | Result")
  23. local padding = "%9s | %8s | %-7s | "
  24. for k,v in pairs(coins) do
  25.     local yeuy = solution[v]==try[v] and "GOOD" or "YOU GOT AN ERROR HERE"
  26.     print(padding:format(currency:format(v),solution[v],currency:format(try[v]))..yeuy)
  27. end
Advertisement
Add Comment
Please, Sign In to add comment