Premium Only Content

The With Statement |Section 5|Celestial Warrior
Please take a look at the following code:
temperatures=[10,-20,-289,100]
def c_to_f(c):
if c< -273.15:
return "That temperature doesn't make sense!"
else:
f=c*9/5+32
return f
for t in temperatures:
print(c_to_f(t))
The code prints outthe output of thec_to_f function multiple times in the terminal.
For this exercise,please write the outputin a text file instead of printing it out in the terminal.
The text file content will look like this:
50.0
-4.0
212.0
Please don't writeany messagein the text file when input is lowerthan -273.15.
One solution could be to open a file once and then iterate inside the functionwhile the file is open:
temperatures=[10,-20,-289,100]
def writer(temperatures):
with open("temps.txt", 'w') as file:
for c in temperatures:
if c>-273.15:
f=c*9/5+32
file.write(str(f)+"\n")
writer(temperatures)#Here we're calling the function, otherwise no output will be generated
Or even better, you could treat both the temperature input and the file path as function arguments:
temperatures=[10,-20,-289,100]
def writer(temperatures, filepath):
with open(filepath, 'w') as file:
for c in temperatures:
if c>-273.15:
f=c*9/5+32
file.write(str(f)+"\n")
writer(temperatures, "temps.txt")#Here we're calling the function, otherwise no output will be generated
In real life you may want to build a website with Python so you connectthe function callwriter()with a button on the website. The user enters some values in a text box, and types in a file name and thenpresses the button. The button executes the writer()function by using the input that user entered in the text box as input values for the function. Once the button is pressed,the generatedfile downloads on the user's browser.
Sure, that's a bit of a demanding project to do but Iwill take you step by step in later sections on how to do such web applications.
-
LIVE
Roseanne Barr
1 hour agoEnd-Time Prophecies REVEALED: Jonathan Cahn’s Warning
14,657 watching -
LIVE
The Jimmy Dore Show
18 minutes agoIn Undercover Video DOJ Investigator ADMITS Epstein Was CIA! UK Pushing COMPULSORY Digital ID!
2,220 watching -
LIVE
vivafrei
2 hours agoComey INDICTED! Proof Jan. 6 was a FED-SURRECTION! Ostrich Crisis Getting More Attention & MORE!
8,775 watching -
LIVE
Dr Disrespect
4 hours ago🔴LIVE - DR DISRESPECT - ARENA BREAKOUT: INFINITE - STORM EVENT
3,217 watching -
LIVE
Barry Cunningham
4 hours agoPRESIDENT TRUMP SPEAKS ON JAMES COMEY INDICTMENT! MORE TO COME! DELICIOUS LIBERAL MELTDOWNS!
2,089 watching -
25:59
Simply Bitcoin
1 day ago $0.16 earnedMichael Saylor Reveals $81T Bitcoin Plan to Cancel National Debt?!
4034 -
1:33:51
Steve-O's Wild Ride! Podcast
1 day ago $0.59 earnedJohn C. Reilly's Surprising Connection To Jackass (And Beef With Weeman!)
8.61K4 -
LIVE
StoneMountain64
2 hours agoBattlefield 6 News and Extraction Gaming
95 watching -
2:13:30
Side Scrollers Podcast
5 hours agoUK Introduces MANDATORY Digital ID + Dallas ICE Shooting BLAMED on Gaming + More | Side Scrollers
67.3K5 -
1:54:17
The Charlie Kirk Show
4 hours agoCharlie's Last Trip + What's Next + AMA | Erika Kirk, Mikey McCoy | 9.26.2025
242K226