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.
-
14:08
Forrest Galante
7 hours agoPrivate Tour Of the World's Most Expensive Pet Show
49K4 -
13:50
Nikko Ortiz
16 hours agoStop Hurting Yourself For Views.
4.8K3 -
2:07:06
Side Scrollers Podcast
1 day agoDiaper Furry Streamer Gets ONLY ONE DAY Suspension + Hasan PLAYS VICTIM + More | Side Scrollers
31.4K20 -
56:38
DeProgramShow
1 day agoDeprogram with Ted Rall and John Kiriakou: "Jake Tapper on the Global Hunt for an Al Qaeda Killer”
4.25K3 -
1:43:07
The Michelle Moore Show
2 days ago'The 12 Open Doors' Guest, Steve Jarvis: The Michelle Moore Show (Oct 17, 2025)
12.9K10 -
LIVE
Lofi Girl
3 years agolofi hip hop radio 📚 - beats to relax/study to
108 watching -
1:45:06
Badlands Media
23 hours agoDevolution Power Hour Ep. 399: No Kings, Antifa’s Collapse & Trump’s Year of Peace
276K84 -
2:56:00
Laura Loomer
10 hours agoEP150: New Yorkers Brace For Islamic Takeover After Mayoral Election Debate
81.5K98 -
1:35:37
Man in America
14 hours agoThe Forbidden Medicine of Light: Why is Big Pharma HIDING This From Us?
61.6K23 -
2:35:13
BlackDiamondGunsandGear
7 hours agoAFTER HOURS ARMORY / BUILDING GUNS ARE ILLEGAL? / Marine Gun Builder RETURNS!!
22.3K3