[Tutor] audio splitting with python
Hi list, I'd like to split an ogg audio file into pieces (small enough to fit in an audio cd). Can anybody suggest me a python module to do that? Thanks, Daniele ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] audio splitting with python
On Thu, Oct 2, 2008 at 3:52 AM, Daniele <[EMAIL PROTECTED]> wrote: > Hi list, > I'd like to split an ogg audio file into pieces (small enough to fit > in an audio cd). Can anybody suggest me a python module to do that? Perhaps PyOgg or oggpy: http://ekyo.nerim.net/software/pyogg/ http://dingoskidneys.com/oggpy/ Or use Audacity or another tool: http://audacity.sourceforge.net/ http://www.exefind.com/split-ogg/ Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hands-on beginner's project?
Okay, I'm resurrecting this project. I did a little more work on it then what you see here, but that ended up getting accidentally deleted, and I haven't done anything with Python since. I'm running into one problem already, and I haven't really added any extra code. In the content (at this point, the rooms still have Bob's descriptions), the very first line of actual code (third line in the program) is returning an error. Offending code: # Content # retrieve data for current room room == start while true: if room == start: desc = "Ahead of you, you see a chasm." ques = "Do you wish to try jumping over it? Y/N" destY = 2 destN = 3 Error: Traceback (most recent call last): File "samplegame.py", line 3, in room == start NameError: name 'room' is not defined What's going on here? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hands-on beginner's project?
room == start is a comparison (check if room is equal to start - doesn't make a whole lot of sense to be doing that here). The "room" variable name doesn't exist in the current namespace, so it can't be compared to start. It looks like you wanted room = start. On 10/2/08, nathan virgil <[EMAIL PROTECTED]> wrote: > > Okay, I'm resurrecting this project. I did a little more work on it then > what you see here, but that ended up getting accidentally deleted, and I > haven't done anything with Python since. > > I'm running into one problem already, and I haven't really added any extra > code. In the content (at this point, the rooms still have Bob's > descriptions), the very first line of actual code (third line in the > program) is returning an error. > > Offending code: > > # Content > # retrieve data for current room > room == start > while true: > if room == start: > desc = "Ahead of you, you see a chasm." > ques = "Do you wish to try jumping over it? Y/N" > destY = 2 > destN = 3 > > Error: > > Traceback (most recent call last): > File "samplegame.py", line 3, in > room == start > NameError: name 'room' is not defined > > > What's going on here? > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] syntax error with a simple print command
Dear list, I was just trying the following code: for i in range(0, 10, 2): print i print "done!" But I do get a syntax error, and I don't understand why: >>> for i in range(0, 10, 2): ... print i ... print "done!" File "", line 3 print "done!" ^ As far as I understand, the for loop is executed, and once the loop is done Python exits the for loop and continues with the next command (print "done!"). Why is he complaining?? Many thanks for enlightening me! David ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] syntax error with a simple print command
You are having an issue with the interpreter. You can only submit 1 command at a time to the interpreter. Therefore, you need to finish your for loop and hit enter *twice* so that the interpreter knows you're done with the loop. The reason you're getting an error is it's trying to put your "print 'done!'" statement in the for loop, but it's not tabbed correctly. it works like this because otherwise you could only have single-line for loops! On Thu, Oct 2, 2008 at 9:35 AM, David <[EMAIL PROTECTED]> wrote: > Dear list, > > I was just trying the following code: > > for i in range(0, 10, 2): >print i > print "done!" > > But I do get a syntax error, and I don't understand why: > > >>> for i in range(0, 10, 2): > ... print i > ... print "done!" > File "", line 3 >print "done!" >^ > > As far as I understand, the for loop is executed, and once the loop is done > Python exits the for loop and continues with the next command (print > "done!"). > Why is he complaining?? > > Many thanks for enlightening me! > > David > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] syntax error with a simple print command
note that I meant it's not tabbed correctly *to be part of the for loop*. On Thu, Oct 2, 2008 at 9:42 AM, Luke Paireepinart <[EMAIL PROTECTED]>wrote: > You are having an issue with the interpreter. You can only submit 1 > command at a time to the interpreter. > Therefore, you need to finish your for loop and hit enter *twice* so that > the interpreter knows you're done with the loop. > The reason you're getting an error is it's trying to put your "print > 'done!'" statement in the for loop, but it's not tabbed correctly. > it works like this because otherwise you could only have single-line for > loops! > > > On Thu, Oct 2, 2008 at 9:35 AM, David <[EMAIL PROTECTED]> wrote: > >> Dear list, >> >> I was just trying the following code: >> >> for i in range(0, 10, 2): >>print i >> print "done!" >> >> But I do get a syntax error, and I don't understand why: >> >> >>> for i in range(0, 10, 2): >> ... print i >> ... print "done!" >> File "", line 3 >>print "done!" >>^ >> >> As far as I understand, the for loop is executed, and once the loop is >> done Python exits the for loop and continues with the next command (print >> "done!"). >> Why is he complaining?? >> >> Many thanks for enlightening me! >> >> David >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sample python twill scripts?
jeremiah wrote: Just wondering if anyone here would be interested in sharing a python twill script? I'd just like to take a gander at how others are engineering their scripts. twill? -- Bob Gailer Chapel Hill NC 919-636-4239 When we take the time to be aware of our feelings and needs we have more satisfying interatctions with others. Nonviolent Communication provides tools for this awareness. As a coach and trainer I can assist you in learning this process. What is YOUR biggest relationship challenge? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Idle and windows XP firewall
I am using python 2.5.2. Is there an alternative to idle that does not have this problem? (I also fixed that this was no longer a problem). ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Idle and windows XP firewall
No that does not work. When I click on idle it just does not open - very annoying. --- On Thu, 2/10/08, Luke Paireepinart <[EMAIL PROTECTED]> wrote: From: Luke Paireepinart <[EMAIL PROTECTED]> Subject: Re: [Tutor] Idle and windows XP firewall To: [EMAIL PROTECTED] Cc: tutor@python.org Date: Thursday, 2 October, 2008, 4:53 PM On Thu, Oct 2, 2008 at 10:31 AM, David Holland <[EMAIL PROTECTED]> wrote: I am using python 2.5.2. Is there an alternative to idle that does not have this problem? (I also fixed that this was no longer a problem). Open IDLE without using the "edit with IDLE" link. Or use a different editor. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Idle and windows XP firewall
On Thu, Oct 2, 2008 at 10:31 AM, David Holland <[EMAIL PROTECTED]>wrote: > I am using python 2.5.2. Is there an alternative to idle that does not > have this problem? > (I also fixed that this was no longer a problem). > > Open IDLE without using the "edit with IDLE" link. Or use a different editor. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Idle and windows XP firewall
Where are you running it from? it needs some weird command line parameters, so you probably need to use the shortcut in the start bar that Python made for you. On Thu, Oct 2, 2008 at 10:55 AM, David Holland <[EMAIL PROTECTED]>wrote: > No that does not work. When I click on idle it just does not open - very > annoying. > > --- On *Thu, 2/10/08, Luke Paireepinart <[EMAIL PROTECTED]>* wrote: > > From: Luke Paireepinart <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Idle and windows XP firewall > To: [EMAIL PROTECTED] > Cc: tutor@python.org > Date: Thursday, 2 October, 2008, 4:53 PM > > > > > On Thu, Oct 2, 2008 at 10:31 AM, David Holland <[EMAIL PROTECTED]>wrote: > >> I am using python 2.5.2. Is there an alternative to idle that does not >> have this problem? >> (I also fixed that this was no longer a problem). >> >> Open IDLE without using the "edit with IDLE" link. > Or use a different editor. > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Idle and windows XP firewall
I just using the short cut and nothing happens :(. I have managed to do this before just not in my new job --- On Thu, 2/10/08, Luke Paireepinart <[EMAIL PROTECTED]> wrote: From: Luke Paireepinart <[EMAIL PROTECTED]> Subject: Re: [Tutor] Idle and windows XP firewall To: [EMAIL PROTECTED] Cc: tutor@python.org Date: Thursday, 2 October, 2008, 5:01 PM Where are you running it from? it needs some weird command line parameters, so you probably need to use the shortcut in the start bar that Python made for you. On Thu, Oct 2, 2008 at 10:55 AM, David Holland <[EMAIL PROTECTED]> wrote: No that does not work. When I click on idle it just does not open - very annoying. --- On Thu, 2/10/08, Luke Paireepinart <[EMAIL PROTECTED]> wrote: From: Luke Paireepinart <[EMAIL PROTECTED]> Subject: Re: [Tutor] Idle and windows XP firewall To: [EMAIL PROTECTED] Cc: tutor@python.org Date: Thursday, 2 October, 2008, 4:53 PM On Thu, Oct 2, 2008 at 10:31 AM, David Holland <[EMAIL PROTECTED]> wrote: I am using python 2.5.2. Is there an alternative to idle that does not have this problem? (I also fixed that this was no longer a problem). Open IDLE without using the "edit with IDLE" link. Or use a different editor. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Idle and windows XP firewall
Try installing pythonwin, it's an editor by the guy that made the win32all extensions. Or use eclipse with Python plugin, maybe? On Thu, Oct 2, 2008 at 11:31 AM, David Holland <[EMAIL PROTECTED]>wrote: > I just using the short cut and nothing happens :(. > > I have managed to do this before just not in my new job > > --- On *Thu, 2/10/08, Luke Paireepinart <[EMAIL PROTECTED]>* wrote: > > From: Luke Paireepinart <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Idle and windows XP firewall > To: [EMAIL PROTECTED] > Cc: tutor@python.org > Date: Thursday, 2 October, 2008, 5:01 PM > > > Where are you running it from? it needs some weird command line > parameters, so you probably need to use the shortcut in the start bar that > Python made for you. > > On Thu, Oct 2, 2008 at 10:55 AM, David Holland <[EMAIL PROTECTED]>wrote: > >> No that does not work. When I click on idle it just does not open - very >> annoying. >> >> --- On *Thu, 2/10/08, Luke Paireepinart <[EMAIL PROTECTED]>* wrote: >> >> From: Luke Paireepinart <[EMAIL PROTECTED]> >> Subject: Re: [Tutor] Idle and windows XP firewall >> To: [EMAIL PROTECTED] >> Cc: tutor@python.org >> Date: Thursday, 2 October, 2008, 4:53 PM >> >> >> >> >> On Thu, Oct 2, 2008 at 10:31 AM, David Holland <[EMAIL PROTECTED]>wrote: >> >>> I am using python 2.5.2. Is there an alternative to idle that does not >>> have this problem? >>> (I also fixed that this was no longer a problem). >>> >>> Open IDLE without using the "edit with IDLE" link. >> Or use a different editor. >> >> >> > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] syntax error with a simple print command
On Thu, Oct 2, 2008 at 11:42 AM, David <[EMAIL PROTECTED]> wrote: > Thanks, Luke, that makes sense. > This is only applicable to the interpreter, though. It's perfectly legal to have a statement on the line immediately following a loop when you're writing code files. Also, please use "reply all" or "reply to group" so that it goes back to the mailing list, so that people reading the archives and such can see your reply, and other people know that you understand the issue and don't need further assistance. Everyone forgets once in a while, so we just try to remind each other. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Idle and windows XP firewall
That works thanks --- On Thu, 2/10/08, Luke Paireepinart <[EMAIL PROTECTED]> wrote: From: Luke Paireepinart <[EMAIL PROTECTED]> Subject: Re: [Tutor] Idle and windows XP firewall To: [EMAIL PROTECTED] Cc: tutor@python.org Date: Thursday, 2 October, 2008, 5:33 PM Try installing pythonwin, it's an editor by the guy that made the win32all extensions. Or use eclipse with Python plugin, maybe? On Thu, Oct 2, 2008 at 11:31 AM, David Holland <[EMAIL PROTECTED]> wrote: I just using the short cut and nothing happens :(. I have managed to do this before just not in my new job --- On Thu, 2/10/08, Luke Paireepinart <[EMAIL PROTECTED]> wrote: From: Luke Paireepinart <[EMAIL PROTECTED]> Subject: Re: [Tutor] Idle and windows XP firewall To: [EMAIL PROTECTED] Cc: tutor@python.org Date: Thursday, 2 October, 2008, 5:01 PM Where are you running it from? it needs some weird command line parameters, so you probably need to use the shortcut in the start bar that Python made for you. On Thu, Oct 2, 2008 at 10:55 AM, David Holland <[EMAIL PROTECTED]> wrote: No that does not work. When I click on idle it just does not open - very annoying. --- On Thu, 2/10/08, Luke Paireepinart <[EMAIL PROTECTED]> wrote: From: Luke Paireepinart <[EMAIL PROTECTED]> Subject: Re: [Tutor] Idle and windows XP firewall To: [EMAIL PROTECTED] Cc: tutor@python.org Date: Thursday, 2 October, 2008, 4:53 PM On Thu, Oct 2, 2008 at 10:31 AM, David Holland <[EMAIL PROTECTED]> wrote: I am using python 2.5.2. Is there an alternative to idle that does not have this problem? (I also fixed that this was no longer a problem). Open IDLE without using the "edit with IDLE" link. Or use a different editor. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] dealing with user input whose value I don't know
Hello, I am trying to do some exercises in John Zelle's book (chapter 4). I got stuck: "Write a program that finds the average of a series of numbers entered by the user. The program should first ask the user how many numbers there are. Note: the average should always be a float, even if the user inputs are all ints." Okay, I can ask how many number are to be added: numbers = input("How many number do you want me to calculate? ") If I then get a reply, say "5", what I would have to do next is to ask for the five numbers so that I can calculate the average. But given that I don't know the the value of 'numbers' ex ante, how could I ask for the right amount of numbers? I don't see how this can be achieved with the tools I have learned so far... I am currently thinking along the lines of ans1, ans2 = input("Enter the numbers separated by a comma: ") average = (ans1 + ans2) / 2.0 But as I say - I don't know how many assignment there have to be, nor do I know how Python could then create these assignments. It would be great if someone could guide me towards the right track!! Thanks, David ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Idle and windows XP firewall
On Thu, Oct 2, 2008 at 12:00 PM, David Holland <[EMAIL PROTECTED]> wrote: > > That works thanks > You should send e-mails to the list in plaintext, because it's really hard to reply to your e-mails in HTML mode. I mean, it's not hard for me to convert them, but it's just one of those minor nuisances that may cause some people to decide they don't want to reply that much, when it's hard to get the formatting right. Or maybe that's just me. Anyway, I hope you like PythonWin. If not, there are countless threads on here about which IDE is the best, and you can look on the online mailing list archive at activestate or gmane if you want to check out some of those threads. We probably don't need to have that discussion again right now, though. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
On Thu, Oct 2, 2008 at 12:06 PM, David <[EMAIL PROTECTED]> wrote: > Hello, > > I am trying to do some exercises in John Zelle's book (chapter 4). > I got stuck: > > "Write a program that finds the average of a series of numbers entered by > the user. The program should first ask the user how many numbers there are. > Note: the average should always be a float, even if the user inputs are all > ints." > > Okay, I can ask how many number are to be added: > > numbers = input("How many number do you want me to calculate? ") > > If I then get a reply, say "5", what I would have to do next is to ask for > the five numbers so that I can calculate the average. > But given that I don't know the the value of 'numbers' ex ante, how could I > ask for the right amount of numbers? > I don't see how this can be achieved with the tools I have learned so far... > I am currently thinking along the lines of > > ans1, ans2 = input("Enter the numbers separated by a comma: ") > average = (ans1 + ans2) / 2.0 > > But as I say - I don't know how many assignment there have to be, nor do I > know how Python could then create these assignments. This is a common issue beginners to programming have. The question you ask yourself here is " do I really need a direct reference in code to all my values?" It appears to me that you don't. For example, how would you do this in real life? would you say x = num1 x2 = num2 x3 = num3 ... xn = numn x + x2 + x3 + x4 ... + xn / n or would you do this: 1 + 2 + 3 + 4 + 5 / count I would do the latter. It's the same way in programming. You can create these generic collections of items in Python. They are called "lists." I'm a little pressed for time (i have a class starting in a few minutes) but this example should hopefully spark something in 'ya. a = [] b = [1,2,3,4,5] for item in b: a.append(item) Does that give you a hint about how you can add items to a collection without caring how many you have? Note that you can also do something like this (this is a bigger hint) a = [] b = [1,2,3,4,5] for i in range(len(b)): a.append(b[i]) Good luck! > > It would be great if someone could guide me towards the right track!! > > Thanks, > > David > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
On Fri, Oct 03, 2008 at 01:06:29AM +0800, David wrote: > Hello, > > I am trying to do some exercises in John Zelle's book (chapter 4). > I got stuck: > > Okay, I can ask how many number are to be added: > > numbers = input("How many number do you want me to calculate? ") > > If I then get a reply, say "5", what I would have to do next is to ask > for the five numbers so that I can calculate the average. > But given that I don't know the the value of 'numbers' ex ante, how > could I ask for the right amount of numbers? You don't need to know in advance what the value of will be. You can have Python iterate times, asking for an additional number each time. You could add each to a variable (so it accumulates the sum as you iterate) and then divide by . You could collect everything in a list and then do the calculation. There's a couple of ideas. See where that leads you and let us know. -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
[EMAIL PROTECTED] wrote on 10/02/2008 01:06:29 PM: > Hello, > > I am trying to do some exercises in John Zelle's book (chapter 4). > I got stuck: > > "Write a program that finds the average of a series of numbers entered > by the user. The program should first ask the user how many numbers > there are. Note: the average should always be a float, even if the user > inputs are all ints." > > Okay, I can ask how many number are to be added: > > numbers = input("How many number do you want me to calculate? ") you should really use raw_input to get the info from the user, and then convert it to a number. numbers=int(raw_input("How many number do you want me to calculate? ")) > > If I then get a reply, say "5", what I would have to do next is to ask > for the five numbers so that I can calculate the average. Write the code like you knew it was going to be a 5 and then replace anywhere the 5 appears with the variable 'numbers'. > But given that I don't know the the value of 'numbers' ex ante, how > could I ask for the right amount of numbers? > I don't see how this can be achieved with the tools I have learned so far... Looking at the table of contents it looks like you should have learned about loops by now. > I am currently thinking along the lines of > > ans1, ans2 = input("Enter the numbers separated by a comma: ") > average = (ans1 + ans2) / 2.0 have each number be its own input and repeat it depending on how their input for numbers, and then do the averaging at the end. > > But as I say - I don't know how many assignment there have to be, nor do > I know how Python could then create these assignments. > you don't need to keep the individual numbers only the sum, but if you want to, use a list and append each new number to the end of the list. > It would be great if someone could guide me towards the right track!! > > Thanks, > > David > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
Hello Christopher, [EMAIL PROTECTED] wrote: > > Okay, I can ask how many number are to be added: > > numbers = input("How many number do you want me to calculate? ") you should really use raw_input to get the info from the user, and then convert it to a number. numbers=int(raw_input("How many number do you want me to calculate? ")) Does that mean input() is obsolete (after all, Zelle's book is not the freshest on the shelf)? Or do they have different uses? Thanks, David ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Multiple windows in Tkinter
Hello, I am a little confused how multi-windows work in Tkinter. I am currently using it while learning to program. Using it for single window apps/scripts is fine. However what I dont understand is: What is toplevel? Is it the same as Tk()? Do I initialise a root using Tk() and then use toplevel for any other windows? How do I switch between the Windows? And while I am on the subject what is a frame and why should I use it? atm the moment it just seems to be the same as Tk()? Many thanks for any advice. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
On Fri, Oct 03, 2008 at 01:38:48AM +0800, David wrote: > Does that mean input() is obsolete (after all, Zelle's book is not the > freshest on the shelf)? Or do they have different uses? Depends on how you look at it. input() automatically evaluates whatever the user types as a Python expression and returns the result. So if they type 5, the integer 5 is returned. For your program, that's probably what you want, and has the advantage of letting you type something like 2+3 so your user can let Python evaluate math expressions. On the other hand, you'd think that you could ask a user for a text response using input(): name = input("What is your name? ") print "Hello, ", name But if they just type the answer, Python will crash with an error because it's expecting a legal Python expression there (so a string value would have to be typed in quotes). However, raw_input() will just return the characters the user typed without doing anything to them. Great for string values, but this means to get an integer result you'll have to pass that into the int() constructor function. IIRC Python 3.0 will actually make input() do what raw_input() today does, because this is confusing to people as it stands now. -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
On Thu, Oct 02, 2008, Steve Willoughby wrote: >On Fri, Oct 03, 2008 at 01:38:48AM +0800, David wrote: >> Does that mean input() is obsolete (after all, Zelle's book is not the >> freshest on the shelf)? Or do they have different uses? > >Depends on how you look at it. > >input() automatically evaluates whatever the user types as a Python >expression and returns the result. So if they type 5, the integer >5 is returned. For your program, that's probably what you want, and >has the advantage of letting you type something like 2+3 so your user >can let Python evaluate math expressions. > >On the other hand, you'd think that you could ask a user for a text >response using input(): > name = input("What is your name? ") > print "Hello, ", name > >But if they just type the answer, Python will crash with an error >because it's expecting a legal Python expression there (so a >string value would have to be typed in quotes). Remember the cardinal rule NEVER TRUST USER INPUT! Always check for validity, and use methods that prevent malicious strings from allowing the user to get unauthorized access or change things they shouldn't. Many of the common exploits of web pages are the result of poor checking of input resulting in sql injection attacks, and other breaches. Bill -- INTERNET: [EMAIL PROTECTED] Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax:(206) 232-9186 There are three kinds of men. The ones that learn by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves. -- Will Rogers ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
Cheers for the insights! However, I just found out that changing input() to raw_input() breaks my code: This program takes the average of numbers you supply!! How many numbers do you want me to work with? 2 You want me to take the average of 2 numbers. Please type the numbers, separated by commas: 1,2 You want to know the average of the numbers: 1,2 Traceback (most recent call last): File "avgInput.py", line 13, in add = add + i TypeError: unsupported operand type(s) for +: 'int' and 'str' End of process output The reason being, I take, that numbers = raw_input("Please type the numbers, separated by commas: ") also returns the comma (1,2) and thus the for loop can't cope... So should I therefore retain numbers = input("Please type the numbers, separated by commas: ") ? Otherwise I don't know (yet) what to do David Bill Campbell wrote: On Thu, Oct 02, 2008, Steve Willoughby wrote: On Fri, Oct 03, 2008 at 01:38:48AM +0800, David wrote: Does that mean input() is obsolete (after all, Zelle's book is not the freshest on the shelf)? Or do they have different uses? Depends on how you look at it. input() automatically evaluates whatever the user types as a Python expression and returns the result. So if they type 5, the integer 5 is returned. For your program, that's probably what you want, and has the advantage of letting you type something like 2+3 so your user can let Python evaluate math expressions. On the other hand, you'd think that you could ask a user for a text response using input(): name = input("What is your name? ") print "Hello, ", name But if they just type the answer, Python will crash with an error because it's expecting a legal Python expression there (so a string value would have to be typed in quotes). Remember the cardinal rule NEVER TRUST USER INPUT! Always check for validity, and use methods that prevent malicious strings from allowing the user to get unauthorized access or change things they shouldn't. Many of the common exploits of web pages are the result of poor checking of input resulting in sql injection attacks, and other breaches. Bill ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
On Thu, Oct 02, 2008 at 10:54:56AM -0700, Bill Campbell wrote: > Remember the cardinal rule NEVER TRUST USER INPUT! Always check > for validity, and use methods that prevent malicious strings from > allowing the user to get unauthorized access or change things > they shouldn't. Yes, I probably should have qualified what I said. This is VERY important. Should you ever allow input() or other eval() of what the user typed (or for that matter, passing what the user types into file operations, SQL queries, etc)? Yes, but *only* if you are *certain* you *must* and that you know exactly what you're doing. And probably not even then if you can avoid it. So the point was what the difference was between raw_input() and input(), but Bill's right, don't just use input() or eval() (and input() is essentially eval(raw_input())) casually. Not sure why? Suppose you put a program up for public use which gets a string value using input(). Instead of How many numbers? 5 the user types: How many numbers? os.system('rm -rf /') Don't ever assume data is safe or valid if it came from outside your realm of control. (And don't fool yourself that a script is "just for me", in most environments things get reused in ways you don't expect, and even if not, get used to good programming habits). -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
On Fri, Oct 03, 2008 at 02:06:47AM +0800, David wrote: > Cheers for the insights! > > However, I just found out that changing input() to raw_input() breaks my > code: Recall that we told you raw_input() returns a string, while input() returns an integer if you typed an integer value. So you need to convert the string of characters the user typed into an integer value before using it as a number: numbers = int(raw_input(...)) > > This program takes the average of numbers you supply!! > How many numbers do you want me to work with? 2 > You want me to take the average of 2 numbers. > Please type the numbers, separated by commas: 1,2 > You want to know the average of the numbers: 1,2 > Traceback (most recent call last): > File "avgInput.py", line 13, in >add = add + i > TypeError: unsupported operand type(s) for +: 'int' and 'str' > > End of process output > > The reason being, I take, that > > numbers = raw_input("Please type the numbers, separated by commas: ") > > also returns the comma (1,2) and thus the for loop can't cope... > So should I therefore retain > > numbers = input("Please type the numbers, separated by commas: ") ? > > Otherwise I don't know (yet) what to do > > David > > > Bill Campbell wrote: > >On Thu, Oct 02, 2008, Steve Willoughby wrote: > > > >>On Fri, Oct 03, 2008 at 01:38:48AM +0800, David wrote: > >> > >>>Does that mean input() is obsolete (after all, Zelle's book is not the > >>>freshest on the shelf)? Or do they have different uses? > >>> > >>Depends on how you look at it. > >> > >>input() automatically evaluates whatever the user types as a Python > >>expression and returns the result. So if they type 5, the integer > >>5 is returned. For your program, that's probably what you want, and > >>has the advantage of letting you type something like 2+3 so your user > >>can let Python evaluate math expressions. > >> > >>On the other hand, you'd think that you could ask a user for a text > >>response using input(): > >> name = input("What is your name? ") > >> print "Hello, ", name > >> > >>But if they just type the answer, Python will crash with an error > >>because it's expecting a legal Python expression there (so a > >>string value would have to be typed in quotes). > >> > > > >Remember the cardinal rule NEVER TRUST USER INPUT! Always check > >for validity, and use methods that prevent malicious strings from > >allowing the user to get unauthorized access or change things > >they shouldn't. > > > >Many of the common exploits of web pages are the result of poor > >checking of input resulting in sql injection attacks, and other > >breaches. > > > >Bill > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
I am not sure how you got from the input to your variable i, it is a good idea to post your code as well. That said raw_input will return the user's input as a string which you then need to convert to integers. So the commas are brought in as well. You can solve this in a couple of ways: First, you can split the string on the commas and get a list of strings each representing one of the numbers. numberlist=numbers.splt(",") will give you: numberslist=["1","2"] which you can then loop over and convert to integers and add up. Secondly, you can have the users input the numbers one at a time inside the loop. add = add + int(raw_input("Please type the next number:")) Chris David <[EMAIL PROTECTED]> Sent by: [EMAIL PROTECTED] 10/02/2008 02:06 PM To tutor@python.org, [EMAIL PROTECTED] cc Subject Re: [Tutor] dealing with user input whose value I don't know Cheers for the insights! However, I just found out that changing input() to raw_input() breaks my code: This program takes the average of numbers you supply!! How many numbers do you want me to work with? 2 You want me to take the average of 2 numbers. Please type the numbers, separated by commas: 1,2 You want to know the average of the numbers: 1,2 Traceback (most recent call last): File "avgInput.py", line 13, in add = add + i TypeError: unsupported operand type(s) for +: 'int' and 'str' End of process output The reason being, I take, that numbers = raw_input("Please type the numbers, separated by commas: ") also returns the comma (1,2) and thus the for loop can't cope... So should I therefore retain numbers = input("Please type the numbers, separated by commas: ") ? Otherwise I don't know (yet) what to do David Bill Campbell wrote: > On Thu, Oct 02, 2008, Steve Willoughby wrote: > >> On Fri, Oct 03, 2008 at 01:38:48AM +0800, David wrote: >> >>> Does that mean input() is obsolete (after all, Zelle's book is not the >>> freshest on the shelf)? Or do they have different uses? >>> >> Depends on how you look at it. >> >> input() automatically evaluates whatever the user types as a Python >> expression and returns the result. So if they type 5, the integer >> 5 is returned. For your program, that's probably what you want, and >> has the advantage of letting you type something like 2+3 so your user >> can let Python evaluate math expressions. >> >> On the other hand, you'd think that you could ask a user for a text >> response using input(): >> name = input("What is your name? ") >> print "Hello, ", name >> >> But if they just type the answer, Python will crash with an error >> because it's expecting a legal Python expression there (so a >> string value would have to be typed in quotes). >> > > Remember the cardinal rule NEVER TRUST USER INPUT! Always check > for validity, and use methods that prevent malicious strings from > allowing the user to get unauthorized access or change things > they shouldn't. > > Many of the common exploits of web pages are the result of poor > checking of input resulting in sql injection attacks, and other > breaches. > > Bill > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
Hello Steve, thanks for all your help and comments. What happens, though, is that with numbers = int(raw_input("Please type the numbers, separated by commas: ")) my code is still defunct (whereas input() works): Please type the numbers, separated by commas: 1,2 Traceback (most recent call last): File "avgInput.py", line 8, in numbers = int(raw_input("Please type the numbers, separated by commas: ")) ValueError: invalid literal for int() with base 10: '1,2' End of process output Here is the entire code: print "This program takes the average of numbers you supply!!" amount = raw_input("How many numbers do you want me to work with? ") print "You want me to take the average of", amount, "numbers." numbers = int(raw_input("Please type the numbers, separated by commas: ")) print "You want to know the average of the numbers:", numbers add = 0 for i in numbers: add = add + i print "The sum of your numbers is:", add average = add / float(amount) print "Therefore the average of your numbers is", average David Steve Willoughby wrote: On Fri, Oct 03, 2008 at 02:06:47AM +0800, David wrote: Cheers for the insights! However, I just found out that changing input() to raw_input() breaks my code: Recall that we told you raw_input() returns a string, while input() returns an integer if you typed an integer value. So you need to convert the string of characters the user typed into an integer value before using it as a number: numbers = int(raw_input(...)) This program takes the average of numbers you supply!! How many numbers do you want me to work with? 2 You want me to take the average of 2 numbers. Please type the numbers, separated by commas: 1,2 You want to know the average of the numbers: 1,2 Traceback (most recent call last): File "avgInput.py", line 13, in add = add + i TypeError: unsupported operand type(s) for +: 'int' and 'str' End of process output The reason being, I take, that numbers = raw_input("Please type the numbers, separated by commas: ") also returns the comma (1,2) and thus the for loop can't cope... So should I therefore retain numbers = input("Please type the numbers, separated by commas: ") ? Otherwise I don't know (yet) what to do David Bill Campbell wrote: On Thu, Oct 02, 2008, Steve Willoughby wrote: On Fri, Oct 03, 2008 at 01:38:48AM +0800, David wrote: Does that mean input() is obsolete (after all, Zelle's book is not the freshest on the shelf)? Or do they have different uses? Depends on how you look at it. input() automatically evaluates whatever the user types as a Python expression and returns the result. So if they type 5, the integer 5 is returned. For your program, that's probably what you want, and has the advantage of letting you type something like 2+3 so your user can let Python evaluate math expressions. On the other hand, you'd think that you could ask a user for a text response using input(): name = input("What is your name? ") print "Hello, ", name But if they just type the answer, Python will crash with an error because it's expecting a legal Python expression there (so a string value would have to be typed in quotes). Remember the cardinal rule NEVER TRUST USER INPUT! Always check for validity, and use methods that prevent malicious strings from allowing the user to get unauthorized access or change things they shouldn't. Many of the common exploits of web pages are the result of poor checking of input resulting in sql injection attacks, and other breaches. Bill ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
Oh, great, this answers my question! Thanks! David [EMAIL PROTECTED] wrote: I am not sure how you got from the input to your variable i, it is a good idea to post your code as well. That said raw_input will return the user's input as a string which you then need to convert to integers. So the commas are brought in as well. You can solve this in a couple of ways: First, you can split the string on the commas and get a list of strings each representing one of the numbers. numberlist=numbers.splt(",") will give you: numberslist=["1","2"] which you can then loop over and convert to integers and add up. Secondly, you can have the users input the numbers one at a time inside the loop. add = add + int(raw_input("Please type the next number:")) Chris *David <[EMAIL PROTECTED]>* Sent by: [EMAIL PROTECTED] 10/02/2008 02:06 PM To tutor@python.org, [EMAIL PROTECTED] cc Subject Re: [Tutor] dealing with user input whose value I don't know Cheers for the insights! However, I just found out that changing input() to raw_input() breaks my code: This program takes the average of numbers you supply!! How many numbers do you want me to work with? 2 You want me to take the average of 2 numbers. Please type the numbers, separated by commas: 1,2 You want to know the average of the numbers: 1,2 Traceback (most recent call last): File "avgInput.py", line 13, in add = add + i TypeError: unsupported operand type(s) for +: 'int' and 'str' End of process output The reason being, I take, that numbers = raw_input("Please type the numbers, separated by commas: ") also returns the comma (1,2) and thus the for loop can't cope... So should I therefore retain numbers = input("Please type the numbers, separated by commas: ") ? Otherwise I don't know (yet) what to do David Bill Campbell wrote: > On Thu, Oct 02, 2008, Steve Willoughby wrote: > >> On Fri, Oct 03, 2008 at 01:38:48AM +0800, David wrote: >> >>> Does that mean input() is obsolete (after all, Zelle's book is not the >>> freshest on the shelf)? Or do they have different uses? >>> >> Depends on how you look at it. >> >> input() automatically evaluates whatever the user types as a Python >> expression and returns the result. So if they type 5, the integer >> 5 is returned. For your program, that's probably what you want, and >> has the advantage of letting you type something like 2+3 so your user >> can let Python evaluate math expressions. >> >> On the other hand, you'd think that you could ask a user for a text >> response using input(): >> name = input("What is your name? ") >> print "Hello, ", name >> >> But if they just type the answer, Python will crash with an error >> because it's expecting a legal Python expression there (so a >> string value would have to be typed in quotes). >> > > Remember the cardinal rule NEVER TRUST USER INPUT! Always check > for validity, and use methods that prevent malicious strings from > allowing the user to get unauthorized access or change things > they shouldn't. > > Many of the common exploits of web pages are the result of poor > checking of input resulting in sql injection attacks, and other > breaches. > > Bill > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Hello again. and another question.
Hello Trask, I have been working on the first program. I got the files to read and print out the grades and averages, but when I write these files to the other txt file, all that I get is: 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 I know it is something simple. I am just not seeing it. Here is my code: Grades = [ ] filename = raw_input("Enter the filename with 10 grades to be averaged: ") my_file_object = open (filename, "r") for a in range (10): temp_string = my_file_object.readline() Grades = Grades + [ float (temp_string)] my_file_object.close() for a in range (len(Grades)): print "Grade", str (a + 1) + ":", Grades [a] total = 0 for a in range (len(Grades)): total = total + Grades[a] average = total/float (len(Grades)) print "The average grade was: ", round(average,2) print "" print "Okay, we are assuming that you have created a file named 'grade_file_2.txt' that is empty, yes?" print "" fname= raw_input("Please enter 'grade_file_2.txt' to write to new file: ") grades_file_2 = open("grade_file_2.txt", "w") for count in range (len(Grades)): grades_file_2.write(str("%.2f"% (len(Grades))) + "\n") grades_file_2.close() print "" print "Nice Job. your file is done." /// I am including the files. Let me know if you see anything goofy. Tom Mundahl tm_week11_problem1.py Description: Binary data 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00 90.50 88.25 98.95 94.90 95.44 96.84 97.99 98.25 99.55 96.88___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello again. and another question.
On Thu, Oct 02, 2008 at 09:41:37PM +, [EMAIL PROTECTED] wrote: > for count in range (len(Grades)): > grades_file_2.write(str("%.2f"% (len(Grades))) + "\n") Look at what you're actually writing for each count. See anything amiss there? -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sample python twill scripts?
On Oct 2, 2008, at 8:21 AM, bob gailer wrote: jeremiah wrote: Just wondering if anyone here would be interested in sharing a python twill script? I'd just like to take a gander at how others are engineering their scripts. twill? Twill is an http client automation tool. It's used quite a bit for performing functional testing from Python. - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello again. and another question.
You're doing a good job getting started with programming here, keep getting the fundamentals and then learn to refine those concepts over time. But for future reference, here are some finer points for you to keep in mind to improve your style and get a more Pythonic approach: On Thu, Oct 02, 2008 at 09:41:37PM +, [EMAIL PROTECTED] wrote: > Grades = [ ] Usually we capitalize class and module names, but not variables and functions/methods. > for a in range (10): > temp_string = my_file_object.readline() > Grades = Grades + [ float (temp_string)] Might be more clear here to say Grades.append(float(temp_string)) instead of constructing a new list just to use + You might also want to just read each line in the file right in the for loop, which allows you to take ANY number of lines, not just 10, and makes the code more concise at the same time: for grade in my_file_object: Grades.append(float(grade)) > for a in range (len(Grades)): > print "Grade", str (a + 1) + ":", Grades [a] If all you're going to do is to deal with each element of a list in turn, instead of using an index over the range of the number of elements, just directly iterate over the list: for a in Grades: print "Grade:", a And one GREAT habit to get into is to always be checking if Python's built in functions and standard library already offer something you're doing manually: > total = 0 > for a in range (len(Grades)): > total = total + Grades[a] > > average = total/float (len(Grades)) average = sum(Grades) / len(Grades) > fname= raw_input("Please enter 'grade_file_2.txt' to write to new file: ") > grades_file_2 = open("grade_file_2.txt", "w") I'm not sure why you're asking them to type in a file name which you're ignoring and opening grade_file_2.txt anyway? > for count in range (len(Grades)): > grades_file_2.write(str("%.2f"% (len(Grades))) + "\n") > grades_file_2.close() No need to explicitly call str() here, and I suspect print would be clearer anyway. And again, note the direct iteration over Grades: for grade in Grades: print >>grades_file_2, "%.2f" % grade grades_file_2.close() -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Multiple windows in Tkinter
2008/10/3 Glen Clark <[EMAIL PROTECTED]>: > What is toplevel? Is it the same as Tk()? Do I initialise a root using Tk() > and then use toplevel for any other windows? How do I switch between the > Windows? And while I am on the subject what is a frame and why should I use > it? atm the moment it just seems to be the same as Tk()? The answer to your third question is: Yes. Toplevel() will create another window that is managed by your window manager (this is Explorer if you are using Microsoft Windows). So you can switch to it using alt-tab or command-` or whatever. You can also programmatically give it focus, although I forget how. (to say that a widget has "focus" means "if you type on the keyboard, this widget gets the keypresses") Frames are containers. The main use for them is to put other widgets into them for layout purposes. For example, you might decide to split your layout into two halves, with some kind of menu in the left, and content in the right. To do that, you would create two frames, pack them left to right, then put the menu into the left frame and your content into the right frame. e.g. here is a short program that puts some buttons in the left and uses them to change the colour of the right-hand frame. (normally you would do something more complex :-) ) from Tkinter import * tk = Tk() # Left frame to hold buttons left = Frame(tk) left.pack(side=LEFT, expand=True, fill=Y) # Right frame to hold display right = Frame(tk, height=200, width=200) right.pack(expand=True, fill=BOTH) # change the colour of the right-hand frame def changeColour(c): def change(): right.config(background=c) return change # Buttons colours = ['red', 'green', 'blue', 'yellow'] for c in colours: b = Button(left, text=c, command=changeColour(c)) b.pack(side=TOP, expand=True) tk.mainloop() -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
"David" <[EMAIL PROTECTED]> wrote However, I just found out that changing input() to raw_input() breaks my code: You want to know the average of the numbers: 1,2 Traceback (most recent call last): File "avgInput.py", line 13, in add = add + i TypeError: unsupported operand type(s) for +: 'int' and 'str' End of process output numbers = raw_input("Please type the numbers, separated by commas: ") numbers = input("Please type the numbers, separated by commas: ") ? What is happening is a bit more subtle than you think. I think!... As others have pointed out input() evaluates whatever the user types as a python expression. Now a list of numbers separated by commas is, to python, a tuple. So your input version stores the list of numbers as a list(ie. a tuple!) whereas raw_input() stores a string containing the list of comma separated values. The solution you have already seen - use string.split(',') to separate the string into substrings and then convert each substring to an integer. As an aside: What you have done is actually quite a user friendly solution but I suspect most programmers would have opted to use a loop. Have you covered loops yet? Specifically a for loop? That would allow you to read in really long lists of numbers and calculate the sum as you go. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
"David" <[EMAIL PROTECTED]> wrote Does that mean input() is obsolete (after all, Zelle's book is not the freshest on the shelf)? Or do they have different uses? They have different uses and input is very convenient at the >>> prompt or when experimenting but in most cases is the wrong choice for 'production'; code because of its vulnerability to exploitation by crackers and other malicious (or unintentionally malign) users. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Multiple windows in Tkinter
"Glen Clark" <[EMAIL PROTECTED]> wrote I am a little confused how multi-windows work in Tkinter. I am currently using it while learning to program. Using it for single window apps/scripts is fine. However what I dont understand is: What is toplevel? Is it the same as Tk()? In any GUI thee is a containment heirarchy of widgets. tk() is the highest level widget in any application. Top Level is the highest level widget in a window which is separate from the main application window. They differ in that you can kill a top level window without killing the entire application but if you kill the Tk based window everything should die! Do I initialise a root using Tk() Yes. Tk is what runs the program. The mainloop() function that organises the events is in Tk() as are several other application evel methods. and then use toplevel for any other windows? Exactly so. How do I switch between the Windows? Usuially the user does that themselves although you can do it programmatically by setting focus. And while I am on the subject what is a frame A frame is just a container for other widgets, it helps you organise your GUI and helps build reusable GUI components by collecting all the bits onto a single Frame. and why should I use it? it just seems to be the same as Tk()? No, they are completely different. A Frame does not have a mainloop or quit function. And Tk does not have the ability to set borders, colours etc. And you cannot create reusable widget collectons using Tk because you can only have one Tk in an appliation but you can have many Frames. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello again. and another question.
<[EMAIL PROTECTED]> wrote I got the files to read and print out the grades and averages, but when I write these files to the other txt file, all that I get is: 10.0 10.0... I know it is something simple. I am just not seeing it. When the output doesn't vary like this you know you must be writing the wrong value or the same value multiple times. I'll add several comments on the code but the solution is in there too... Grades = [ ] filename = raw_input("Enter the filename with 10 grades to be averaged: ") my_file_object = open (filename, "r") for a in range (10): temp_string = my_file_object.readline() Grades = Grades + [ float (temp_string)] my_file_object.close() for a in range (len(Grades)): print "Grade", str (a + 1) + ":", Grades [a] ## # It would be better to just iterate over Grades: for index, grade in enumerate(Grades): print "Grade", index,":",grade total = 0 for a in range (len(Grades)): total = total + Grades[a] ## # same here: for grade in Grades: total += grade average = total/float (len(Grades)) ## # you don't need the float conversion here because you converted them all to floats at the beginning. print "The average grade was: ", round(average,2) print "" print "Okay, we are assuming that you have created a file named 'grade_file_2.txt' that is empty, yes?" print "" fname= raw_input("Please enter 'grade_file_2.txt' to write to new file: ") grades_file_2 = open("grade_file_2.txt", "w") for count in range (len(Grades)): grades_file_2.write(str("%.2f"% (len(Grades))) + "\n") # # Notice you are writing len(Grades) which is always 10 - your error! But... # # and again the for loop, and simplify the output string: for grade in Grades: grades_file_2.write("%.2f\n" % grade) grades_file_2.close() Python for loops are much more powerful when used as foreach loops rather than always using indexing. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How trustworthy are pseudo-random numbers?
Hello list, How trustworthy is the "randomness" generated by the random module? I just wrote a script (with the help of some tutors here!) that finds the largest streak in a series of coin flips. My collected data: 100 coin flips = 6-7 streak (usually) 1000 coin flips = 10-12 streak (usually) 1 coin flips = 15-19 streak (usually) etc etc. I'm curious, how much can I trust the randomness? I can't really do 10 coin flips in real life to compare... -- Alec Henriksen <[EMAIL PROTECTED]> @ http://alecwh.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How trustworthy are pseudo-random numbers?
[Alec Henriksen] > How trustworthy is the "randomness" generated by the random module? Python uses the Mersenne Twister algorithm for generating pseudo-random numbers, and that's one of the highest-quality methods known. You can read more about it, e.g., here: http://en.wikipedia.org/wiki/Mersenne_Twister > I just wrote a script (with the help of some tutors here!) that finds > the largest streak in a series of coin flips. > > My collected data: > > 100 coin flips = 6-7 streak (usually) > 1000 coin flips = 10-12 streak (usually) > 1 coin flips = 15-19 streak (usually) > etc etc. > > I'm curious, how much can I trust the randomness? For this purpose, you can have high confidence -- provided your program is correct ;-) > I can't really do 10 coin flips in real life to compare... The theoretical expected maximum run length is given by equation (15) here, with p=0.5 and n=10: http://mathworld.wolfram.com/Run.html although I'm not certain that coincides with what you mean by "streak". ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor