Jump to content


Photo

Chapter 4 Problem

Name Error

  • Please log in to reply
3 replies to this topic

#1 suckingspider

suckingspider

    New Member

  • Members
  • Pip
  • 2 posts

Posted 12 July 2015 - 04:15 PM

this is the code I write it . It is exactly as what the book write.
 
 
scores=[]
result_f = open("results.txt")
for line in result_f:
    (name.score) = line.split()
    scores.append(float(score))
    
result_f.close()
 
print (scores[0])
print (scores[1])
print (scores[2])
 
However, that is the result get from the shell
 
Traceback (most recent call last):
  File "C:/Users/lihuiwen/Desktop/日电脑!/array1.py", line 4, in <module>
    (name.score) = line.split()
NameError: name 'name' is not defined
 
Anyone can help me solve that ?
 


#2 #TM#

#TM#

    Active Member

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

Posted 13 July 2015 - 12:44 AM

 

this is the code I write it . It is exactly as what the book write.
 
 
scores=[]
result_f = open("results.txt")
for line in result_f:
    (name.score) = line.split()
    scores.append(float(score))
    
result_f.close()
 
print (scores[0])
print (scores[1])
print (scores[2])
 
However, that is the result get from the shell
 
Traceback (most recent call last):
  File "C:/Users/lihuiwen/Desktop/日电脑!/array1.py", line 4, in <module>
    (name.score) = line.split()
NameError: name 'name' is not defined
 
Anyone can help me solve that ?

 

 

Hello, suckingspider, I must say that please use <> button (called code tag) in the editor while pasting code. sad.png 

 

Anyway, there is slight problem in your code. Just look at your traceback and the line giving the error.

 

your code uses

(name.score) = line.split()

rather than

(name,score) = line.split()

which changes the whole meaning in python.

 

. and , have a lot different meaning when used in context of Python and can devastate your program. tongue.pnglaugh.pngcool.png

 



#3 Vitü

Vitü

    New Member

  • Members
  • Pip
  • 1 posts

Posted 13 July 2015 - 10:30 PM

I had a problem in chapter 4:
I wrote the code exactly as it is writen in the book




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)



But the result I got from the shell was this:


>>>Traceback (most recent call last):
File "C:\Users\Victor\Desktop\Porcarias\Python\highest score.py", line 4, in <module>
(name,score) = line.split()
ValueError: need more than 0 values to unpack
>>>


Can anyone help me with this problem?

#4 #TM#

#TM#

    Active Member

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

Posted 14 July 2015 - 05:53 AM

I had a problem in chapter 4:
I wrote the code exactly as it is writen in the book




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)



But the result I got from the shell was this:


>>>Traceback (most recent call last):
File "C:\Users\Victor\Desktop\Porcarias\Python\highest score.py", line 4, in <module>
(name,score) = line.split()
ValueError: need more than 0 values to unpack
>>>


Can anyone help me with this problem?

Hello Vitu, Can you please post your code using <> button on the editor sad.png  It is difficult to debug things. It may be an indentation problem. Who knows!?

 

Though in your case i think it is the results.txt file. I think you have probably opened and messed up with that file tongue.png . There will be a blank line on the top of that file. Open that file and delete that blank line. smile.png






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users