Re: [Tutor] Read, Write, Split and Append lines from a text file
On 19/07/14 22:27, LN A-go-go wrote: AK 36 AL 39 AR 39 AZ 45 CA 61 CO 54 # and now my newbie attempt: # I can get this far in approach #1 >>> filename = "z:/Geog482/egund919/Program1/BOJ.txt" >>> myfile = open(filename,"r") >>> newfile = "z:/Geog482/egund919/Program1/BOJ_B.txt" >>> mynewfile = open(newfile,"w") >>> while True: line = myfile.readline() print line mynewfile.write(line) if not line: break States OJ AK 36 Assuming this is the output... where does the first line come from? Its not in your data above and you don't print it? So either your data is not the real data or the code is not the real code. Either case makes it difficult for us to work out what is really going on. UT 34 VA 53 VT 67 WA 58 WI 56 WV 43 WY 33 If this is the output what exactly is "wrong" with it? Apart from the first line anomaly it looks exactly like what I'd expect. In approach number 2: trying to append, convert to integer, but the list comes up empty: >>> infile = open('C:/Python27/egund919/Program1/BOJ_B.txt','r') >>> import string Don't import string. Use the string methods instead. >>> state_name = [] >>> data_value = [] >>> counter = 0 >>> x = 0 >>> line = infile.readline() >>> while True: line = infile.readline() if not line: break It would be easier to use enumerate and a for loop: for counter, line in enumerate(infile): counter = counter + 1 States, OJ = string.split(line) XSTATES = str(States) XNUM = int(OJ) By convention all caps means a constant value. These obviously change so it would be better to name them xstates and xnum. Also since there is only one state per line it should be xstate rather than xstates. Just minor style points. state_name.append(XSTATES) data_value.append(XNUM) You could use a dictionary here instead of two lists. states = dict() for counter,line in ... states[xtstate] = xnum >>> type(data_value) >>> print(data_value) [] This suggests your loop exited on the first iteration. Is there a blank line at the start of the file? This is another reason why using a for loop is better, it wouldn't get caught out that way. >>> print (line) All I get is errors from here on out. Tell us what the errors are. If they are actual Python error messages then show us the full error message - it is full of useful data. If they are just errors in the sense that they are not what you expected, then tell us what you expected and show us what you got. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using module Facebook
Dear Danny, thank you for your detailed reply. On 07/19/2014 03:26 AM, Danny Yoo wrote: > This is what piqued my concerns. As you can see from the transcript, > the search shows that there's not a single use of 'iteritems' anywhere > in the facebook-sdk, across the entire history of that project. And > yet the error message you're showing has a use of "args.iteritems()". > Therefore, there's a discrepancy there that needs to be explained. I'm not sure, what the error was. I had also installed another facebook package, not facebook-sdk. Maybe this was the reason. I've deleted the directory, upgraded to Python 2.7 and reinstalled facebook: git clone "https://github.com/pythonforfacebook/facebook-sdk"; pip install facebook-sdk Now, accessing the facebook API is working. -- Christian ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
My apologies Python Gurus for repeating this request for read, write, split and append from a text file in notepad. I worked on it till late last night and will again today. I can't seem to get past trying to write the names to one list and the numbers (converted to integers) to the other. My lists come up empty. I am a full-time firefighter that taught myself to use ArcGIS to make maps and now am in school so I can move into emergency mgmt. I can't seem to teach myself Python and I am really trying. thank-you, LN >>> filename = "C:/Python27/egund919/Program1/BOJ.txt" >>> myfile = open(filename,"r") >>> newfile = "C:/Python27/egund919/Program1/BOJ_B.txt" >>> mynewfile = open(newfile,"w") >>> while True: line = myfile.readline() print line mynewfile.write(line) if not line: break States OJ AK 36 AL 39 AR 39 AZ 45 CA 61 CO 54 CT 61 DC 93 DE 62 FL 51 GA 47 HI 72 IA 54 ID 36 IL 62 IN 50 KS 41 KY 41 LA 40 MA 62 MD 62 ME 58 MI 57 MN 54 MO 49 MS 43 MT 47 NC 50 ND 45 NE 41 NH 54 NJ 57 NM 57 NV 55 NY 62 OH 51 OK 34 OR 57 PA 55 RI 63 SC 45 SD 45 TN 42 TX 44 UT 34 VA 53 VT 67 WA 58 WI 56 WV 43 WY 33 >>> myfile.close() >>> infile = open('C:/Python27/egund919/Program1/BOJ_B.txt','r') >>> import string >>> state_name = [] >>> data_value = [] >>> counter = 0 >>> x = 0 >>> line = infile.readline() >>> while True: line = infile.readline() if not line: break counter = counter + 1 States, OJ = string.split(line) XSTATES = str(States) XNUM = int(OJ) state_name.append(XSTATES) data_value.append(XNUM) >>> len(counter) Traceback (most recent call last): File "", line 1, in len(counter) TypeError: object of type 'int' has no len() >>> counter.count Traceback (most recent call last): File "", line 1, in counter.count AttributeError: 'int' object has no attribute 'count' >>> len(state_name) 0 >>> list.state_name Traceback (most recent call last): File "", line 1, in list.state_name AttributeError: type object 'list' has no attribute 'state_name' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using module Facebook
- On Sun, Jul 20, 2014 6:16 PM CEST Chris wrote: >Dear Danny, > >thank you for your detailed reply. > >On 07/19/2014 03:26 AM, Danny Yoo wrote: >> This is what piqued my concerns. As you can see from the transcript, >> the search shows that there's not a single use of 'iteritems' anywhere >> in the facebook-sdk, across the entire history of that project. And >> yet the error message you're showing has a use of "args.iteritems()". >> Therefore, there's a discrepancy there that needs to be explained. > >I'm not sure, what the error was. I had also installed another facebook >package, not facebook-sdk. Maybe this was the reason. I've deleted the >directory, upgraded to Python 2.7 and reinstalled facebook: > >git clone "https://github.com/pythonforfacebook/facebook-sdk"; >pip install facebook-sdk > >Now, accessing the facebook API is working. Hi, just wondering: why are you cloning the repo? You pip install from Pypi. You can also pip install from the repo. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using module Facebook
> I'm not sure, what the error was. I had also installed another facebook > package, not facebook-sdk. Maybe this was the reason. I would upgrade that "maybe" to an "almost certainly". :P You should have mentioned that detail of installing that second facebook library. It's actually crucial to explaining the bizarre behavior we were seeing. We should revisit the claim made earlier that facebook-sdk doesn't support Python 2.6. In retrospect, it probably does! Let's summarize the situation, since there was some confusion leading up to your solution. 1. You initially had some difficulty because your script was using the system-installed version of Python, rather than the virtualenv-installed one. You corrected the shebang line to refer to the virtualenv-installed version. 2. You installed two different Facebook libraries, and unfortunately one of them uses the same name as the other, effectively shadowing the official one from facebook-sdk. 3. The Facebook library that you were using on accident required Python 2.7. 4. You upgraded to Python 2.7. Effectively, this created a fresh installation with no third-party libraries. 5. You installed only the single Facebook library from facebook-sdk into your Python 2.7 installation. In light of this, reinstalling Python 2.7 might have been slightly overkill. But that being said, it's probably for the best that you're on Python 2.7, since it's the one that the majority of Python 2 users are using now. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On Sun, Jul 20, 2014 at 8:40 AM, LN A-go-go wrote: > My apologies Python Gurus for repeating this request for read, write, split > and append from a text file in notepad. I worked on it till late last night > and will again today. I can't seem to get past trying to write the names to > one list and the numbers (converted to integers) to the other. My lists > come up empty. That's fine, but please also respond to the replies you're getting too. Rather than start a whole new thread, try to continue the thread by using your email client's "Reply to All" feature. Also, feel free to treat this as a conversation: if there's anything about the replies that use terms you don't understand, ask, and we'll try to clarify. Looking closely at your revised program, I see that you've added a file close. Unfortunately, you closed the wrong file. :P Effectively, you've written: myfile = open(filename,"r") mynewfile = open(newfile,"w") ## writing into mynewfile myfile.close() but you needed to have done: myfile = open(filename,"r") mynewfile = open(newfile,"w") ## writing into mynewfile mynewfile.close() It also appears that you are trying to write whole programs at the interpreter loop. This can be inconvenient for larger programs. You might want to use a text editor or IDE to write the program as a single file, and then run Python over that program file. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
> It also appears that you are trying to write whole programs at the > interpreter loop. This can be inconvenient for larger programs. You > might want to use a text editor or IDE to write the program as a > single file, and then run Python over that program file. See: https://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/ for a quick tutorial on using IDLE to write Python programs. The tutorial assumes Python 2, which you appear to be using. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On Sun, Jul 20, 2014 at 8:40 AM, LN A-go-go wrote: > > States OJ > AK 36 > AL 39 > AR 39 > AZ 45 > CA 61 > CO 54 > CT 61 > DC 93 > DE 62 > FL 51 > GA 47 > HI 72 > IA 54 > ID 36 > IL 62 > IN 50 > KS 41 > KY 41 > LA 40 > MA 62 > MD 62 > ME 58 > MI 57 > MN 54 > MO 49 > MS 43 > MT 47 > NC 50 > ND 45 > NE 41 > NH 54 > NJ 57 > NM 57 > NV 55 > NY 62 > OH 51 > OK 34 > OR 57 > PA 55 > RI 63 > SC 45 > SD 45 > TN 42 > TX 44 > UT 34 > VA 53 > VT 67 > WA 58 > WI 56 > WV 43 > WY 33 > > >>> myfile.close() > >>> infile = open('C:/Python27/egund919/Program1/BOJ_B.txt','r') > >>> import string > >>> state_name = [] > >>> data_value = [] > >>> counter = 0 > >>> x = 0 > >>> line = infile.readline() > >>> while True: > line = infile.readline() > if not line: break > counter = counter + 1 > States, OJ = string.split(line) > XSTATES = str(States) > XNUM = int(OJ) > state_name.append(XSTATES) > data_value.append(XNUM) > > >>> len(counter) > Traceback (most recent call last): > File "", line 1, in > len(counter) > TypeError: object of type 'int' has no len() > >>> counter.count > Traceback (most recent call last): > File "", line 1, in > counter.count > AttributeError: 'int' object has no attribute 'count' > >>> len(state_name) > 0 > >>> list.state_name > Traceback (most recent call last): > File "", line 1, in > list.state_name > AttributeError: type object 'list' has no attribute 'state_name' > First of all, I would take advantage of the "with" construction, like so: with open('C:/Python27/egund919/Program1/BOJ_B.txt','r') as infile: #do stuff with infile When the block of code inside of the "with" section finishes, Python will take care of closing the file for you - even if your program encountered an error. MAJOR advantage over doing it yourself. Second, there's no need to import "string" anymore; all of the functions defined in that module are native methods of strings. "line" is a string, so you can just do "line.split()". split() returns a list; you can get the first item in that list with line.split()[0], the second item with line.split()[1], etc. Third, free your mind from the shackles of counting how many objects are in a list and using that counter as the index of your loops - this is not the Python way! Instead, let Python take care of the counting and indexing for you. Fourth (and here I differ from some of the others on this list, notably Alan G) - I don't like the "while True: / break" paradigm. Your mileage may vary, but I find it makes it harder to understand what the program is actually trying to achieve. In your case, you read a line from the file, then check whether it contains anything; I like to move the readline() to the bottom of the loop. Having said all that, here's my suggested version: state_name = [] data_value = [] with open('C:/Python27/egund919/Program1/BOJ_B.txt','r') as infile: line = infile.readline() # Doing this twice because you apparently want to discard the first line...? line = infile.readline() while line: state_name.append(line.split()[0]) data_value.append(line.split()[1]) line = infile.readline() print(state_name) print(data_value) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On 20/07/14 21:37, Marc Tompkins wrote: Fourth (and here I differ from some of the others on this list, notably Alan G) - I don't like the "while True: / break" paradigm. FWIW I don't like it either, but it has become a canonical Python idiom. In fact in this case I suggested he use a for loop to iterate over the file and use a dictionary to store the results... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Is this possible for a newbie?
Am a newbie to programming and I started learning python days ago. I have this idea I want to execute, am a big sport and fantasy fan and I wanted to create something that will make it things a little easy for me. My idea is simple I want to create a program that I can enter name and some information about a player, for example: NFL plays: Jamaal Charles RB : ATT 259 , YDS 1,287 , AVG 5.0, TD 12, REC 70 Tony Romo QB: CMP% 63.9, YDS 3,828 , TD 31, INT 10, Rating 96.7 Dez Bryant WR: REC 93, YDS 1,233, AVG 13.3, LNG 79, TD 13 and other players information. I want to be able to ask the program which QB is best on my list and it give me the names of all the QB on my list, or ask for WR on my list and it gives me all the WR on my list, and also I want to be able to crossed out name simply by typing the players name. What do I need to lean in order to accomplish this? string, variable functions? feel free to ask any questions. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On 20.07.2014 17:40, LN A-go-go wrote: >>> filename = "C:/Python27/egund919/Program1/BOJ.txt" >>> myfile = open(filename,"r") >>> newfile = "C:/Python27/egund919/Program1/BOJ_B.txt" >>> mynewfile = open(newfile,"w") >>> while True: line = myfile.readline() print line mynewfile.write(line) if not line: break States OJ AK 36 AL 39 AR 39 AZ 45 CA 61 .. >>> myfile.close() Closing the file you've read from is good, but not nearly as important as closing the file you've been writing to. >>> infile = open('C:/Python27/egund919/Program1/BOJ_B.txt','r') There's your problem ! You didn't close mynewfile before opening the file again for reading. Most likely, what you thought you've written to the file is still buffered in memory. The main purpose of closing a file after writing is to flush the contents of this buffer to disk. Try mynewfile.close() just before infile = open .. Best, Wolfgang ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On 20.07.2014 22:37, Marc Tompkins wrote: First of all, I would take advantage of the "with" construction, like so: with open('C:/Python27/egund919/Program1/BOJ_B.txt','r') as infile: #do stuff with infile When the block of code inside of the "with" section finishes, Python will take care of closing the file for you - even if your program encountered an error. MAJOR advantage over doing it yourself. Yes, definitely, as you have experienced yourself now. Second, there's no need to import "string" anymore; all of the functions defined in that module are native methods of strings. "line" is a string, so you can just do "line.split()". split() returns a list; you can get the first item in that list with line.split()[0], the second item with line.split()[1], etc. I fully agree on the string method part. As for the use of line.split()[0] and [1], see my comment in the code below. Third, free your mind from the shackles of counting how many objects are in a list and using that counter as the index of your loops - this is not the Python way! Instead, let Python take care of the counting and indexing for you. True (see the earlier suggestion of using enumerate()), but I'm sure you'll learn this over time. Fourth (and here I differ from some of the others on this list, notably Alan G) - I don't like the "while True: / break" paradigm. Your mileage may vary, but I find it makes it harder to understand what the program is actually trying to achieve. In your case, you read a line from the file, then check whether it contains anything; I like to move the readline() to the bottom of the loop. In my opinion, this is less readable than the original code. I know that some people (usually coming from C or other languages with more expressive while loops) don't like it, but there's nothing wrong, in general, with using while True with break. It's perfectly pythonic. Direct use of the file object in a for loop has been suggested already and THAT's more readable. Having said all that, here's my suggested version: state_name = [] data_value = [] with open('C:/Python27/egund919/Program1/BOJ_B.txt','r') as infile: line = infile.readline() # Doing this twice because you apparently want to discard the first line...? line = infile.readline() while line: state_name.append(line.split()[0]) data_value.append(line.split()[1]) Rewriting the original States, OJ = string.split(line) as states, oj = line.split() is better (and it's remarkable that you used it as a beginner). This line unpacks the elements of the list returned by the split method into the two variables on the left side. Besides avoiding repetitive calls to line.split(), it may also help you discover problems with your input file format. If there are more than two elements in the list, you'll get an error, so you'll know there was something in the file you didn't expect. With the line.split()[0] / [1] approach, potential other elements in the list (i.e. unexpected line content) will be ignored silently. Once you've learnt that errors in Python are nothing evil, but there to help you and once you've read about try/except to catch them, you'll appreciate the advantage of the first version. line = infile.readline() I don't see why repeating infile.readline() three times should be more elegant than a nice and clean while True / break. print(state_name) print(data_value) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is this possible for a newbie?
On 20/07/14 21:57, keith papa wrote: me. My idea is simple I want to create a program that I can enter name and some information about a player, for example: NFL plays: Jamaal Charles RB : ATT 259 , YDS 1,287 , AVG 5.0, TD 12, REC 70 Tony Romo QB: CMP% 63.9, YDS 3,828 , TD 31, INT 10, Rating 96.7 Dez Bryant WR: REC 93, YDS 1,233, AVG 13.3, LNG 79, TD 13 and other players information. Thus far its easy. I want to be able to ask the program whichQB is best on my list and it give me the names of all the QB on my list, Really? I'd have thought you wanted only the 'best' one - however you define best... or ask for WR on my list and it gives me all the WR on my list, This is easier and also I want to be able to crossed out name simply by typing the players name. I'm not sure what crossed out means in this contexzt? Is that a technical term in US football(?) or do you simply mean you want to delete the entry? What do I need to lean in order to accomplish this? string, variable functions? You would be best working througfh a general purpose introduction. Can you program already in another language or are you new to programming as well as Python? Either way there are tutorials on the Python web site that can help. Once you have completed one of these your project should be straightforward. But trying to learn only the minimum needed for your project will in variable lead to frustration as you keep finding new things you need to learn, but not in any logical order. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On Sun, Jul 20, 2014 at 2:37 PM, Alan Gauld wrote: > In fact in this case I suggested he use a for loop to iterate over > the file and use a dictionary to store the results... Ah. I missed that, as I've only noticed this newer thread. And I apologize for imputing motive (a liking for "while True"); I'd just noticed that you often advise it. I don't know who _does_ think this is a desirable pattern; I'd love to hear their argument for it - it must be really good. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On 21/07/2014 01:34, Marc Tompkins wrote: On Sun, Jul 20, 2014 at 2:37 PM, Alan Gauld wrote: In fact in this case I suggested he use a for loop to iterate over the file and use a dictionary to store the results... Ah. I missed that, as I've only noticed this newer thread. And I apologize for imputing motive (a liking for "while True"); I'd just noticed that you often advise it. I don't know who _does_ think this is a desirable pattern; I'd love to hear their argument for it - it must be really good. It works :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is this possible for a newbie?
On Sun, Jul 20, 2014 at 04:57:24PM -0400, keith papa wrote: > Am a newbie to programming and I started learning python days ago. I > have this idea I want to execute, am a big sport and fantasy fan and I > wanted to create something that will make it things a little easy for > me. My idea is simple I want to create a program that I can enter name > and some information about a player, for example: [...] > I want to be able to ask the program which QB is best on my list and > it give me the names of all the QB on my list, or ask for WR on my > list and it gives me all the WR on my list, and also I want to be able > to crossed out name simply by typing the players name. What do I need > to lean in order to accomplish this? string, variable functions? feel > free to ask any questions. This sounds more like something for a database than for Python itself. You can write Python code to talk to the database, and Python has excellent libraries for a simple database, such as sqlite. But if you're on a Linux system and can install Postgres, I'd just use that directly and not worry about Python at all. https://wiki.python.org/moin/DatabaseInterfaces https://docs.python.org/2/library/persistence.html http://www.postgresql.org/docs/7.4/static/tutorial.html -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On Sun, Jul 20, 2014 at 6:12 PM, Mark Lawrence wrote: >> Ah. I missed that, as I've only noticed this newer thread. And I >> apologize for imputing motive (a liking for "while True"); I'd just >> noticed that you often advise it. I don't know who _does_ think this >> is a desirable pattern; I'd love to hear their argument for it - it >> must be really good. >> > > It works :) So does GOTO. Seriously, though, how is 1) Do {this} forever, until something happens that I'll tell you about later better than 2) Do {this} until this condition, which I'm telling you about RIGHT NOW, changes ? Granted, this can all be clarified with good comments - but the second approach basically documents itself. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On 21/07/2014 03:05, Marc Tompkins wrote: On Sun, Jul 20, 2014 at 6:12 PM, Mark Lawrence wrote: Ah. I missed that, as I've only noticed this newer thread. And I apologize for imputing motive (a liking for "while True"); I'd just noticed that you often advise it. I don't know who _does_ think this is a desirable pattern; I'd love to hear their argument for it - it must be really good. It works :) So does GOTO. What is the problem with GOTO? GOTO is used extremely effectively throughout the Python C source code. I've yet to hear of anybody say never use it. Seriously, though, how is 1) Do {this} forever, until something happens that I'll tell you about later better than 2) Do {this} until this condition, which I'm telling you about RIGHT NOW, changes ? Granted, this can all be clarified with good comments - but the second approach basically documents itself. So what, to me this is a trivial issue compared to many other problems that can easily be solved in Python that can only be solved in other languages by jumping through hoops, with the Factory pattern springing instantly to my mind. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is this possible for a newbie?
>> Am a newbie to programming and I started learning python days ago. I >> have this idea I want to execute, am a big sport and fantasy fan and I >> wanted to create something that will make it things a little easy for >> me. My idea is simple I want to create a program that I can enter name >> and some information about a player, for example: > [...] > This sounds more like something for a database than for Python itself. I agree with Steven; this does sound like a database storage and query problem. You can use a general-purpose language like Python to tackle this problem. But you may have an easier time if you use a database-oriented language like SQL to handle the data management. There's an SQL tutorial in: http://sql.learncodethehardway.org/ that might be helpful if you're not already familiar with SQL. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Ending a loop with a condition not at the top
Marc Tompkins writes: > And I apologize for imputing motive (a liking for "while True"); I'd > just noticed that you often advise it. I don't know who _does_ think > this is a desirable pattern; I'd love to hear their argument for it - > it must be really good. It works better than alternatives already available in the language; it is simpler than alternatives already available in the language. It isn't particularly ugly (nor is it particularly beautiful, I'll grant you). And it works well enough, and is simple enough, to be recommended without caveat to anyone needing a loop to end somewhere other than the top. Any alternative structure would need to be *significantly* better than what already works now. That, in the end, is the strongest argument for ‘while True: … break’. -- \ “When [science] permits us to see the far side of some new | `\ horizon, we remember those who prepared the way – seeing for | _o__) them also.” —Carl Sagan, _Cosmos_, 1980 | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Ending a loop with a condition not at the top
Marc Tompkins writes: > Seriously, though, how is > 1) Do {this} forever, until something happens that I'll tell you about > later > > better than > 2) Do {this} until this condition, which I'm telling you about RIGHT > NOW, changes > ? Here's how: The first of them is already implemented in Python, and works well enough for the purpose. This is a significant benefit the other lacks. The second one is not implemented in Python, and there are no successful proposals (which, by the way, are better discussed at the Python Ideas forum https://mail.python.org/mailman/listinfo/python-ideas>) to implement them in a way that justifies the cost of adding it to the language. What are you asking for? If you want someone to agree that a ‘while True: … break’ is not as aesthetically pleasing as it might be ideally: yes, I agree. It is not perfect. Take that confirmation and be content. If, on the other hand, you are asking – as your words imply – for the currest state of affairs to be justified: you already have that. The justification is: it works *now*, well enough that any alternative so far proposed is not sufficiently better to displace it within Python. If you want to propose an improvement to Python, feel free to take it up at the Python Ideas forum. But do your research first on what a change to the language would mean – what you're thereby asking the Python developers and community to accept – and the burden such proposals need to meet in order to be accepted. -- \ “I got some new underwear the other day. Well, new to me.” —Emo | `\ Philips | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is this possible for a newbie?
On 2014-07-21, Steven D'Aprano wrote: > On Sun, Jul 20, 2014 at 04:57:24PM -0400, keith papa wrote: > >> Am a newbie to programming and I started learning python days ago. I > > This sounds more like something for a database than for Python itself. > > You can write Python code to talk to the database, and Python has > excellent libraries for a simple database, such as sqlite. I'd agree up to this point... > But if you're on a Linux system and can install Postgres, I'd just use that > directly But this...? Why in the name of Pete would you suggest a newbie setup something like postgresql for something this small/simple? Yes, sqlite has its limitations but I don't think the OP is likely to run into them any time soon on a project like this. Monte ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Ending a loop with a condition not at the top
On Sun, Jul 20, 2014 at 8:10 PM, Ben Finney wrote: > Marc Tompkins writes: > >> Seriously, though, how is >> 1) Do {this} forever, until something happens that I'll tell you about >> later >> >> better than >> 2) Do {this} until this condition, which I'm telling you about RIGHT >> NOW, changes >> ? > > Here's how: > > The first of them is already implemented in Python, and works well > enough for the purpose. This is a significant benefit the other lacks. > > The second one is not implemented in Python, and there are no successful > proposals (which, by the way, are better discussed at the Python Ideas > forum https://mail.python.org/mailman/listinfo/python-ideas>) to > implement them in a way that justifies the cost of adding it to the > language. Actually, if you read the Python docs - 8.2 for Python 3: https://docs.python.org/3/reference/compound_stmts.html#the-while-statement or 7.2 for Python 2: https://docs.python.org/2/reference/compound_stmts.html#the-while-statement you'll see that it _is_ implemented: > The while statement is used for repeated execution as long as an expression > is true: > while_stmt ::= "while" expression ":" suite > ["else" ":" suite] > This repeatedly tests the expression and, if it is true, executes the first > suite; > if the expression is false (which may be the first time it is tested) the > suite of the else clause, if present, is executed and the loop terminates. So I don't know where you got the idea that I wanted to change the language. However, I will admit that, until a few minutes ago, I had not read this: https://docs.python.org/2/faq/design.html#why-can-t-i-use-an-assignment-in-an-expression which deals with my preferred "while" syntax as an edge case: > There’s an alternative way of spelling this that seems attractive but is > generally less robust than the “while True” solution: One might ask whether the designer(s) of the language are on speaking terms with whoever wrote that paragraph - but I bow my head to Guido's (or whoever's) wisdom. In any case, I wholeheartedly agree with the next paragraph, which states: > The best approach is to use iterators, making it possible to loop through > objects using the for statement. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor