1

I am looking for an answer to this error with my specific code. I have searched the others and they are still so confusing.

I am unsure why this is happening.

Here is the code section that the error is referencing, followed by the error.

def processScores( file, score):
#opens file using with method, reads each line with a for loop. If content in line
#agrees with parameters in  if statements, executes code in if statment. Otherwise, ignores line    

    with open(file,'r') as f:
        for line in f:  #starts for loop for all if statements
            if line[0].isdigit: 
                start = int(line) 
                score.initialScore(start) #checks if first line is a number if it is adds it to intial score

The error message I'm getting:

    Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    processScores('theText.txt',score)
  File "C:/Users/christopher/Desktop/hw2.py", line 49, in processScores
    start = int(line)
ValueError: invalid literal for int() with base 10: '\n'

Thanks everyone, I wouldn't be posting this if i didnt find a clear answer in the other posts

divibisan
  • 11,659
  • 11
  • 40
  • 58
Christopher Jakob
  • 91
  • 1
  • 2
  • 11

4 Answers4

9

This is giving you trouble:

edited: also as pointed by @PadraicCunningham, you're not calling the isdigit().. missing ()

if line[0].isdigit(): 
    start = int(line)

You're checking only line[0] is digit and then convert the whole line to start, the line could possibly contain Tab or Space or Linefeed.

Try this instead: start = int(line[0])

Also for a cleaner approach, you should strip() each line you're checking, and for the safe side in case the data being passed are like "5k" your logic needs to be a bit more SAFE, which you can use a try/except approach:

for line in f:
    line = line.strip()
    # edited: add `if line and ...` to skip empty string
    if line and line[0].isdigit():
        try:
            start = int(line) 
            score.initialScore(start)
        except ValueError:
            # do something with invalid input
    elif #... continue your code ...

As a side note, you should use if/elif to avoid unnecessary if checking if previous condition has already met.

Anzel
  • 19,825
  • 5
  • 51
  • 52
  • ohhh I see @Anzel I actually made a fix to this for something else. my start = int(line) once looked like start = lnt(line[0]) but then it was only reading the first number in the line. I.e. the first line was 50 but it was only printing 5. But then once I put start to where it needed to be i.e. start = int(line) i got 50. I still need the code for this particular if statement to only read the first line of the file though. so I guess im curious, what can i do to ensure that? can I change the code to this? if line(0).isdigit: start = int(line) – Christopher Jakob Jan 18 '15 at 00:54
  • @ChristopherJakob, try to **strip** each line you're trying to process, and you what about an invalid data like "5k" will pass your check but still fail the **int("5k")**... – Anzel Jan 18 '15 at 00:56
  • @ChristopherJakob, please see my updates for a proper check and assign **start** to be **int(line)**, one advice, never assume what the user/input could be, properly handle the assignment is the way to go – Anzel Jan 18 '15 at 01:01
  • i see so if I create an exception for a value error, the code will see it and will ignore it. continuing on with my code. Now I have never used an exception before so Im sorry if this quesiton is stupid, must i use the elif? I have several if statements would it be better to have the first if statement and then rest be elif statements? It seems that way, but unsure – Christopher Jakob Jan 18 '15 at 01:06
  • @ChristopherJakob, yes if you only want one of the conditions executes, always use `if/elif`... it saves overhead to keep checking unnecessary conditional check whereas previous `if` has already satisfied the requirement. – Anzel Jan 18 '15 at 01:12
  • heres the thing though lets just make sure elif is a right choice for this. the file is a only one item on each line of the file. The first is a number then a letter then a letter. certain letters mean that either the next line is a number or a list of numbers. My code is to look at the first line and add it to a running score and then go to the next line and see what letter is present if its a letter for a number then add that number to running score if its a letter to a list add the list the the running score. it needs to check for each line. So is elif still a good way to go? ty – Christopher Jakob Jan 18 '15 at 01:16
  • @ChristopherJakob, let's be clear, if each line is only either **number** or **string**, then use **elif** as you can only have 1 condition met, a number or string. For the next line, all the **if/elif** are running again. – Anzel Jan 18 '15 at 01:20
2

replace :

start = int(line) 

to

start = int(line.strip())   # strip will chop the '\n' 
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • @ChristopherJakob your line must contain only integer then only it will work – Hackaholic Jan 18 '15 at 00:59
  • ok cool so @Hackaholic , i have a new error. I suspect the error is stating that the code is also now reading white space? processScores('theText.txt',score) Grabing intial score from file, inital score set to 50 Traceback (most recent call last): File "", line 1, in processScores('theText.txt',score) File "C:/Users/christopher/Desktop/hw2.py", line 49, in processScores start = int(line.strip()) ValueError: invalid literal for int() with base 10: '' if that is the case should the code be int(line.strip(/n,'')) ? – Christopher Jakob Jan 18 '15 at 01:02
  • `int("10\n")` would still work, the OP is not actually calling isdgit – Padraic Cunningham Jan 18 '15 at 01:07
0

Alternatively, if you want to add the number instead of the first digit, you can use .strip() to remove any whitespace and newlines.

if line.strip().isdigit(): 
    start = int(line.strip()) 
    score.initialScore(start) #checks if first line is a number if it is adds it to intial score
Matthew
  • 160
  • 9
  • 1
    as a side note, it will fail if the line is "1.23" or "1.0". OP doesn't mention whether the input will be always Integers though. – Anzel Jan 18 '15 at 01:16
0

if the idea is to check the first line for a digit, how about using readlines instead of looping line by line; something like this.

I also think using regex is better

import re
def processScores( file, score):
#opens file using with method, reads each line with a for loop. If content in line
#agrees with parameters in  if statements, executes code in if statment. Otherwise, ignores line    

    f = open(file,'r')
    lines_list = f.readlines()
    if bool(re.search("^-?\\d*(\\.\\d+)?$",'-112.0707922')): 
        start = int(lines_list[0]) 
        score.initialScore(start)

credit: regex borrowed from here https://stackoverflow.com/a/22112198/815677

Krishna
  • 415
  • 1
  • 4
  • 11