simple programming help?

This is the place for queries that don't fit in any of the other categories.

simple programming help?

Postby williamwatkins » Sun Jan 26, 2014 7:49 pm

Hi i have been given a task to read these numbers from within a text file
"1,3,5,7 ,9,11,13,15" and to add them together how would i do this? :)
Last edited by stranac on Sun Jan 26, 2014 8:09 pm, edited 1 time in total.
Reason: First post lock.
williamwatkins
 
Posts: 3
Joined: Sun Jan 26, 2014 7:41 pm

Re: simple programming help?

Postby stranac » Sun Jan 26, 2014 8:10 pm

You would go through a basic python tutorial.
Then you would give it a try using what you learned.
Then, if you had problems, you would come here and ask specific questions about your code.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1137
Joined: Thu Feb 07, 2013 3:42 pm

Re: simple programming help?

Postby williamwatkins » Sun Jan 26, 2014 8:12 pm

stranac wrote:You would go through a basic python tutorial.
Then you would give it a try using what you learned.
Then, if you had problems, you would come here and ask specific questions about your code.


okay i will thank you :)
williamwatkins
 
Posts: 3
Joined: Sun Jan 26, 2014 7:41 pm

Re: simple programming help?

Postby williamwatkins » Sun Jan 26, 2014 8:15 pm

i have this so far

Code: Select all
file = open("C:\Users\william\Desktop\School\programming\odd_numbers.txt, "r")

line=file.read()

intline= int(line)
print(intline)


i receive a error

eol while scanning litreal
Last edited by stranac on Sun Jan 26, 2014 9:03 pm, edited 1 time in total.
Reason: Added code tags.
williamwatkins
 
Posts: 3
Joined: Sun Jan 26, 2014 7:41 pm

Re: simple programming help?

Postby stranac » Sun Jan 26, 2014 9:07 pm

Please read this to learn how to post your code:
viewtopic.php?t=145

Also, you should be posting the entire error traceback, not just one line of it.
It makes helping much easier in most cases.

You are missing the closing quote in the filename.
You should be either using raw strings(r'your string here') or forward slashes for paths.

And you wont be able to turn an entire line to an int like that.
You'll need to split it, convert the pieces to ints an sum them up.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1137
Joined: Thu Feb 07, 2013 3:42 pm

Re: simple programming help?

Postby Somelauw » Sun Jan 26, 2014 9:59 pm

Code: Select all
file = open("C:\Users\william\Desktop\School\programming\odd_numbers.txt, "r")

line=file.read()

# Add this line for debugging. If your code works remove it.
print("Line is:", repr(line))

intline= int(line)
print(intline)


Some quotes are missing at the end of your filename.
Also you should use doube '\\' slashes or do r"C:\Users\william\Desktop\School\programming\odd_numbers.txt".
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 71
Joined: Tue Feb 12, 2013 8:30 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests