Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local currency = "%s USD"
- local coins = {2,1,0.5,0.2,0.1,0.05,0.02,0.01}
- math.randomseed(os.time())
- local total = math.random(1,10) + math.random(0,99)/100
- local given = total + math.random(1,5) + math.random(0,99)/100
- print("Total price: "..currency:format(total))
- print("Money given by the customer: "..currency:format(given))
- local solution,try = {},{}
- for k,v in pairs(coins) do
- local am = math.floor(given/v)
- solution[v] = am
- given = given - am*v
- given = math.floor(given*100+0.5)/100
- io.write("How much will you give of "..currency:format(v).."? ")
- try[v] = tonumber(io.read()) or error("tonumber: sigh",0)
- end
- print(" Currency | Solution | Entered | Result")
- local padding = "%9s | %8s | %-7s | "
- for k,v in pairs(coins) do
- local yeuy = solution[v]==try[v] and "GOOD" or "YOU GOT AN ERROR HERE"
- print(padding:format(currency:format(v),solution[v],currency:format(try[v]))..yeuy)
- end
Advertisement
Add Comment
Please, Sign In to add comment