Re: [Tutor] Help with if-elif-else structure
Hello, I'm quite new on python, so don't hit too hard if I'm wrong ;) A question on the logic... Does this means if roll[0] > 15: if roll[1] >= 2: print("Success") elif roll[2] >= 2: print("Critical failure!") that rolling multiple 1's get the priority on rolling multiple 6's? I mean result > 15 and 2 1's is always a critical success even if multiple 6 are rolled? also if roll[0] > 15: . elif roll[0] <=15: <--- this is redundant. already checked for > 15 so if here is always <= 15 if roll[1] >= 2: you can change with: elif roll[1] >= 2: And... which is the RPG name ? :p Cheers ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String encoding
>Think about it this way... if I gave you a block of data as hex bytes: > >240F91BC03...FF90120078CD45 > >and then asked you whether that was a bitmap image or a sound file or >something else, how could you tell? It's just *bytes*, it could be anything. Yes, but if you give me data and then tell me it is a sound file then I might be able to reverse engineer or reconstruct it. I know what the character does/should look like. I just need the equivalent to the ASCII table for the various encodings; once I have the table I can compare different characters at \311 and see if they are the correct character. I have not been able to find an encoding table (other than ASCII). Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with if-elif-else structure
>if roll[0] > 15: > . >elif roll[0] <=15: <--- this is redundant. already checked for > 15 so if >here is always <= 15 > if roll[1] >= 2: > >you can change with: > >elif roll[1] >= 2: That is not true. The first one looks at index [0], while the second one is index[1]. >>if roll[0] > 15: >> if roll[1] >= 2: >> print("Success") >> elif roll[2] >= 2: >> print("Critical failure!") This will print "Success" if the first roll is greater than 15 AND second roll is 2 or greater. It will print "Critical failure!" if the first roll is greater than 15 AND the second roll is less than 2 AND the third roll is 2 or greater. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 From: tutor-bounces+ramit.prasad=jpmorgan@python.org [mailto:tutor-bounces+ramit.prasad=jpmorgan@python.org] On Behalf Of TheIrda Sent: Friday, August 26, 2011 8:55 AM To: Christopher King Cc: python mail list Subject: Re: [Tutor] Help with if-elif-else structure Hello, I'm quite new on python, so don't hit too hard if I'm wrong ;) A question on the logic... Does this means if roll[0] > 15: if roll[1] >= 2: print("Success") elif roll[2] >= 2: print("Critical failure!") that rolling multiple 1's get the priority on rolling multiple 6's? I mean result > 15 and 2 1's is always a critical success even if multiple 6 are rolled? also if roll[0] > 15: . elif roll[0] <=15: <--- this is redundant. already checked for > 15 so if here is always <= 15 if roll[1] >= 2: you can change with: elif roll[1] >= 2: And... which is the RPG name ? :p Cheers This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String encoding
On Thu, Aug 25, 2011 at 7:07 PM, Prasad, Ramit wrote: > Nice catch! Yeah, I am stuck on the encoding mechanism as well. I know how to > encode/decode...but not what encoding to use. Is there a reference that I can > look up to find what encoding that would correspond to? I know what the > character looks like if that helps. I know that Python does display the > correct character sometimes, but not sure when or why. In this case, the encoding is almost certainly "latin-1". I know that from playing around at the interactive interpreter, like this: >>> s = 'M\xc9XICO' >>> print s.decode('latin-1') MÉXICO If you want to see charts of various encodings, wikipedia has a bunch. For instance, the Latin-1 encoding is here: http://en.wikipedia.org/wiki/ISO/IEC_8859-1 and UTF-8 is here: http://en.wikipedia.org/wiki/Utf-8 As the other respondents have said, it's really hard to figure this out just in code. The chardet module mentioned by Steven D'Aprano is probably the best bet if you really *have* to guess the encoding of an arbitrary sequence of bytes, but it much, much better to actually know the encoding of your inputs. Good luck! -- Jerry ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String encoding
>In this case, the encoding is almost certainly "latin-1". I know that >from playing around at the interactive interpreter, like this: > > >>> s = 'M\xc9XICO' > >>> print s.decode('latin-1') > MÉXICO > >If you want to see charts of various encodings, wikipedia has a bunch. > For instance, the Latin-1 encoding is here: >http://en.wikipedia.org/wiki/ISO/IEC_8859-1 and UTF-8 is here: >http://en.wikipedia.org/wiki/Utf-8 Yep, it is. Thanks those charts are exactly what I wanted! Now I have another question. What is the difference between what print shows and what the interpreter shows? >>> print s.decode('latin-1') MÉXICO >>> s.decode('latin-1') u'M\xc9XICO' >>> print repr(s) 'M\xc9XICO' >>> repr(s) "'M\\xc9XICO'" Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String encoding
Prasad, Ramit wrote: Think about it this way... if I gave you a block of data as hex bytes: 240F91BC03...FF90120078CD45 and then asked you whether that was a bitmap image or a sound file or something else, how could you tell? It's just *bytes*, it could be anything. Yes, but if you give me data and then tell me it is a sound file then I might be able to reverse engineer or reconstruct it. I know what the character does/should look like. I just need the equivalent to the ASCII table for the various encodings; once I have the table I can compare different characters at \311 and see if they are the correct character. I have not been able to find an encoding table (other than ASCII). In practice, you can often guess the encoding by trying the most common ones (such as Latin-1 and UTF-8) and seeing if the strings you get make sense. But note that more than one encoding may give sensible results for a specific string: >>> b = 'M\311XICO' # byte-string >>> print b.decode('latin-1') MÉXICO >>> print b.decode('iso 8859-9') # Turkish MÉXICO So was M\311XICO encoded using the Latin-1 or Turkish encoding, or something else? There is no way to tell. Many encodings overlap. If you have arbitrary byte-strings, and no context to tell what makes sense, then all bets are off. Just because something *can* be decoded doesn't make it meaningful: >>> b = '...\xf7...' >>> print b.decode('macroman') ...˜... >>> print b.decode('latin-1') ...÷... Which is the right encoding to use and which string is intended? So guessing can sometimes work, but guesses can be wrong because encodings overlap. In general, you must know the encoding to be sure. But if you have to guess, try to guess using the largest byte-string that you can. Python 2.7 comes with 108 encodings: http://docs.python.org/library/codecs.html#standard-encodings Since anyone can define their own encoding, there is no upper limit to the number of encodings, and no promise that Python will include them all. There are even two joke encodings, invented for April's Fool Day, that use nine-bit nonets instead of eight-bit octets (bytes): UTF-9 and UTF-18. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String encoding
On 08/26/2011 11:49 AM, Prasad, Ramit wrote: Yep, it is. Thanks those charts are exactly what I wanted! Now I have another question. What is the difference between what print shows and what the interpreter shows? print s.decode('latin-1') MÉXICO The decoded characters are a Unicode string. Python prints that string by encoding it according to whatever sys.stdout is defaulted to. If that matches your actual terminal, then you see it properly. s.decode('latin-1') u'M\xc9XICO' Here, because you don't assign it to anything, the interpreter is printing a repr() of the object. print repr(s) 'M\xc9XICO' Here your code is doing the same thing, but explicitly this time. repr(s) "'M\\xc9XICO'" Here, the repr() is created (which is a string containing single quotes), but then you don't print it, you just leave it. So the interpreter shows you the repr() of that object, enclosing it in double quotes for simplicity. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with if-elif-else structure
Well, I'm not sure what your asking for the first question, but for the second one, you would have an unhandled exception in your code if roll[0] was less than or equal to 0. Hope that helps a bit! On Fri, Aug 26, 2011 at 8:55 AM, TheIrda wrote: > Hello, > > I'm quite new on python, so don't hit too hard if I'm wrong ;) > > > A question on the logic... Does this means > > if roll[0] > 15: >if roll[1] >= 2: >print("Success") >elif roll[2] >= 2: >print("Critical failure!") > > > that rolling multiple 1's get the priority on rolling multiple 6's? I mean > result > 15 and 2 1's is always a critical success even if multiple 6 are > rolled? > > also > > if roll[0] > 15: >. > elif roll[0] <=15: <--- this is redundant. already checked for > 15 so if > here is always <= 15 > if roll[1] >= 2: > > you can change with: > > elif roll[1] >= 2: > > > > And... which is the RPG name ? :p > Cheers > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with if-elif-else structure
Robert Sjoblom wrote: > #assuming target number 15 > roll = (result, initial_mins, initial_max) > if roll[0] > 15: > if roll[1] >= 2: > print("Success") > elif roll[2] >= 2: > print("Critical failure!") > else: > print("Failure.") > elif roll[0] <= 15: > if roll[1] >= 2: > print("Critical success!") > elif roll[2] >= 2: > print("Failure") > else: > print("Success") > > This handles all the test cases I've come up with, but it feels very > ugly. Is there a better way to do this? Sometimes a table-driven approach is preferable. Your code would become something like def show_result(result, initial_mins, initial_max): messages = [ ["Critical success!", "Failure", "Success"], ["Success", "Critical failure!", "Failure."]] messages = messages[result > 15] if initial_mins >= 2: message = messages[0] elif initial_max >= 2: message = messages[1] else: message = messages[2] print(message) or, taken to the extreme, def first(pairs): for predicate, value in pairs: if predicate: return value raise ValueError def show_result(result, initial_mins, initial_max): messages = [ ["Critical success!", "Failure", "Success"], ["Success", "Critical failure!", "Failure."]] def checks(): yield initial_mins >= 2 yield initial_max >= 2 yield True print(first(zip(checks(), messages[result > 15]))) I don't think these alternatives have an advantage over the nested ifs in this case. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor