Python Forum
Please, help to identify the mistake in the code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Please, help to identify the mistake in the code (/thread-3764.html)



Please, help to identify the mistake in the code - Alberto - Jun-21-2017

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()



RE: Please, help to identify the mistake in the code - Ofnuts - Jun-21-2017

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?


RE: Please, help to identify the mistake in the code - zivoni - Jun-21-2017

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.


RE: Please, help to identify the mistake in the code - Larz60+ - Jun-21-2017

Quote:never executes it, it continues to run forever
is an oxymoron.


RE: Please, help to identify the mistake in the code - Alberto - Jun-22-2017

(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