-
Notifications
You must be signed in to change notification settings - Fork 0
/
G05GuessingGame.go
55 lines (46 loc) · 1.29 KB
/
G05GuessingGame.go
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
//Guessing game
//Author Jeremy Yon G00330435
package main
import (
"time"
"fmt"
"math/rand"
)
func main(){
// seeds the random number with currect time
rand.Seed(time.Now().UTC().UnixNano())
//generate secret number
secretNum := rand.Intn(100)
// gets the number of tries taken for the user to guess the secret number
var numOfTries = 1
// variable for user input
var input int
// prompt and get user input
fmt.Print("\nEnter guess (integer) and press enter: ")
_, err := fmt.Scan(&input)
if err != nil {
fmt.Println(err)
return
}
// keep looping until the secret number is guessed
for (input != secretNum){
// checks if user input is higher or lower than secret number
if input > secretNum{
fmt.Println("Your input is too high!")
}else{
fmt.Println("Your input is too low!")
}
// keep looping
fmt.Print("\nEnter guess (integer) and press enter: ")
_, err := fmt.Scan(&input)
if err != nil {
fmt.Println(err)
return
}
// adds the number of tries taken
numOfTries++
}
// print results
fmt.Printf("You took %d tries to guess the secret number\n",numOfTries)
fmt.Printf("The secret number is %d",secretNum)
}