Python Forum
Please, help to identify the mistake in the code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please, help to identify the mistake in the code
#1
Dear Python Users,

Please, help to identify where is my mistake in the code. What I am trying to do is to write the info (lines 1-3) into txt file.
However, when I run this code the python never executes it, it continues to run forever.

tsv = [20, 31, 52, 73]
forecast = [0, 20, 31, 52]
forecast_error = [0, 0, 0, 0]

for x in range(0, len(tsv)):
   forecast_error[x] = tsv[x] - forecast[x]

F1 = open('C:\\Users\\Documents\\Exercise2\\fixeddata.txt', 'w')
#example line
#37        52        352
#          111111111122222222222
#0123456789012345678901234567890

for x in range(0, len(tsv)):
   strrec = ""
   #handle tsv
   str1 = str(tsv[x])
   while len(str1) < 10:
       str1 = str1 + ""
   strrec = strrec + str1 
   #handle forecast
   str1 = str(forecast[x])
   while len(str1) < 10:
       str1 = str1 + ""
   strrec = strrec + str1
   #handle forecast error
   str1 = str(forecast_error[x])
   while len(str1) < 10:
       str1 = str1 + ""
   strrec = strrec + str1
   strrec = strrec + "\n"
   F1.write(strrec)
       
F1.close()
Reply
#2
Do you really mean 'C:\\Users\\Documents\\Exercise2\\fixeddata.txt'? Shouldn't that be 'C:\\Users\\Alberto\\Documents\\Exercise2\\fixeddata.txt' or something like this? And do the directories exist?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
I would guess that problem is with
 while len(str1) < 10:
      str1 = str1 + ""
Your first str1 has length 2 and adding empty string does not increase it, so its infinite loop ...

You probably wanted to use
       str1 = str1 + " " # notice space inside quotes
You should check basic string formating and perhaps zip function - you could use constructs like
for ts, fore in zip(tsv, forecast):
   print("{}{:10}{:10}".format(ts, fore, ts - fore))
to shorten your code.
Reply
#4
Quote:never executes it, it continues to run forever
is an oxymoron.
Reply
#5
(Jun-21-2017, 10:45 PM)zivoni Wrote: I would guess that problem is with
 while len(str1) < 10:
      str1 = str1 + ""
Your first str1 has length 2 and adding empty string does not increase it, so its infinite loop ...

You probably wanted to use
       str1 = str1 + " " # notice space inside quotes
You should check basic string formating and perhaps zip function - you could use constructs like
for ts, fore in zip(tsv, forecast):
   print("{}{:10}{:10}".format(ts, fore, ts - fore))
to shorten your code.

Thank you very much for your answer! The problem was in
       str1 = str1 + " " # notice space inside quotes
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  identify not white pixels in bmp flash77 17 2,266 Nov-10-2023, 09:21 PM
Last Post: flash77
  [Solved] Please, help me find a simple mistake AlekseyPython 2 1,701 Jun-17-2021, 12:20 PM
Last Post: AlekseyPython
  [split] Could you please clarify where i did mistake also how run without admin right Abubakkar 1 1,753 Jun-14-2021, 09:32 AM
Last Post: Larz60+
  guys please help me , pycharm is not able to identify my xlsx file CrazyGreenYT7 1 1,971 Jun-13-2021, 02:22 PM
Last Post: Larz60+
  Need to identify only files created today. tester_V 5 4,549 Feb-18-2021, 06:32 AM
Last Post: tester_V
  Please help to me to find my mistake in code leonardin 2 1,801 Nov-29-2020, 04:17 PM
Last Post: Larz60+
  minor mistake in code for factorial spalisetty06 2 1,854 Aug-22-2020, 05:00 PM
Last Post: spalisetty06
  Simple mistake about for Nomatter 4 2,183 Jul-16-2020, 02:24 PM
Last Post: Nomatter
  Install Mistake jlerette5 1 1,857 Feb-18-2020, 12:19 AM
Last Post: jefsummers
  Need to identify sheet color in excel workbook chewy1418 2 2,444 Feb-14-2020, 03:26 PM
Last Post: chewy1418

Forum Jump:

User Panel Messages

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