Python Forum
Incorrect code output (File not being created)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Incorrect code output (File not being created)
#1
Hello,
As a newbie I wrote the following code.
temperatures = [10, -20, -289, 100]

def temp_convert(temperatures):
    with open("temps.txt", 'w') as myfile:
        for c in temperatures:
            if c > -273.15:
                f = c* 9/5 + 32
                myfile.write(str(f) + "\n")  
My understanding is that this will create a file called temps.txt and this file will have 4 temperatures in it.

The code executes without any error messages.

However the file is not being created in the directory.

I am using python 3.7 in the Eclips IDE on windows 7.

I would really appreciate if someone can point out to me where I am going wrong.
Reply
#2
You need to call the function and pass the temperatures to it. I would add this at the bottom:

if __name__ == '__main__':
    convert(temperatures)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Hi Thanks for pointing out one of the mistakes.

I have added in the following line of code after myfile.write

temp_convert(temperatures)
Unfortunately its still not working. I am calling the temp_convert function and passing it a value but still there is no output.
Reply
#4
Add some print statements to tell what is happening. Say, one after the open file, and one to print each c as you go through the loop.
Reply
#5
(Dec-31-2018, 08:38 PM)Hass Wrote: I have added in the following line of code after myfile.write

What indentation did you add it at? It needs to be added flush left, completely unindented.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File is not being created with 'w' argument CAD79 3 434 Mar-14-2024, 12:05 PM
Last Post: snippsat
  problem in output of a snippet code akbarza 2 373 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  Code is returning the incorrect values. syntax error 007sonic 6 1,231 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,108 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  I cannot able to see output of this code ted 1 756 Feb-22-2023, 09:43 PM
Last Post: deanhystad
  why I dont get any output from this code William369 2 1,130 Jun-23-2022, 09:18 PM
Last Post: William369
  error 1102 (42000) incorrect database name 's' Anldra12 4 1,711 Jun-08-2022, 09:00 AM
Last Post: Anldra12
  How can I organize my code according to output that I want ilknurg 1 1,180 Mar-11-2022, 09:24 AM
Last Post: perfringo
  openpyxl incorrect delete rows VladislavM 6 4,123 Jul-19-2021, 08:54 AM
Last Post: VladislavM
  Incorrect Type Error milkycow 4 2,910 Jun-25-2021, 06:04 AM
Last Post: milkycow

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020