code_with_ki/2021-11-19/main.go

53 lines
1.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// SPDX-FileCopyrightText: 2021 Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: BSD-3-Clause
// Write a program that lets the user input a fruit (Apple, Banana, Strawberry).
// Depending on the input, let the program output a fun fact about the fruit.
// Ask the user if he wants to input another fruit, and if so let him do so.
// Think about what kind of variables you need and in which level to write them.
package main
import (
"fmt"
"os"
)
func main() {
prompt()
}
func printFact(s string) {
if s == "a" {
fmt.Println("Some apple varieties such as Pink Pearl and Kissabel have flesh that ranges from pink or orange to bright red.")
} else if s == "b" {
fmt.Println("Bananas dont actually grow on trees—they grow on plants that are officially classified as an herb (not surprisingly, the worlds largest herb). Theyre in the same family as lilies, orchids, and palms.")
} else if s == "s" {
fmt.Println("The strawberry is a member of rose family and it is the only fruit which has seeds outside.")
} else {
fmt.Println("Please choose one of [a]pple, [b]anana, or [s]trawberry.")
}
}
func loop() {
fmt.Print("Would you like to hear another fact? [y/n]: ")
var s string
fmt.Scanln(&s)
if s == "y" {
prompt()
} else if s == "n" {
os.Exit(0)
} else {
fmt.Println("Please enter either [y]es or [n]o.")
}
}
func prompt() {
fmt.Print("Please select a fruit to hear an interesting fact about ([a]pple, [b]anana, [s]trawberry): ")
var s string
fmt.Scanln(&s)
printFact(s)
loop()
}