FAQ: Variables - Challenge: Temperature

This community-built FAQ covers the “Challenge: Temperature” exercise from the lesson “Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Swift

FAQs on the exercise Challenge: Temperature

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 (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 (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 (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In the Challenge temperature, temp results in a lot of decimals, how can we choose that the result shows only 2 decimals?

1 Like
var tempf:Double = 102.3

var tempc:Double?

tempc = (tempf - 32)/1.8

print("The temp is \(tempc!) degrees Celsius.")
var tempf:Double = 102.3

var tempc:Double?

tempc = (tempf - 32)/1.8

print("The temp is \(String(describing: tempc!)) degrees Celsius.")

why isn’t either of these answers accepted ?

// Write your code below 🌡️
var tempf:Double = 68

var tempc:Double?

tempc = (tempf - 32)/1.8

print("The temp is \(tempc) degrees Celsius.")

It isn’t working ;-;

The instructions specify

“Declare another Double variable named tempc.”

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.

Thanks for the help!

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.

3 Likes

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 :thermometer:

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 ().

1 Like

It should be "Celsius"

To preserve code formatting in forum posts, see: [How to] Format code in posts

Thanks! :man_shrugging:t2: Do you use a script to detect the differences, like from the command line or is it a regular programming language?

i don’t quite understand the question.

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.

“Declare a Double variable named tempf and initialize it with the temperature.”
All I the user was given was the formula: C=(F−32)/1.8

In the stuck? Hint: They say it’s var tempf: double = 83.0

How did they get that? It wasn’t mentioned in the story and online Mathway and AI got different answers.

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?

Very new here but this is how I managed to do it. Thanks to this post from Stack Overflow (seems very useful for these kinds of questions!):


import Foundation

var tempf: Double = 53
var tempc: Double

tempc = (tempf - 32)/1.8

var tempcRounded = String(format: "%.2f", tempc)

print("The temp is \(tempcRounded) degrees Celsius.")