Re: [Tutor] why do i keep getting syntax errors in python when this runs
!-- On Fri 6.Sep'13 at 5:27:23 BST, mike johnson (pretor...@hotmail.com), wrote: > can you please help me figure out why this isnt working thanks > # convert.py > # this program is used to convert Celsius temps to Fahrenheit > # By: James Michael Johnson > > Def main (): > Celsius = float (input ("What is the Celsius temperature? ")) > Fahrenheit = 9.0 / 5.0 * Celsius + 32 > Print ("The temperature is ", Fahrenheit, " degrees Fahrenheit.") > > > Main () [ ...] > # convert.py > # this program is used to convert Celsius temps to Fahrenheit > # By: James Michael Johnson > > Def main (): > Celsius = float (input ("What is the Celsius temperature? ")) > Fahrenheit = 9.0 / 5.0 * Celsius + 32 > Print ("The temperature is ", Fahrenheit, " degrees Fahrenheit.") > > > Main () I'm sure this is homework. I had a peice of homework in my first year Undergrad exactly like this. -- James Griffin: jmz at kontrol.kode5.net A4B9 E875 A18C 6E11 F46D B788 BEE6 1251 1D31 DC38 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Is Python a scripting language
Hi all, I just wanted to know what exactly are scripting language used for and if python is one such language. Can it be used in place of say, PHP? I'm asking because I have a project in my 1st sem to design a website using PHP and since I already know a good deal of python and nothing of PHP, I was wondering if I could use python instead. Thanks Jatin P.S I'm the guy who started the project euler conversation with the question :-) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is Python a scripting language
On 07/09/13 08:18, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote: I just wanted to know what exactly are scripting language used for Your best bet is to read what wikipedia has to say on the subject. The whole question of "scripting languages" has become clouded in recent times and the definition is open to some interpretation. and if python is one such language. Python can be used as a scripting language but in my personal definition of the term Python is more than a scripting language it is a purpose programming language which happens to be interpreted rather than compiled. (and even that is only partly true). Can it be used in place of say, PHP? Yes. I'm asking because I have a project in my 1st sem to design a website using PHP and since I already know a good deal of python and nothing of PHP, I was wondering if I could use python instead. If the class is in PHP then no, you will likely fail if you use a different language from that assigned. If the class is in web design then yes, you probably can, but you will need to pick a suitable web framework (and Python has many). Something like Pylons or CherryPy are candidates. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why do i keep getting syntax errors in python when this runs
def main (): Celsius = float (input ("What is the Celsius temperature? ")) Fahrenheit = 9.0 / 5.0 * Celsius + 32 print("The temperature is ", Fahrenheit, " degrees Fahrenheit.") main() You need to check your capitalisation on all the basic things for a start # def main() Def? # then you call it with Main()? On 7 September 2013 17:09, James Griffin wrote: > !-- On Fri 6.Sep'13 at 5:27:23 BST, mike johnson (pretor...@hotmail.com), > wrote: > > can you please help me figure out why this isnt working thanks > > # convert.py > > # this program is used to convert Celsius temps to Fahrenheit > > # By: James Michael Johnson > > > > Def main (): > > Celsius = float (input ("What is the Celsius temperature? ")) > > Fahrenheit = 9.0 / 5.0 * Celsius + 32 > > Print ("The temperature is ", Fahrenheit, " degrees Fahrenheit.") > > > > > > Main () > > [ ...] > > > # convert.py > > # this program is used to convert Celsius temps to Fahrenheit > > # By: James Michael Johnson > > > > Def main (): > > Celsius = float (input ("What is the Celsius temperature? ")) > > Fahrenheit = 9.0 / 5.0 * Celsius + 32 > > Print ("The temperature is ", Fahrenheit, " degrees Fahrenheit.") > > > > > > Main () > > I'm sure this is homework. I had a peice of homework in my first year > Undergrad exactly like this. > > -- > > > James Griffin: jmz at kontrol.kode5.net > > A4B9 E875 A18C 6E11 F46D B788 BEE6 1251 1D31 DC38 > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Luke Pettit ,,, ^..^,,, http://lukepettit-3d.blogspot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is Python a scripting language
On 07/09/13 09:13, Alan Gauld wrote: of the term Python is more than a scripting language it is a purpose programming language ... Oops, that should be *general purpose* -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Polymorphic function in Python 2 & 3?
Hi, I have a class and I want it's initializer to be able to take both byte strings (python 3: byte objects) and unicode strings (python 3: strings). So it's foward compatible Python 2 code (or backward compatible Python 3 code, if you like). If needed, the arguments of __init__ are converted into bytes using a function called encode(). I pasted the code that I wrote here: http://pastebin.com/2WBQ0H87. Sorry if it's a little long. It works for Python 2.7 and 3.3. But is this the best way to do this? In particular, is inspect.getargs the best way to get the argument names? (I don't want to use **kwargs). Also, I am using setattr to set the parameters, so the encode() method has side effects, which may not be desirable. I need bytes because I am working with binary data. Thank you in advance! Regards, Albert-Jan ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Polymorphic function in Python 2 & 3?
On 7/9/2013 15:45, Albert-Jan Roskam wrote: > Hi, > > I have a class and I want it's initializer to be able to take both byte > strings (python 3: byte objects) and unicode strings (python 3: strings). So > it's foward compatible Python 2 code (or backward compatible Python 3 code, > if you like). If needed, the arguments of __init__ are converted into bytes > using a function called encode(). I pasted the code that I wrote here: > http://pastebin.com/2WBQ0H87. Sorry if it's a little long. It works for > Python 2.7 and 3.3. But is this the best way to do this? In particular, is > inspect.getargs the best way to get the argument names? (I don't want to use > **kwargs). Also, I am using setattr to set the parameters, so the encode() > method has side effects, which may not be desirable. I need bytes because I > am working with binary data. > Seems to me you went way around the barn, just to avoid the **kwargs (and *args) construct. Is this an assignment where the teacher specified extra constraints, or is it a self-challenge, or are you just trying to learn more about introspection? Perhaps you were trying to make sure the caller never uses any keyword arguments other than those 5? Your sample top-level call never uses keyword arguments at all, so if that was your goal, you could have used *args, instead of kwargs. At the same time, you allow several specific types of data in each argument. You don't permit list of dicts, or dict of dict of dict, or many other variants that could have been done. So it's not as general as it might have been, with a little more work. You give no indication how the user of this library would use the data stored in the object. But presently he has to use the names a, b, ... instead of the more natural numeric subscripting if you had just accepted a *args. And finally, you say that your data is binary. But valid utf-8 byte strings are a tiny subset of possible binary byte strings. So if some data is being converted from unicode strings using utf-8, and other data is binary byte strings, it seems the result might be confusing and error prone. If I were mixing binary byte strings and encoded unicode strings, I'd want to store flags with each. And what better way to do that than just not to convert from unicode at all. The strict typing of Python handles it nicely. Presumably you have some real goal which makes all these points moot. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] cs student needs help import math
I am writing a simple program based off an ipo chart that I did correctly. I need to use ceil but I keep getting an error saying ceil is not defined. I did import math, I think. I am using 3.2.3 and I imported this way... >>> import math >>> math.pi 3.141592653589793 >>> math.ceil(math.pi) 4 >>> math.floor(math.pi) 3 ... but I get the error when using ceil... pepsticks = ceil(peplength / StickLength) Traceback (most recent call last): File "", line 1, in pepsticks = ceil(peplength / StickLength) NameError: name 'ceil' is not defined Thanks for the help! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cs student needs help import math
On 2013-09-07 17:02, Byron Ruffin wrote: > I am writing a simple program based off an ipo chart that I did correctly. > I need to use ceil but I keep getting an error saying ceil is not defined. > I did import math, I think. I am using 3.2.3 and I imported this way... > > >>> import math > >>> math.pi > 3.141592653589793 > >>> math.ceil(math.pi) > 4 > >>> math.floor(math.pi) > 3 > > ... but I get the error when using ceil... > > pepsticks = ceil(peplength / StickLength) > Traceback (most recent call last): > File "", line 1, in > pepsticks = ceil(peplength / StickLength) > NameError: name 'ceil' is not defined The error message is pretty clear, "ceil" is not defined. If you started by using "import math", this is expected, because you need to explicitly call "math.ceil", not just "ceil". If you want to import ceil into your current namespace, you need to use "from": >>> from math import ceil >>> ceil(3.14) 4 pgp0TCLXzzsD9.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cs student needs help import math
On Sun, Sep 8, 2013 at 8:02 AM, Byron Ruffin wrote: > I am writing a simple program based off an ipo chart that I did correctly. > I need to use ceil but I keep getting an error saying ceil is not defined. > I did import math, I think. I am using 3.2.3 and I imported this way... > import math math.pi > 3.141592653589793 math.ceil(math.pi) > 4 math.floor(math.pi) > 3 > > ... but I get the error when using ceil... > > pepsticks = ceil(peplength / StickLength) > Traceback (most recent call last): > File "", line 1, in > pepsticks = ceil(peplength / StickLength) > NameError: name 'ceil' is not defined So, like you see earlier, you used math.ceil() to refer to the ceil() function. That is how you refer to a function defined in a module. If you want to refer to it as ceil(), you have to import it like so: from math import ceil Best, Amit. -- http://echorand.me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cs student needs help import math
On Sep 7, 2013, at 6:02 PM, Byron Ruffin wrote: > > >>> math.ceil(math.pi) > 4 > ... but I get the error when using ceil... > > pepsticks = ceil(peplength / StickLength) > Traceback (most recent call last): > File "", line 1, in > pepsticks = ceil(peplength / StickLength) > NameError: name 'ceil' is not defined Look carefully again at that error. Basically, it's telling you that 'ceil' is not the same as 'math.ceil' which is how you used it correctly the first time. That's an easy mistake to make. Take care, Don ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Polymorphic function in Python 2 & 3?
On Sat, Sep 07, 2013 at 12:45:02PM -0700, Albert-Jan Roskam wrote: > Hi, > > I have a class and I want it's initializer to be able to take both > byte strings (python 3: byte objects) and unicode strings (python 3: > strings). [...] I need bytes because I am > working with binary data. Consider only accepting binary data. It is not very difficult for the caller to explicitly convert their text strings into binary data ahead of time, and besides, "convert text to binary" is ambiguous. As the Zen of Python says, resist the temptation to guess. Consider the *text* string "abcdef". Which of the following binary data (shown in hex) does it represent? Six values, limited between 0 and FF? 1) 61 62 63 64 65 66 2) 81 82 83 84 85 86 # Hint: IBM mainframe users might expect this. Six values, limited between 0 and ? 3) 6100 6200 6300 6400 6500 6600 4) 0061 0062 0063 0064 0065 0066 Twelve values between 0 and FF? 5) 61 00 62 00 63 00 64 00 65 00 66 00 6) 00 61 00 62 00 63 00 64 00 65 00 66 Three values between 0 and FF? 7) AB CD EF Something else? 8) ValueError: expected decimal digits but got "abcdef" Even assuming that you are expecting single byte data, not double bytes, there are still six legitimate ways to convert this string. It seems risky to assume that if the caller passes you "▼□■" they actually meant E2 96 BC E2 96 A1 E2 96 A0. If I were designing this, I'd probably take the rule: - byte strings are accepted by numeric value, e.g. b'a' -> hex 61 - text strings are expected to be pairs of hex digits, e.g. u'a' is an error, u'abcdef' -> hex AB, CD EF, u'hello' is an error. That seems more useful to me than UTF-8. > So it's foward compatible Python 2 code (or backward > compatible Python 3 code, if you like). If needed, the arguments of > __init__ are converted into bytes using a function called encode(). I > pasted the code that I wrote here: http://pastebin.com/2WBQ0H87. Sorry > if it's a little long. It works for Python 2.7 and 3.3. But is this > the best way to do this? In particular, is inspect.getargs the best > way to get the argument names? (I don't want to use **kwargs). Also, I > am using setattr to set the parameters, so the encode() method has > side effects, which may not be desirable. Some questions/observations: * Why do you bother setting attributes a, b, ... e only to then set them again in the encode method? * It isn't clear to me what the encode method is supposed to do, which suggests it is trying to do too much. The doc string says: Params can be bytes, str, unicode, dict, dict of dics, list of str/bytes/unicode but it isn't clear what will happen if you pass these different values to encode. For instance, if params = {1: None, 2: None}, what do you expect to happen? How about {None: 42} or ['a']? My *guess* is that it is actually expecting a list of (attribute name, string value) pairs, that at least is how you seem to be using it, but that documentation gives me no help here. I think a much better approach would be to use a helper function: def to_bytes(string): if isinstance(string, unicode): return string.encode('uft-8') # But see above, why UTF-8? elif isinstance(string, bytes): return string raise TypeError class Test: def __init__(self, a, b, *args): self.a = to_bytes(a) try: self.b = to_bytes(b) except TypeError: self.b = None self.extras = [to_bytes(s) for s in args] Short, sweet, easy to understand, efficient, requires no self-inspection magic, doesn't try to guess the caller's intention, easily useable without worrying about side-effects, which means it is easy to test. Some other observations... I dislike the fact that on UnicodeDecodeError you assume that the dodgy bytes given must be encoded in the default encoding: except UnicodeDecodeError: # str, python 2 cp = locale.getdefaultlocale()[-1].lower() if cp != "utf-8": return arg.decode(cp).encode("utf-8") return arg I think the whole approach is complicated, convoluted and quite frankly nasty (you have no comments explaining the logic of why you catch some exceptions and why you do what you) but if you're going to use the default locale this is how you ought to do it IMO: lang, encoding = locale.getdefaultlocale() if encoding is None: # no default locale, or can't determine it # what the hell do we guess now??? else: try: unistr = arg.decode(encoding) except UnicodeDecodeError: # And again, what guess do we make now??? else: return unistr.encode('utf-8') # Why UTF-8? but as already mentioned, I think that being less "Do What I Mean" and more "be explicit about what you want" is a better idea. As far as your 2/3 compatibility code at the top of the module: try: unichr except NameError: