Jump to content


Photo

Chapter 4 - , Line 4, In <module> Builtins.valueerror: Need More Than 0 Values To Unpack


  • Please log in to reply
2 replies to this topic

#1 stockita

stockita

    New Member

  • Members
  • Pip
  • 2 posts

Posted 09 March 2014 - 08:28 PM

Hello,

I wrote the code and down load the file results.txt they are in one directory... but return the error : , line 4, in <module>
builtins.ValueError: need more than 0 values to unpack

the code the i wrote:

highest_score = 0
result_f = open("results.txt")
for line in result_f:
(name, score) = line.split()
if float(score) > highest_score:
highest_score = float(score)
result_f.close()
print('The highest score was:')
print(highest_score)

#2 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 83 posts
  • Gender:Not Telling

Posted 13 March 2014 - 02:56 AM


The code is perfectly fine if you have indented it properly.

Probably the problem is while copying the "results.txt" file content ( or after downloading the "results.txt" file, you opened it and messed up a little bit ;) :P ); you left a blank line at the top of the file. That's why the split() method is not able to unpack anything. i.e., 0 values to unpack.

highest_score = 0
result_f = open("results.txt")
for line in result_f:
    (name, score) = line.split() # Problem is here line no. 4; the built-in method split get no value to unpack so ValueError.
    if float(score) > highest_score:
        highest_score = float(score)
result_f.close()
print('The highest score was:')
print(highest_score) 

Try opening the file and clear any blank lines at the top.
:rolleyes: B)


Edited by #TM#, 13 March 2014 - 02:58 AM.


#3 stockita

stockita

    New Member

  • Members
  • Pip
  • 2 posts

Posted 18 March 2014 - 03:47 AM

thanks,
right, actually the code is indented, im using Komodo and wings, however that wasn't the case.
you were right, i did re write the result.txt file my self, and it works now.

thanks again.




The code is perfectly fine if you have indented it properly.

Probably the problem is while copying the "results.txt" file content ( or after downloading the "results.txt" file, you opened it and messed up a little bit ;) :P ); you left a blank line at the top of the file. That's why the split() method is not able to unpack anything. i.e., 0 values to unpack.

highest_score = 0
result_f = open("results.txt")
for line in result_f:
    (name, score) = line.split() # Problem is here line no. 4; the built-in method split get no value to unpack so ValueError.
    if float(score) > highest_score:
        highest_score = float(score)
result_f.close()
print('The highest score was:')
print(highest_score) 

Try opening the file and clear any blank lines at the top.
:rolleyes: B)







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users