Re: [Tutor] Random number selection
Dave Angel wrote: Variables with integer values are not 'called." They also don't change values unless you change it. So if you've got a loop, and you want them to have different values each time, then give them different values each time, by calling a function that does return a different value. In fairness, some programming communities call instance attributes "variables" (which annoys me no end, but it is common), and computed attributes ("variables") can get a different value each time you look them up. So if you can have this: instance = MyClass() instance.x # x is a computed "instance variable" (blah, I hate that term) => gives 23 instance.x => gives 42 then in principle it isn't a stupid idea to have this too: x = MyVariable() x => gives 23 x => gives 42 I don't know any language that operates like that, and it is probably a bad idea in practice, but the principle isn't entirely crazy. It's also an *incredibly* common thought -- I have seen so many people assume that after doing this once: x = random(1, 100) x will now have a different random number each time you look at it. Which kind of makes sense, if you think of x as a variable that varies on its own instead of something which can be varied. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Which should it be, lists, tuples, dictionary or files?
On 03/01/12 07:31, Devin Jeanpierre wrote: It's probably worth mentioning that shelve is not secure; loading a saved shelf can involve executing arbitrary python code embedded inside it. This probably isn't important for this particular project, but maybe in the future, if you consider using shelf for something more security-conscious... A good point, shelve should be treated more like an internal persistence tool than a general purpose database. It's fine if you know what you are storing but if random data is being put into it there could be problems. -- 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: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random number selection
On 03/01/12 01:52, shane wrote: I was wondering is there a way to have a variable generate a random integer each time the variable is called. You can call a function to assign a value to a variable, but you can't "call" a variable (unless its assigned to a function but that's another subject entirely!). Ive tried random.randint(a, b) and the range one to. It selects a random number and assigns it to the variable Yes, that's what it's supposed to do. So where do you have a problem? this part of the program would be math equations with random number selections x=ran y=ran z=x*y In Python that translates to x = random.randint(a,b) y = random.randint(a,b) z = x*y x * y = z But this doesn't make sense unless you mean an equality test. In which case in Python it's written: x*y == z and given the code above should equate to True s=input=('x*y= ') Too many equal signs there. I assume you mean s = input("x*y= ") Which will assign a string value from the user to s(*). I'm not sure what you think the string will contain? The letter 'z' maybe? or some arbitrary number that you need to factorise to come up with new x,y values? I can only guess. I'll assume the second option - a string version of a number (*) I'm assuming you are using Python version 3? If not input could return a number rather than a string but since input() is not secure we generally recommend you don;pt use it in version 2. if s != z z is a number, s a string so you need to convert: if int(s) != z: There are many things I need to solve its my first program. True, but at the moment I don't think we understand what it is you are trying to do. But I just need help on assigning a random number and looping through to get diff values for x and y A random number or multiple random numbers? And do you want the random numbers assigned to x,y or something else? And "looping through" what? -- 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: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Which should it be, lists, tuples, dictionary or files? [SOLVED]
On 01/03/2012 06:28 AM, Alan Gauld wrote: On 03/01/12 07:31, Devin Jeanpierre wrote: It's probably worth mentioning that shelve is not secure; loading a saved shelf can involve executing arbitrary python code embedded inside it. This probably isn't important for this particular project, but maybe in the future, if you consider using shelf for something more security-conscious... A good point, shelve should be treated more like an internal persistence tool than a general purpose database. It's fine if you know what you are storing but if random data is being put into it there could be problems. My thanks to all of those who replied and offered suggestions. I will be looking into every one of those suggestions. Again, my thanks. This is truly a great Python group. Ken, Louisville, KY, USA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Parse multi line with re module.
Hi Guys, I want parse multiple line. with re.module, this is my given string http://dpaste.com/680760/ I have try with re.compile module. I want parse two line mac address and channel, I have done with for mac address finding r = re.compile("^Searching for OPUSH on (\w\w(:\w\w)+)") for channel finding device_r = re.compile("^Channel: (\d+)") the two parsing string working. but I want combine two pattern in to one. This is my code http://www.bpaste.net/show/21323/ please guide me . -Ganesh Did I learn something today? If not, I wasted it. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Parse multi line with re module.
On Tue, Jan 3, 2012 at 9:13 AM, Ganesh Kumar wrote: > Hi Guys, > > I want parse multiple line. with re.module, this is my given string > http://dpaste.com/680760/ I have try with re.compile module. I want parse > two line mac address and channel, > I have done with for mac address finding > > r = re.compile("^Searching for OPUSH on (\w\w(:\w\w)+)") > > for channel finding > > device_r = re.compile("^Channel: (\d+)") > > the two parsing string working. but I want combine two pattern in to one. > > This is my code > http://www.bpaste.net/show/21323/ > > please guide me . > > > -Ganesh > > Did I learn something today? If not, I wasted it. > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Do you know about str.startswith() http://docs.python.org/library/stdtypes.html#str.startswith Since your data is so well defined I think it would be simpler to just read each line, strip whitespace, use startswith() to check for "Searching for OPUSH on" and "Channel:" You can slice to pull out the mac address and the channel number like so: >>> s = "Searching for OPUSH on 00:1D:FD:06:99:99" >>> s[23:] '00:1D:FD:06:99:99' # this will work if no other text follows the mac address >>> s[23:41] '00:1D:FD:06:99:99' # this will work to just capture the mac address >>> For the Channel number, after stripping whitespace, take the slice from s[9:] Searching for OPUSH on 00:1D:FD:06:99:99 ... Channel: 9 Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Still the Python Challenge
Hi list! After this past week of no PC and no work, I'm again at the Python Challenge Cypher problem. I decided to go with the flow and solve it with maketrans (6 lines of code against the 40 and going I had already...) and it solved it quickly and clean! However, and please be sure I don't want to re-invent any wheel, I still got some doubts unexplained... - How to correctly populate a list using, for instance, a for loop; - how to correctly use join to read the said list in a human friendly way... This was the code I was using. for letter in cypheredText: asciiValue = ord(letter) if asciiValue in range(97, 123): asciiValue += shiftedCypherNumber if asciiValue > 122: asciiValue -= 26 newLetter = chr(asciiValue) text = list() text.append(newLetter) joinedText = ' '.join(text) return joinedText Thanks for all your time and patience. Sorry for before not using Plain text! Joaquim Santos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Parse multi line with re module.
On 3 January 2012 16:21, Joel Goldstick wrote: > -- Joel, when you start your emails with this (two dashes) some email readers (gmail among them) will think you're ending your email; I have to expand your post because it thinks that what is hidden is the signature. Just thought you should know. -- best regards, Robert S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Still the Python Challenge
On 03/01/12 17:44, Joaquim Santos wrote: - How to correctly populate a list using, for instance, a for loop; I can't tell what you had because the formatting got lost in the email ether... - how to correctly use join to read the said list in a human friendly way... You seem to be doing that OK as is. Do you think you have a problem with it? This was the code I was using. for letter in cypheredText: asciiValue = ord(letter) if asciiValue in range(97, 123): asciiValue += shiftedCypherNumber if asciiValue> 122: asciiValue -= 26 newLetter = chr(asciiValue) Up to her is OK, but... Asssuming this next line is still inside the for loop? text = list() If this is in the loop then you create a new list for every letter. And you override the previous one so losing it... You probably want this before the loop. Its more usual to just do text = [] though But both techniques work. text.append(newLetter) joinedText = ' '.join(text) This is right but your text only has a single value in it as explained above. Thanks for all your time and patience. Sorry for before not using Plain text! It seems something is still broken! :-( -- 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: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Parse multi line with re module.
On Tue, Jan 3, 2012 at 12:51 PM, Robert Sjoblom wrote: > On 3 January 2012 16:21, Joel Goldstick wrote: >> -- > > Joel, when you start your emails with this (two dashes) some email > readers (gmail among them) will think you're ending your email; I have > to expand your post because it thinks that what is hidden is the > signature. Just thought you should know. > > > -- > best regards, > Robert S. Thank you. I'm not sure how that happens. I'll check my signature. Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Primitive Chess Clock Program Question
Hi, So far, I know about raw_input as a way to get something from the keyboard into my program. My question is this. Let's say I am trying to subtract time from a value as though someone is thinking and they want to type in a move. If my program prints a prompt to the screen, while the person is thinking, is it still running, or is it asleep? Another way to put this is, can my program use one of the functions in datetime to keep ticking off seconds for itself, but not show them to the user while it's waiting for input, or do I need to simply check the time after they type their move in, then update the clock at that point? Thanks. Jim Jim Homme, Usability Services, Phone: 412-544-1810. This e-mail and any attachments to it are confidential and are intended solely for use of the individual or entity to whom they are addressed. If you have received this e-mail in error, please notify the sender immediately and then delete it. If you are not the intended recipient, you must not keep, use, disclose, copy or distribute this e-mail without the author's prior permission. The views expressed in this e-mail message do not necessarily represent the views of Highmark Inc., its subsidiaries, or affiliates. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Still the Python Challenge
Joaquim Santos wrote: Hi list! After this past week of no PC and no work, I'm again at the Python Challenge Cypher problem. I decided to go with the flow and solve it with maketrans (6 lines of code against the 40 and going I had already...) and it solved it quickly and clean! However, and please be sure I don't want to re-invent any wheel, I still got some doubts unexplained... - How to correctly populate a list using, for instance, a for loop; In general: mylist = [] for value in some_sequence: mylist.append( process(value) ) Here's a concrete example: take a list of words, convert to lower case, and make them plural: words = ['CAT', 'DOG', 'TREE', 'CAR', 'PENCIL'] plurals = [] for word in words: word = word.lower() plurals.append(word + 's') print(plurals) - how to correctly use join to read the said list in a human friendly way... I don't understand this question. You can't use join to read a list. Perhaps you mean to use join to print a list? Specifically, you use join to build a single string from a list of sub-strings. Given plurals above, we can build a single list of words separated by double spaces and a hyphen: print(' - '.join(plurals)) To join a list of single characters into a string, use join on the empty string. Compare: chars = ['a', 'b', 'c', 'd'] print(' '.join(chars)) print(''.join(chars)) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Primitive Chess Clock Program Question
Homme, James wrote: Hi, So far, I know about raw_input as a way to get something from the keyboard into my program. My question is this. Let's say I am trying to subtract time from a value as though someone is thinking and they want to type in a move. If my program prints a prompt to the screen, while the person is thinking, is it still running, or is it asleep? raw_input blocks for as long as it takes for the user to type something and press Enter. If they take an hour, raw_input will wait an hour. I assume you want to display something like this: Enter your next move: 0:30 where the "0:30" is the time remaining, and is constantly updating. When it hits zero, the function returns whether the user has typed anything or not. Oooh, this is a cunning problem! I like it. But I have no idea how to solve it. I'm going to have to do some research, in my Copious Spare Time. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Primitive Chess Clock Program Question
On 03/01/12 21:28, Steven D'Aprano wrote: I assume you want to display something like this: Enter your next move: 0:30 where the "0:30" is the time remaining, and is constantly updating. When it hits zero, the function returns whether the user has typed anything or not. Assuming Steven has guessed right then I think you need to use one of the non blocking input mechanisms like kbhit() or getch() or somesuch. Those methods are notioriously unreliable and OS specific. For example you may need to use curses or the Microsoft runtime module msvcrt. The general code will look like Display prompt while no key hit sleep briefly update time in prompt (using ctrl characters to delete/overwrire previouis entry) #when key hit process input. It is a non trivial problem and the details will depend on whether you use curses or the microsoft route. It is one of those few cases that is actually much easier to do in a GUI. GUIs generally make life more complex but in this case updating a label while awaiting user input is almost trivial (for a GUI). -- 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: http://mail.python.org/mailman/listinfo/tutor