Last active
February 24, 2021 18:25
-
-
Save Miqueas/5104bc348f913c91ecab4a218acfd201 to your computer and use it in GitHub Desktop.
[Lua] Classic guessing game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Generates a constantly-changin number | |
-- Note: Lua 5.4 don't need this | |
math.randomseed(os.time()) | |
local MagicNum = math.random(100) | |
local Lives = 3 | |
print ("Guess the number!") | |
print ("Enter your choice:") | |
io.write("> ") | |
local Choice = io.stdin:read("*n") | |
while true do | |
if Choice ~= MagicNum and Lives ~= 0 then | |
print ("Bad! Try again:") | |
io.write("> ") | |
Choice = io.stdin:read("*n") | |
Lives = Lives - 1 | |
elseif Choice ~= MagicNum and Lives == 0 then | |
print("You loss!") | |
print("The number is " .. MagicNum) | |
break | |
else | |
print("You win!") | |
break | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment