#๐Ÿ”’ Error Appending info from .txt file to a variable

57 messages ยท Page 1 of 1 (latest)

sleek ermine
#

I'm building a program to reada student ID (MXX) and exam grades from an input file, calculate the average, and display the result on screen and write it to an output file. I need to first write the grades to the variable GradeList, but the variable doesn't seem to be getting the info from the .txt file. I'm not sure why GradeList isn't getting updated. Thoughts?

full code(with average function): https://paste.pythondiscord.com/AS4A
partial code: https://paste.pythondiscord.com/DFSQ
.txt file: https://paste.pythondiscord.com/53JA

mint geyserBOT
#

@sleek ermine

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.

sleek ermine
#

Thank you

#

Correct

#

I don't think I have one

#

Could you type an example?

cunning canopy
#

!e

text = """x1 1 2 3
x2 4 5 6
x3 7 8 9
"""

for line in text.splitlines():
    name, *values = line.split()
    print("name:", name, "average:", sum(int(value) for value in values) / len(values))
mint geyserBOT
sleek ermine
errant berry
#

does the output file have only averages?

sleek ermine
sleek ermine
errant berry
#

ok, trying

cunning canopy
#

Please don't post the solution. This is homework

#

@errant berry

errant berry
#

sure man

cunning canopy
#

It's about getting to the solution

#

I'm in VC with them

sleek ermine
#

I need the average function for the HW

#

I think it helps

#

Yep

#

That's correct

errant berry
#

are you sure you are writing the average to the file?

sleek ermine
sleek ermine
cunning canopy
#

I'm here

#

Yeah, that is looking good. You'd still want your file IO stuff in there, but it should be a bit easier now that the logic is reduced. You don't need total_test_scores=3 in your function.

#

Make sure that you write to your output file after you have all of the info you need to write

sleek ermine
cunning canopy
#

file IO just means file input/output. It's opening, reading and writing to files like you now have back in your code.

#

splitlines() is just for strings. You need your old method which was just looping over the file itself.

zenith kindle
#
            ID += line[index]
            index = 0
           #Skip spaces after the id```
mint geyserBOT
#

Hey @zenith kindle!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
zenith kindle
#
            ID += line[index]
            index = 0
           #Skip spaces after the id 
#

this code will run forever

cunning canopy
#

We're already past that

zenith kindle
#

good

cunning canopy
#

Once what we discussed is fixed, you'll have one more issue with your output file.

#

How's it going? @sleek ermine

sleek ermine
sleek ermine
#

Thoughts?

cunning canopy
#

Maybe the newline character can help "\n"

sleek ermine
#

Thank you for all of your help!

cunning canopy
#

Awesome!

#

Any time!

#

Cheers

sleek ermine
# cunning canopy Cheers

There was one more thing. I need to separate the ID and averages when the value is separated. This is the line that writes the info onto the outfile: outfile.write(ID+''+str(avr)+ '\n')

#

How could I get the space added?

cunning canopy
#

This is your output line: outfile.write(ID+''+str(avr)+ '\n') - just add the space where it needs to go. Not sure if you noticed that you have an empty string between ID and str(avr).

sleek ermine
#

!close

mint geyserBOT
#
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.