0
Help in Popsicles question with Swift
Apparently, I have finished the Popsicle question in code couch with all the programming languages, except Swift. I don't know how to create an input since I'm too stupid. Here is my attempt: let a = readLine() let b = readLine() var popsicles = Int(a) var siblings: = Int(b) if popsicles % siblings == 0 { print("give away") } else { print("eat them yourself") }
1 Réponse
+ 7
Your code is good but there are small errors 😅😅 Here I corrected it:
let a:String! = readLine()
let b:String! = readLine()
var siblings:Int! = Int(a)
var popsicles:Int! = Int(b)
if popsicles % siblings == 0 {
print("give away")
} else {
print("eat them yourself")
}
Use ! to force unwrap variables. Basically for Swift, every variable is not of type Int or String or etc but of type Int? or String? etc therefore you need to tell it that your variables are either String (by adding the !) either Int (same !).
Another small problem is that you inverted popsicles and siblings in input. The rest is perfect 👍👍