+ 1
SWIFT Halloween Candy
Three nights with no sleep. Tried Classes, Structures and settled for normal functions... Now, if I donât put the print line I donât get an output... other combinations end up telling me that I canât convert a String? to Int. I even tried to force the unwrap... I feel like Iâm getting lost in something stupid. Help? ... âĄïž How do I tell the code to print the result? Thank youuu https://code.sololearn.com/cC60EIrRcy47/?ref=app Thank you đđ»đđ»đđ»
7 RĂ©ponses
+ 2
I don't know if you code will solve the challenge.
But searching for swift codes on code playground gives me this solution:
print(calc(houses: houses))
Your problem is now that var houses is a String. I think you need to convert it to Integer.
+ 2
let input = readLine()
var num = Int(input!)
print(num!)
https://www.programiz.com/swift-programming/basic-input-output
I am not familiar with swift so I don't know if this the only solution. Maybe it is better for to learn the basics of swift before you continue with this challenge.
+ 1
solved! it was all around the management of optionals
+ 1
You need to get some sleep.
0
tried that - it wont let me convert it even with an if let and adding ?? i swear it must be something silly im missing on...
0
thanks denise, that article is how i did all my tries. surely the point of SoloLearn would be to be able to develop enough skills to be able to solve easy puzzles like these?
anyway ive amended the code and i can confirm that it still does not allow me as it tells me i need to convert num to integer on line 4
func calc(number: Int) -> Int {
let input = readLine()
var num = Int(input!)
var solution = 2 / num * 100
return solution
}
let input = readLine()
var num = Int(input!)
print(calc(number: num!))
if i make that same num as non-optional ! i get a fatal error... its just weird!
0
Maybe this article helps you:
https://learnappmaking.com/swift-optionals-how-to/