-
Notifications
You must be signed in to change notification settings - Fork 0
/
guessing-game.jl
56 lines (52 loc) · 1.32 KB
/
guessing-game.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function ask_user_for_a_guess()
print("Please enter an integer:")
while true
x = readline()
try
y = int(x)
return y
catch
println("Sorry, that wasn't an integer")
end
end
end
function ask_user_to_play_again()
print("Hey, would like like to play another game? (y/n):")
answer = readline()
answer = lowercase(strip(answer))[1]
if answer == 'y'
return true
elseif answer == 'n'
return false
end
while true
print("Please enter either \"yes\" or \"no\":")
answer = readline()
answer = lowercase(strip(answer))[1]
if answer == 'y'
return true
elseif answer == 'n'
return false
end
end
end
play = true
while play
println("""Welcome to the Guessing Game!
I have a number in mind between 1 and 10 that I need you to guess.""")
value = rand(1:10)
guess = ask_user_for_a_guess()
while true
if guess > value
println("Your guess $guess was too high")
elseif guess < value
println("Your guess $guess was too low")
else
println("Yay! You won!")
break
end
guess = ask_user_for_a_guess()
end
play = ask_user_to_play_again()
end
println("Bye!")