Beginner ยท ages 9+ ยท functions ยท return ยท formatting
Temperature converter in Python.
Turn Celsius into Fahrenheit and print a conversion table.
The program
Press Run to see what it prints. Change anything you like, then run it again.
def to_fahrenheit(c):
return c * 9 / 5 + 32
for c in range(0, 101, 20):
print(f"{c} C = {to_fahrenheit(c):.1f} F")0 C = 32.0 F 20 C = 68.0 F 40 C = 104.0 F 60 C = 140.0 F 80 C = 176.0 F 100 C = 212.0 F
How it works
- A function is a named recipe.
defdefines it andreturnsends back the answer. range(0, 101, 20)counts in steps of 20.:.1finside an f-string means 'show one decimal place'.
Try this
- Write a function that turns Fahrenheit back into Celsius.
- Print every 10 degrees.
- Find the temperature where both scales give the same number.
Stuck on an error? Read how to debug Python.
Similar ideas
More programs
Palindrome checker
Check whether a word reads the same forwards and backwards.
Easy ยท ages 10+A calculator with functions
Write functions for add, subtract, multiply and divide, and use a dictionary to choose one.
Easy ยท ages 10+Shopping basket
Add up the price of items in a basket, with quantities and a discount.
Confident ยท ages 11+FAQ
Temperature converter: questions
What does the temperature converter program do?
Turn Celsius into Fahrenheit and print a conversion table. It prints: 0 C = 32.0 F / 20 C = 68.0 F / 40 C = 104.0 F โฆ
Which Python ideas does it use?
functions, return, formatting. Beginner level, suitable from about age 9.
How do I run it?
Press Run on this page. To run it on your own computer, install Python 3 and save the code in a file ending in .py. It prints the same thing.
What should I try next?
Write a function that turns Fahrenheit back into Celsius. Print every 10 degrees. Find the temperature where both scales give the same number.
Start a conversation
Want a teacher to guide the learning?
Tell us the learner's age and what they can already do, and we will suggest a starting point. A parent or guardian can make the enquiry.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Write some code right now
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay