404 Posted Topics
Re: Thanks! The question and answer were both helpful. Jeff | |
Re: [quote] Maybe somebody could give an example of how to pass variables between classes without using global variables. [/quote] You might be asking one of three things: (1) How do I get the user-supplied info into my particular class object? In which case, you simply attach the info either in … | |
Hi, I hope this is the right forum to post this. Anyways, I have greatly enjoyed and profited from the forum, so thanks to all who make it work. I have one small quibble that would be lovely to address if it's possible. There are currently code and php tags … | |
Re: Well, in general, you should validate your input. What that means is making mathematically sure that the user enters a correct something, or else the program prints an error and continues on with life. That's what the first code snippet above does: if the user didn't type a (non-negative!) integer, … | |
Re: I think your code is actually working correctly, if I'm understanding what you want to do. The only thing is that you needed to print the result of fib(n): [code] def fib(n): print "Computing fib:", n if 0 < n < 3: value = 1 print "Returning value: ", value … | |
Re: That seems like an extreme solution. I have no experience with py2exe, or I would offer to help ... but could perhaps one of you experts turn sharky's code into a .exe and send him the file in a private message or something? Just a thought, Jeff | |
Re: I would suggest using a dictionary for the grades, for two reasons: (1) It'll make your life a lot easier (see code below) (2) Your code currently contains a bug that will allow a silly or malicious user to get weird results or break the code. [code] if grade_str not … | |
Hi all, I know there have been several threads about getting card images into a GUI, but I'm flustered trying to understand why some things work and some things don't. Here's the code: [php] import random from Tkinter import * import Image, ImageTk class Card(object): suits = ["C","D","H","S"] values = … | |
Re: > HI, > > I NEED TO MODIFY THE PROGRAM BY HAVING IT PRINT TRACING INFORMATION. IN OTHER WORDS THE OUTPUT SHOULD LOOK SOMETHING LIKE THIS: > > Computing fib(4) > ... > Leaving fib(4) returning 3 > > THIS IS WHAT I HAVE COME UP WITH SO FAR: (I … | |
Hi all, In the never-ending quest to master Tkinter, I wrote a small version of Notepad. Here is my "SaveAs" function: [php] def save_as(): if not mainw.filename: mainw.filename = tkFileDialog.asksaveasfilename(title="Save As...", filetypes=[("All Files","*.*"),("Text Files","*.txt")]) if not mainw.filename: return f = open(mainw.filename, "w") s = mainw.frame.edit.get("1.0",END) f.write(s) f.close() [/php] As you … | |
Re: If I'm understanding correctly, you want to divy up the countries evenly, but randomly, between the firstTurnsCountries and secondTurnsCountries. The key, I think, is to remove the countries from the list as you choose them. Thus: [php] availableCountries = range(8) # [0,1,2,3,4,5,6,7] fiirstTurnsCountries = [] secondTurnsCountries = [] tmp = … | |
Re: If you want to use ASCII art for the game, then Python command-line ought to be fine. You can simply call your program blah.py and double-click its icon, and it will pop up a DOS-like window with output and all. Is that what you were asking? Jeff | |
Re: I'd like to know... Thanks, Jeff | |
Re: [quote=Haze;283794]when i say basic i mean basic it doesnt store the encrypted string in a file or anything it just prints out what it would be and all i intend it to do is swap a for b, b for c and so on then turn the string round backwards … | |
Re: I think the indent requirements serve two purposes. One you have already mentioned: making blocks more human-readable. The other purpose is pedagogical, remembering that Python began as a teaching language. By having to indent his blocks, the beginner is forced to see code in blocks. Jeff | |
Re: These are just general suggestions, but I hope they will be helpful rather than leading you down a wrong path. (1) Creating a card class will solve a lot of problems for you. You can do something like this: [code] class Card(object): images = {0:"ace_spades.bmp", 1:"two_spades.bmp"} # etc. strings = … | |
Re: Sorry about your HDD. :( [quote] Text File [code]Barnsley,Dronfield,1.70[/code] When it gets to a , I want it to take the characters in the first block, and assign it to Variable (Var1a) and then go to the second one and do the same for Var2a, then do the following: [code]Check … | |
Re: I'm confused. Couldn't you do this: [code] btn1 = Button(root, text="Play", command=show_images) [/code] and then [code] def show_images(): show_image2() show_image3() [/code] Jeff ![]() | |
Re: Actually, it's OK that dictionaries aren't sorted. The real thing that we sometimes want is to be able to access the values in a sorted manner. Here's how: [code] k = d.keys() # or, k = [x for x in d] k.sort() for i in k: print d[i] # or … | |
Re: or for the literal match, [code] if not list: # condition will be True if 'list' is any of 0, [], "", (), etc. do(this) else: do(that) [/code] | |
Hi all, We just hit functions, and I assigned my students to write ten short functions to do things like convert Fahr. to Cels., etc. My "brilliant" :rolleyes: plan was to create a test suite and have them import their file and run the tests. Because the filename wasn't absolutely … | |
Re: [quote=sneekula;274483]I keep reading about endless loops, is that something you want or do you always have to control an endless loop situation?[/quote] Your operating system runs in an endless loop. :) Truly endless loops are obviously bad news (i.e., shut off the computer and restart). However, most "endless loops" usually … | |
Re: If the data are packaged as neatly as your file shows, then I would recommend using .split() and then eval() to convert the parenthesized string to a tuple: [php] f = open("my_data") colors = {} for line in f: line = line.strip('\n') name, value = line.split(' RGB ') colors[name] = … | |
Re: [quote=sneekula;271696]I think I am getting a feeling in which case to use 'for' or 'while'. Never looked at a recursive fuction as a loop, but I guess it is and can be used for that purpose. Almost looks like a limited goto! With the loops there is also continue, break … | |
![]() | Re: If you're talking about Newton's Method to find a zero of a function, I recommend the following: [code=python] def deriv(f , c, dx = 0.0001): """ deriv(f,c,dx) --> float Returns f'(c), computed as a symmetric difference quotient. Make dx smaller for more precise results. """ return (f(c+dx) - f(c - … ![]() |
![]() | Re: Recommendations: (1) BugFix: [php] # strip newlines #old code -- might snip the last line in the file if no newline present for line in schedule_data: del line[-1] [/php] [php] # new code -- removes trailing '\n's *and* empty lines all at once! schedule_data = [line.strip('\n') for line in schedule_data … ![]() |
Re: I have code that attempts to do the same, but it reveals a couple of discrepancies. I'm wondering whether my code is wrong, or if perhaps there's something in your code, LaMouche [php] # create test file f = open("file.txt", "w") f.write(\ """name1 surname,1,0,0,0,0 name2 surname,0,1,1,0,0 name3 surname,0,1,0,1,1 name4 surname,0,1,1,0,0 … | |
So I wanted to write some code to provide a framework for math quizzes. Among other things, I wanted to be able to show LaTeX-like output on a canvas. But first, I just wanted to get the canvas showing simple text. Part of the code: [php] def get_level_string(self): return self.f1.level_text.get() … | |
Re: [code] [COLOR=#000000][COLOR=#0000bb]int[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]str[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]x[/COLOR][COLOR=#007700])[::-[/COLOR][COLOR=#0000bb]1[/COLOR][COLOR=#007700]]) [/COLOR][/COLOR][/code][COLOR=#000000][COLOR=#007700] What the ... I never knew you could do that!!! LOL! Jeff Testing results: [php] >>> s = "my test string" >>> s[::-1] 'gnirts tset ym' >>> s[::2] 'm etsrn' >>> s[::-2] 'git sty' >>> [/php] :cool: [/COLOR][/COLOR] | |
Re: Also, if I'm debugging code that I want to run in a separate window, I first open it with IDLE and either Check Module (Run --> Check Module or Alt-X) or just Run it (F5). IDLE gives me *much* more info about my errors than just having my code start … | |
Re: [quote="vegaseat"]They are used by folks familiar with C/C++ programming, where passing arguments, particularly multiple arguments are a possible pointer nightmare.[/quote] Although, I was taught to avoid globals when I learned C back last century. Jeff | |
Re: Yes, returning a tuple is considered the standard way to do it. | |
![]() | Re: I make my students learn both interpolation (%) *and* comma syntax, because both are useful. The % is my preferred way of printing numbers, floats, and strings because (a) It allows you to print without annoying extra spaces, and (b) It allows formatting, as mentioned above. The following output is … ![]() |
Re: Matt: If your song is MIDI data, then it should be a trivial solution; I believe that MIDI files encode their tempo as metadata. If your song is in MP3 or .wav format, then you are going to have to scan the data for amplitude peaks or patterns, which is … | |
![]() | Re: [quote=Ene Uran;262395]Each time you move s, n, e, or west heal the monster(s) a little. That would be in your while loop's if command == 's' etc.[/quote] Right. I would probably have a Maze class, Room class, and a Monster class, and then maintain a dictionary like this: [php] class … |
Re: Actually, Tkinter has a 'show' option that allows you to specify which character shows in the box. I've used it for password boxes: [code] my_entry["show"] = "*" [/code] Jeff | |
Re: Right. Python does not allow the use of unbound methods like MyClass.method() Instead, it always requires my_inst = MyClass() my_inst.method() The reason I got confused by these things is that we can do things like import random random.randint(5) which appears to be an unbound method. However, it's actually just a … | |
Re: I've done some of each, and I'm loving learning Python. It cleans up the thought processes considerably. Jeff | |
Re: I scanned through the bug list, and all of the ones that I saw were really obscure. None of them were core language functions; rather, they seemed to be module issues. However, I didn't give it a thorough lookover... Jeff | |
Hi, I've written a nifty projectile simulator using pygames with the livewires wrapper for physic class: rocket on launch pad has assignable speed and direction, launching causes rocket to move under influence of gravity. Currently, the user can use the mouse to change the initial direction of the rocket, but … | |
Here is the code that burned me today. I'm re-writing my Sudoku solver to make it more "Pythonic" -- it was my first Python project, and the old code looks like translated C :lol: -- anyways, at one point, I need a dictionary to hold the possible locations in a … | |
Re: If you want to do a recursive search, look up os.path.walk(). If you just want a shallow directory listing, try os.listdir(). Regards, Jeff | |
Hi all, What are your thoughts about how many and which type of errors should be caught and handled within functions, especially roll-your-own utility functions? For example: suppose I have a function that removes a directory and all subdirectories and files underneath it. Should that function test for path existence? … | |
Re: I second Vegaseat on that. I looked into the issue once in my pre-Python days and ended up with 300+ pages of documentation on OLE files. Basically, a .doc is stored not only with the text, but also with the entire edit history. My understanding ... someone correct me if … | |
Hi all, I am testing a somewhat large module, and I would like to automate the testing of the documentation (I know, I could just look at the code, but I wanted to make sure *and* the question became a cool one :cheesy:) Here's the basic idea: [code] for i … | |
OK, this is a minor quibble from someone who is not *normally* concerned with neatness. I want to put docstrings in my functions. If I put them in using the 'textbook' method: [code] def is_same(self,x): """A.is_same(x) --> Bool\n\nReturns True if A and x represent essentially the same item, as determined … | |
Re: Pascal's triangle is most easily done using recursion, IMO. | |
A friend of mine just introduced me to the Eclipse IDE, which may or may not end up replacing IDLE for me. Anyway, one of the features of Eclipse is error warnings as the code is *displayed*, rather than waiting for errors to pop up at runtime. This code got … | |
I'm befuddled by something. Here is transcript of the interactive session: [code] >>> range(9).remove(3) >>> >>> print range(9).remove(3) None >>> a = range(9).remove(3) >>> a >>> a = range(9) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8] >>> a.remove(3) >>> a [0, 1, 2, 4, 5, 6, … | |
Hi, I'm still trying to straighten out Tk in my mind. :eek: What I want to do is, under certain circumstances, completely clear the screen and display something new. Instead, it displays the new stuff together with the old. Here's excerpts from the code, with the offending line marked with … |
The End.