+ 1
Code Coach: How are values inputted? (Swift)
I am trying to do my first Code Coach challenge, specifically the Popsicles one. I realize that I am not quite understanding how the program will input the values into the code I create, so I have no idea what format my code should be in. I am still very new to coding and am using the Swift language. Should I be using a function? Should I just initialize two Int variables and assume the program will fill them in? The latter is what I did below. var siblings = Int() var popsicles = Int() if popsicles % siblings == 0 { print("give away") } else { print("eat them yourself") } This just creates a "divide by 0" fatal error, so I guess the values aren't actually being filled in. How am I supposed to be doing this?
10 Respostas
+ 2
Michelle code coach will provide the input through console. You can use readline() to take input from console.
Here is an example of taking input as integer:-
let number1 = Int(readLine(strippingNewline: true)!)!
In the program that you have attached, you are not taking any kind of input from the user and the default value of them is zero, so thats why it is generating "divide by zero error"
+ 2
~ swim ~ generally in swift, we don't take input from the console but from GUI instead, that's why taking input from console is hardly taught in any swift tutorial (not even sololearn's), and thus the confusion occurs.
+ 1
OK, now that I know about readLine and that I must force unwrap, this is what I have:
var siblings = readLine()
var popsicles = readLine()
if popsicles!%siblings! == 0 {
print("give away")
} else {
print("eat them yourself")
}
But this errors that I cannot use % with two String operand values. The inputs should be integers, so why does it think they are Strings???
+ 1
Michelle that's because, by default readline() takes input as *optional string*, you have to force wrap the input to integer.
Here is the fixđ
https://code.sololearn.com/cM56BBDbpP9x/?ref=app
+ 1
Michelle
Try this :
var siblings = readLine()!
var popsicles = readLine()!
if popsicles % siblings == 0{
print ("give away ")
}else {
print ("eat them yourself ")
}
+ 1
Thank so much guys! With your help and another user's i was finally able to solve it!
Destiny, yours doesn't quite work for me, but i did use it to come up with a second solution that works. What you need to do is use Int(popsicles) and Int(siblings) in order to use the % operand between them. You then also have to add a ! at the end to make them Int(popsicles)! and Int(siblings)!
What I did was Int(popsicles!)! with two exclamation points and none at the end of readLine(), but yours at the end of readLine() seems to take the place of the inner one that I needed.
Either way works!
+ 1
Michelle
It's good to know that ur problem is solved. đ
Sorry I forgot to put the "Int" there. đą
Keep on coding!! đđ
0
Hmm I don't recall readline() in the Swift lessons on here, and this is my first language, so that explains why I have no idea what I am doing. Lol. I did stop at the final group of lessons about XCode (because I haven't yet invested in a Mac, so I don't have it), so maybe getting user input is mentioned in there somewhere.
Thanks guys!