#๐ Issue with Getting Value Pulled from Text file
46 messages ยท Page 1 of 1 (latest)
@uncut moss
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.
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
can you send an example of what the lines in grades.txt look like?
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?
There's a second part of the assignment where I need to take the three grades in the .txt file, compute their average, and write that info into a new .txt file
From what my instructor said, we have the second while loop to assist with that task
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?
When I run the code, nothing happens. I put in a print statement to see if the variable GradeList is being pulled correctly, but it seems the variable isn't being pulled properly.
you never write it to your outfile
what does "isn't being pulled properly" mean
like incorrect values or it's empty
I broke up the code from the outfile section to see if GradeList was being updated correctly. I'm not sure if it is being given the scores from the text file.
Here is the full code I had: https://paste.pythondiscord.com/KVWA
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?
When I run the code, nothing happens
Nothing gets printed
I meant here
when you printed the GradeList
Tha'ts what I mean. When I try printing the GradeList, it doesn't print
oh
on your first loop you increment index to the end
then you never reset it
the second while loop never runs
I see. What do you suggest I adjust to fix that?
just putting index = 0 after the first while loop
Is that with the if statement, elif statement, or both?
both
So I would replace line with line[index]
yes
Trying to run the code. Just having an issue with my server.
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.