[Tutor] While loop issue, variable not equal to var or var
Hey guys n gals, New to python, having some problems with while loops, I would like to make a program quick once q or Q is typed, but thus far I can only get the first variable to be recognized. My code looks like: message = raw_input(- ) while message != 'q': s.send(message) data = s.recv(2048) print str(data) message = raw_input(- ) s.close() print(Shutting Down) I've tried: while message != 'q' or 'Q': while message != 'q' or message != 'Q': while message != ('q' or 'Q'): Any ideas would be much appreciated! Thanks! :D Regards, Steve Rodriguez ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While loop issue, variable not equal to var or var
Steve Rodriguez wrote: Hey guys n gals, New to python, having some problems with while loops, I would like to make a program quick once q or Q is typed, but thus far I can only get the first variable to be recognized. My code looks like: message = raw_input(- ) while message != 'q': s.send(message) data = s.recv(2048) print str(data) message = raw_input(- ) s.close() print(Shutting Down) I've tried: while message != 'q' or 'Q': This evaluates (message != q) or Q and Q is always True in a boolean context: bool(Q) True while message != 'q' or message != 'Q': This is true when at least one of the subexpressions message != q message != Q is True. When the value of message is q it must be unequal to Q and vice versa, so there is always at least one true subexpression. while message != ('q' or 'Q'): When you try the right side in the interactive interpreter you get q or Q 'q' so this is the same as just message != q Any ideas would be much appreciated! Thanks! :D while message.lower() != q: while message not in (q, Q): while message != q and message != Q: There's another one that chains boolean expressions while q != message != Q: that I don't recommend here but that is very readable for checking number intervals: if 0 = some_number 1: print some_number, is in the half-open interval [0,1) You also may consider an infinite loop: while True: message = raw_input(- ) if message in (q, Q): break s.send(message) ... This avoids the duplicate raw_input(). PS: You sometimes see message in qQ but this is buggy as it is true when the message is either q, Q, or qQ. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While loop issue, variable not equal to var or var
Peter Otten wrote: PS: You sometimes see message in qQ but this is buggy as it is true when the message is either q, Q, or qQ. Oops, I forgot . ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While loop issue, variable not equal to var or var
On 11/07/14 22:16, Steve Rodriguez wrote: Hey guys n gals, New to python, having some problems with while loops, I would like to make a program quick once q or Q is typed, but thus far I can only get the first variable to be recognized. My code looks like: message = raw_input(- ) while message != 'q': Peter has given a comprehensive reply. There are two other options he didn't mention. 1) Use 'and' instead of 'or': while message != 'q' and message != 'Q': 2) Better (IMHO) is to convert message to lower case (or upper if you prefer) and only do one comparison: while message.lower() != 'q': HTH, -- 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] While loop issue, variable not equal to var or var
On Sat, Jul 12, 2014 at 09:33:20AM +0100, Alan Gauld wrote: 2) Better (IMHO) is to convert message to lower case (or upper if you prefer) and only do one comparison: while message.lower() != 'q': I second this advice, but with a slight modification. If you're using Python 3.3 or higher, it is better to use message.casefold rather than lower. For English, there's no real difference: py Hello World!.casefold() 'hello world!' but it can make a difference for non-English languages: py Große.lower() # German for great or large 'große' py Große.casefold() 'grosse' -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While loop issue, variable not equal to var or var
On 12/07/14 10:28, Steven D'Aprano wrote: If you're using Python 3.3 or higher, it is better to use message.casefold rather than lower. For English, there's no real difference: ... but it can make a difference for non-English languages: py Große.lower() # German for great or large 'große' py Große.casefold() 'grosse' You learn something new etc... But I'm trying to figure out what difference this makes in practice? If you were targeting a German audience wouldn't you just test against the German alphabet? After all you still have to expect 'grosse' which isn't English, so if you know to expect grosse why not just test against große instead? I think I'm missing something. -- 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] While loop issue, variable not equal to var or var
On Sat, Jul 12, 2014 at 11:27:17AM +0100, Alan Gauld wrote: On 12/07/14 10:28, Steven D'Aprano wrote: If you're using Python 3.3 or higher, it is better to use message.casefold rather than lower. For English, there's no real difference: ... but it can make a difference for non-English languages: py Große.lower() # German for great or large 'große' py Große.casefold() 'grosse' You learn something new etc... But I'm trying to figure out what difference this makes in practice? If you were targeting a German audience wouldn't you just test against the German alphabet? After all you still have to expect 'grosse' which isn't English, so if you know to expect grosse why not just test against große instead? Because the person might have typed any of: grosse GROSSE gROSSE große Große GROßE GROẞE etc., and you want to accept them all, just like in English you'd want to accept any of GREAT great gREAT Great gReAt etc. Hence you want to fold everything to a single, known, canonical version. Case-fold will do that, while lowercasing won't. (The last example includes a character which might not be visible to many people, since it is quite unusual and not supported by many fonts yet. If it looks like a box or empty space for you, it is supposed to be capital sharp-s, matching the small sharp-s ß.) Oh, here's another example of the difference, this one from Greek: py 'Σσς'.lower() # three versions of sigma 'σσς' py 'Σσς'.upper() 'ΣΣΣ' py 'Σσς'.casefold() 'σσσ' I suspect that there probably aren't a large number of languages where casefold and lower do something different, since most languages don't have distinguish between upper and lower case at all. But there's no harm in using it, since at worst it returns the same as lower(). -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While loop issue, variable not equal to var or var
Steven D'Aprano st...@pearwood.info Wrote in message: On Sat, Jul 12, 2014 at 09:33:20AM +0100, Alan Gauld wrote: 2) Better (IMHO) is to convert message to lower case (or upper if you prefer) and only do one comparison: while message.lower() != 'q': I second this advice, but with a slight modification. If you're using Python 3.3 or higher, it is better to use message.casefold rather than lower. For English, there's no real difference: py Hello World!.casefold() 'hello world!' but it can make a difference for non-English languages: py Große.lower() # German for great or large 'große' py Große.casefold() 'grosse' I don't remember my high school German enough to remember if the ß character is an example, but in various languages there are characters that exist only in uppercase, and whose lowercase equivalent is multiple letters. Or vice versa. And characters that have multiple valid spellings in uppercase, but only one in lowercase. If the latter is true for German, perhaps GROSSE and GROßE are valid uppercase, but only grosse for lowercase. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While loop issue, variable not equal to var or var
Hi Steve, In your conditionals: … while message != 'q' or 'Q'/message != “q” or message != “Q”: … Python will only match the first variable. A better approach (which might be a good solution) would be capturing the exit commands in a list like this: JL’s code: while message not in [“q”, “Q”]: # blah, blah, your code… # code end Effectively, we’re testing if the command is a member of our exit commands list, and if it is, we’ll fall off the loop. Try this solution and see if it works for you. Cheers, Joseph ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While loop issue, variable not equal to var or var
Thank you guys! Works perfectly! :D Regards, Steve Rodriguez On Sat, Jul 12, 2014 at 1:21 AM, Peter Otten __pete...@web.de wrote: Peter Otten wrote: PS: You sometimes see message in qQ but this is buggy as it is true when the message is either q, Q, or qQ. Oops, I forgot . ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While loop issue, variable not equal to var or var
On 12.07.2014 14:19, Steven D'Aprano wrote: On Sat, Jul 12, 2014 at 11:27:17AM +0100, Alan Gauld wrote: On 12/07/14 10:28, Steven D'Aprano wrote: If you're using Python 3.3 or higher, it is better to use message.casefold rather than lower. For English, there's no real difference: ... but it can make a difference for non-English languages: py Große.lower() # German for great or large 'große' py Große.casefold() 'grosse' You learn something new etc... But I'm trying to figure out what difference this makes in practice? If you were targeting a German audience wouldn't you just test against the German alphabet? After all you still have to expect 'grosse' which isn't English, so if you know to expect grosse why not just test against große instead? Because the person might have typed any of: grosse GROSSE gROSSE große Große GROßE GROẞE etc., and you want to accept them all, just like in English you'd want to accept any of GREAT great gREAT Great gReAt etc. Hence you want to fold everything to a single, known, canonical version. Case-fold will do that, while lowercasing won't. (The last example includes a character which might not be visible to many people, since it is quite unusual and not supported by many fonts yet. If it looks like a box or empty space for you, it is supposed to be capital sharp-s, matching the small sharp-s ß.) Very interesting advice. Wasn't aware at all of this feature of casefold. As a native German speaker, I have to say that your last two examples involving the capital ß are pretty contrived: although the capital ß is part of unicode, it is not an official part of the German alphabet and nobody is using it (in fact, I had to look it up in Wikipedia now to learn what that letter is). An even better example than the rest of yours would be Kuß (German for the noun kiss), which only people above 30 (like me) still spell this way, but younger people spell Kuss since the official rules have changed over the last 10 years. In this particular case, you should definitely be prepared to handle Kuss and Kuß as legal input. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While loop issue, variable not equal to var or var
On 12.07.2014 14:20, Dave Angel wrote: I don't remember my high school German enough to remember if the ß character is an example, but in various languages there are characters that exist only in uppercase, and whose lowercase equivalent is multiple letters. Or vice versa. And characters that have multiple valid spellings in uppercase, but only one in lowercase. If the latter is true for German, perhaps GROSSE and GROßE are valid uppercase, but only grosse for lowercase. No, that's not the case. Only große is valid lowercase, but many people got so used to computers not dealing with ß correctly that they'd type grosse automatically. Conversely, since there is no official capital form of ß (see my reply to Steven), GROSSE is standard uppercase although you might encounter GROßE occasionally. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While loop issue, variable not equal to var or var
On 12/07/14 13:19, Steven D'Aprano wrote: Because the person might have typed any of: grosse große etc., and you want to accept them all, just like in English The bit I was missing was that a German user might use the ss version instead the ß so testing for either of them alone is insufficient. lower() or casefold() would deal with the mixed case variations, but lower() would not fix the ss v ß issues. Thanks, -- 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] While loop issue, variable not equal to var or var
On Sat, Jul 12, 2014 at 02:39:12PM +0200, Wolfgang Maier wrote: [...] Very interesting advice. Wasn't aware at all of this feature of casefold. As a native German speaker, I have to say that your last two examples involving the capital ß are pretty contrived: although the capital ß is part of unicode, it is not an official part of the German alphabet and nobody is using it (in fact, I had to look it up in Wikipedia now to learn what that letter is). Interestingly, although capital ß is not official, it used to be a lot more common than it is now, and never quite disappeared: http://opentype.info/blog/2011/01/24/capital-sharp-s/ Now that the common fonts provided on Windows support the capital sharp s, I wouldn't be surprised if it starts to come back into vogue. Typesetters will be at the forefront, since they care about the look of things: http://www.glyphsapp.com/tutorials/localize-your-font-german-capital-sharp-s An even better example than the rest of yours would be Kuß (German for the noun kiss), which only people above 30 (like me) still spell this way, but younger people spell Kuss since the official rules have changed over the last 10 years. In this particular case, you should definitely be prepared to handle Kuss and Kuß as legal input. Good example! Thank you! -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor