[Tutor] Help with python
I'm writing a program to calculate totals and change for a menu, and I'm having a few issues. If you could help me, it would be greatly appreciated. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Perl equivalent of $#var
My current dilemma is that I've got a program that takes one argument and needs to be run multiple times with this argument being validated based on the previous one. So proper usage might be myprog red myprog blue myprog green where it would be wrong to do myprog red myprog green I have a list which gives the proper order colors = ['red', 'blue', 'green'] I've got to say, that's an odd setup you've got there. If it really needs to run in the proper order, why not just take no arguments, and put the whole program in a for color in colors: loop? Or take no arguments, store the last run in a file, and if now == len(colors) - 1 which is distinctly ugly and I would prefer if now == $#colors Perhaps it would look nicer to you if you had: colors = ['red', 'blue', 'green'] LAST_COLOR = len(colors) - 1 [...] if now == LAST_COLOR: # Do something. Later, Blake. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generator function question?
Sorry for jumping in to this a little late, but... This is (IMO) more elegant: def neverEndingStatus(): index = 0 statusChars = ['|', '\\', '-', '/'] while True: yield statusChars[index] index = (index + 1) % 4 Why not go a step further? def neverEndingStatus(): statusChars = ['|', '\\', '-', '/'] while True: for char in statusChars: yield char or def neverEndingStatus(): while True: yield '|' yield '\\' yield '-' yield '/' Later, Blake. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is there an easy way to conduct binary numbers?
Juan Shen wrote: Binary integer is extremely useful in my electronic-related job. So...I need help. Is there any function to transform between binary and decimal integers in python's library? If not, what's the solution to binary? I can't speak for everyone, but most of the people I've met who wrok with binary learn how to read hex and translate it to binary in their heads. (It's slow at first, but it gets really quick with a bit of practice.) You could, if you needed to, write a bin function yourself. I would recommend calling the oct function, and replacing the characters in the string. (If you need help getting that working, give it a good try, and post what you have, and I'll be happy to assist you.) Later, Blake. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] am I missing another simpler structure?
Juan Shen wrote: def is_leap_year(year): is_leap = True try: datetime.date(year, 2, 29) except ValueError: is_leap = False return is_leap I would write def is_leap_year(year): try: datetime.date(year, 2, 29) return True except ValueError: return False Yeah, I support Kent. Brian's code is obviously C style, define a variable and give it an origin value, then use it, modify its value and so on. If you choose Python, you should adapt to it that variable needn't to be defined specificly before being used! I far prefer the Brian's version, because it lets me set a single breakpoint while I'm debugging, and I can look at the return value before returning it, instead of having to set n breakpoints (or, usually n-1 because I've overlooked the one that's actually being executed) and looking at what's being returned on each line. (Yes, I do the same thing in C and C++, but I originally started using it in Java, and after a few debugging sessions it makes a lot of sense.) Only having one return point from a function is a long-standing convention that is supposed to make programs easier to read/debug/optimize/prove correct. Later, Blake. ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor