Bottom Page

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()
Quote
#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
Quote
#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.
Quote
#4
Quote:never executes it, it continues to run forever
is an oxymoron.
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Need help to identify Mersenne Primes, I do need a search pattern. Pleiades 0 131 Dec-03-2019, 11:05 PM
Last Post: Pleiades
  What was my mistake in this Python code (easy)? voltman 4 174 Nov-19-2019, 09:58 PM
Last Post: snippsat
  countdown script not working..plz help what is mistake randyjack 1 131 Oct-28-2019, 06:57 AM
Last Post: perfringo
  Beginner mistake. bbweeg 1 297 Aug-17-2019, 07:27 AM
Last Post: perfringo
  Help to understand my mistake TeeMan 8 442 Jul-05-2019, 01:42 PM
Last Post: TeeMan
  Syntax Error : I can't identify what's wrong! caarsonr 11 735 Jun-10-2019, 11:18 PM
Last Post: Yoriz
  Identify two specific words next to each stahorse 9 571 Apr-26-2019, 09:59 AM
Last Post: perfringo
  Cobol code to English like language/Identify ENDIF for correspoding IF in a string Venkat 6 1,391 Apr-12-2018, 01:05 PM
Last Post: buran
  Identify Hidden rows in xls ayonsarkar 2 3,946 Apr-10-2018, 08:33 PM
Last Post: ljmetzger
  Need to find a mistake in my code boris602 3 853 Jan-11-2018, 01:49 PM
Last Post: boris602

Forum Jump:


Users browsing this thread: 1 Guest(s)