[Tutor] Python 3.4.1 question for Mac users
I’m teaching myself Python 3.4.1 on a Mac and the book I’m using is written for Windows users. I’m trying to open a file on the desktop and I created a path using the example in the book. Any Mac users out there with a solution? My main drive is named “OS”. Here’s my code: def main(): my_file = input('Enter file to open: ') infile = open(r'\OS\Users\richarddillon\Desktop\my_file','r') file_contents = infile.read() infile.close() print(file.contents) main()___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] reading strings and calculating totals
I apologize in advance - This is my third week using Python (3.4.1 on a Mac) I need to read a text file, convert the values into numbers and calculate a total. The total I get doesn't match the values entered in the file. def main(): total = 0 infile = open('/Users/richarddillon/Desktop/numbers.txt', 'r') # read first record line = infile.readline() a = float(line) # read rest of records while line != '': total = total + a line = infile.readline() infile.close() print(total) main() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading strings and calculating totals
When I tried total = 0 with open('/Users/richarddillon/Desktop/numbers.txt', 'r') as infile: for line in infile: total += float(line) print(total) Python returned "ValueError: could not convert string to float: " Richard On Aug 30, 2014, at 1:13 PM, Alan Gauld wrote: > total = 0 > with open('/Users/richarddillon/Desktop/numbers.txt', 'r') as infile: >for line in infile: >total += float(line) > print(total) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] This is driving my crazy! My file hasn't been corrupted
My text file has five numbers, 1-5 I don't what the problem is. I've written the file using Word (saved as .txt ) as well as TextEdit Python 3.4.1 on a Mac Here's the code: # Richard Dillon # This program reads data from a .txt file and calculates a total # data in text file: 1,2,3,4 and 5 for a total of 15 # error message: Non-numeric data found in the file def main(): total = 0.0 try: infile = open('/Users/richarddillon/Desktop/numbers.txt', 'r') for line in infile: amount = float(line) total += amount infile.close() print(format(total, ',.2f')) except IOError: print('An error occured trying to read the file.') except ValueError: print('Non-numeric data found in the file.') except: print('An error occured.') main() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] format command help
I create column headings using \t print('base1\tbase2\theight\tarea') and I would like the numbers to align with the headings. I think that I need to use format instead of doing this: print(A,' ',B,' ',C,' ',int(area1)) print(D,' ',E,' ',F,' ',int(area2)) but I don't know how. I've looked at code examples and I can't figure out how. Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor