Created
April 9, 2022 19:05
-
-
Save genghisjahn/562f4e2c1aff6190863c622ab164fffa to your computer and use it in GitHub Desktop.
First Program Part II
This file contains 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
package main | |
import ( | |
"flag" | |
"fmt" | |
"math/rand" | |
"strconv" | |
"time" | |
) | |
func main() { | |
var n int | |
var ug string | |
var g int | |
var c int | |
var maxg int = 10 | |
var maxnum int = 100 | |
flag.IntVar(&maxg, "guess", 10, "Number of guesses to win. Default value is 10.") | |
flag.IntVar(&maxnum, "maxnum", 100, "Maxium value the number to guess can be. Default value is 100.") | |
flag.Parse() | |
rand.Seed(int64(time.Now().Nanosecond())) | |
n = rand.Intn(maxnum) | |
for { | |
var inputError error | |
fmt.Printf("Enter your guess: ") | |
fmt.Scanln(&ug) | |
c = c + 1 | |
g, inputError = strconv.Atoi(ug) | |
if inputError != nil { | |
fmt.Println("Not a number:", ug) | |
fmt.Println("Try again.") | |
continue | |
} | |
maxg = maxg - 1 | |
if g == n { | |
fmt.Println("You guessed it!") | |
fmt.Println("No. Guesses: ", c) | |
break | |
} else if g > n { | |
fmt.Println("Too high.") | |
} else { | |
fmt.Println("Too low.") | |
} | |
if maxg == 0 { | |
fmt.Println("Out of guesses. Better luck next time.") | |
break | |
} | |
fmt.Println("You have", maxg, "guesses remaning.") | |
fmt.Println("") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment