There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
You can do so with the declaration var tempc: Double
In your code, you have used var tempc: Double? which is not quite a Double variable but is actually an Optional Double.
If your remove the ?, your code should work.
You can read more about optionals: Swift - Optionals
If you use a debugging statement like print(type(of: tempc)), you will see that the type is shown as Optional<Double>. If you omit the ?, then the type will be shown as Double.
why does one need to put “:” in order for var tempf: double = 40.0 to run instead of getting an error? I don’t fully understand the need for it or why I use it.
The instructions specify Declare a Double variable named tempf and initialize it with the temperature.
If we do something like var tempf = 40.0, this is valid syntax, but the instructions ask us to explicitly set the type of this variable to be Double. You may want to revisit the earlier lesson on types.
By setting a type for a variable, we ensure that if we try to assign a new value to our already declared variable, then values other than Double (such as strings or booleans) will create problems.
If we omit the type, then tempf can be assigned any value. You can set it to a string or a boolean or some other value of any type.
Since we are going to be doing mathematical calculations for converting the temperature to Celsius, so it makes sense that we declare the type as Double. If we omited the type, somebody could assign a string to tempf and our mathematical calculation code would break. So, making the type explicit is useful.
Hi. My code provides the correct answers and when I compare it to the Codecademy version, it appears to be the same. However, the step will not check off that final checkbox. See my code below:
My Code (no selection option for Swift):
// Write your code below
var tempf: Double = 72.0
var tempc: Double = 0.0
tempc = (tempf - 32)/1.8
print(“The temp is (tempc) degrees Celcius.”)
By the way, your example also has the mistake of specifying string interpolation with instead of ().
How did you tell the difference between the two programs? Did you just look at it, or did you use something like a shell script run with the two files to find my error?
Your code was pretty short (4 lines), so just looking at the code carefully was enough to reveal the typo.
But, for longer pieces of code, I first do a quick read and see if anything stands out. If that doesn’t reveal the issue, then I just use some free online diff tool (I usually use https://text-compare.com but there is nothing special about the site. You can do a web search for “text compare” and it will reveal plenty of similar websites).
For exercises which I have completed previously, I use my code for comparison. If I haven’t done a particular exercise/project, I either search the forums or do a google search for terms such as “codecademy name_of_exercise github” and use that code for comparison.
If that isn’t an option, I copy the code and try running it on some free online compiler/interpreter (e.g. google searches for “swift online”, “js online”, “python online” reveal many free options) and see what errors show up (they also usually highlight any syntax issues such as missing curly braces etc.). If the error is unfamiliar to me, I do web searches for that error and make use of results from stackoverflow and other sites to educate myself as to the cause of the issue. Once I have a reasonable understanding of the issue, then I share links or my thoughts on possible solutions.
You don’t have to use 83.0 as the value. It is just an example.
If you wanted, you could declare and initialize the variable as:
var tempf: Double = 65
You could do a web search for the current temperature in NYC or you could use a dummy value.
If you are still having trouble, there is a copy to clipboard button at the bottom of the exercise. To paste the code with proper formatting in the forums, see: How do I format code in my posts?