#๐Ÿ”’ Issue with Getting Value Pulled from Text file

46 messages ยท Page 1 of 1 (latest)

sly novaBOT
#

@uncut moss

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

uncut moss
#

So I need to open a .txt file with ID numbers and grades, and have it pull values from the file. However, it doesn't seem like the code is pulling value from the file. Please advise. https://paste.pythondiscord.com/SYKA

lunar kettle
#

can you send an example of what the lines in grades.txt look like?

lunar kettle
#

first thing I see is your first while loop just reassigns ID over and over until you get to the end

#

you never do any operations on anything but the last one

#

did you mean to have the second while loop nested inside of it?

uncut moss
#

From what my instructor said, we have the second while loop to assist with that task

lunar kettle
#

oh nvm

#

I read the first while loop wrong

#

it's building a string

#

okay, what output does the program give you and what are you expecting?

#

you said it outputs an empty file?

uncut moss
lunar kettle
#

you never write it to your outfile

#

what does "isn't being pulled properly" mean

#

like incorrect values or it's empty

uncut moss
lunar kettle
#

mh I think I see an issue

#
while index < len(line): # 2 cases
            #1. If current digit not a space then append current digit to current grade
            if line!='':
                current_grade+=line[index]
                #2. If current digit a space, then append the grade to the grades list and reset current grade back to empty string
            elif line=='':
                GradeList.append(int(current_grade))
                print(GradeList) # Check if the value is being pulled by doing this separately.
            index += 1
#

so here you have current_grade that is a string, you add each letter of the string until you find a space then append it to the gradelist

#

but you never reset the current_grade string

#

so if your input is M09 100 100 100 you get
[100, 100100, 100100100] (or something along those lines)

#

is that what you're seeing?

uncut moss
#

Nothing gets printed

lunar kettle
#

when you printed the GradeList

uncut moss
lunar kettle
#

oh

#

on your first loop you increment index to the end

#

then you never reset it

#

the second while loop never runs

uncut moss
lunar kettle
#

and here you compare line to a character instead of line[index]

lunar kettle
uncut moss
lunar kettle
#

both

uncut moss
lunar kettle
#

yes

uncut moss
sly novaBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.