Advertisement
Ozzypig

Part 2 - Exercise 3 Solution

May 21st, 2018
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.63 KB | None | 0 0
  1. -- Problem: Imagine a game where a player starts at level 1 and gains
  2. -- a level when they gain 10 times their current level in experience.
  3. -- Given a player's total experience, calculate what level they are.
  4. -- Hard mode: do this without using a while-do or repeat-until block.
  5. -- Input
  6. total_xp = 270
  7. level = 1
  8. -- Calculation
  9. while total_xp >= level * 10 do
  10.     total_xp = total_xp - level * 10
  11.     level = level + 1
  12. end
  13. -- Output
  14. print("Level:", level)
  15.  
  16. -- Alternate solution using a repeat-until
  17. total_xp = 270
  18. level = 0
  19. repeat
  20.     level = level + 1
  21.     total_xp = total_xp - level * 10
  22. until total_xp < 0
  23. print("Level:", level)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement