761 Posted Topics
Re: [QUOTE=gunbuster363;1046437]Here is my code. If I use forloop to write, it got error: Traceback (most recent call last): File "m.py", line 20, in <module> f.write(tag[i].string) TypeError: argument 1 must be string or read-only character buffer, not None HOWEVER, if I don't use for loop, instead write as : f.write(tag[1].string) it … | |
Re: What platform is this? Windows XP, Ubuntu Linux, Mac OSX, etc? There's a number of possibilities here. Most likely the folder you're working in doesn't give you write permissions. | |
Re: In the future, please use code tags when posting code. It will allow the forum members here to read your post easily and will give you answers quicker (and of better quality). Now I will repost your code with code tags so you can see the difference: a=2 b=3 def … | |
Re: I'd look into beautifulsoup if you're looking to break this thing down into an object hierarchy type structure. I haven't used it much, but I know there's plenty of examples on this site of how to implement it. | |
Re: The method [ICODE]os.getcwd()[/ICODE] can only ever return a single string. There's ever only one current working directory, so when you're saying [ICODE]for d in os.getcwd()[/ICODE], you're actually iterating over the string character by character. What you really want is just [ICODE]generators = os.walk(os.getcwd())[/ICODE] | |
Re: [QUOTE=xm1014;1043038]Ah ha! That worked! Now, since you're putting it in a list to sort the values, is there also a way to separate those values you are sorting on into a list by themselves?[/QUOTE] Is this what you mean? [code=python] >>> my_dict = {'a':1, 'b':2, 'c':3} >>> my_dict.keys() ['a', 'c', … | |
Re: [QUOTE=pyprog;1043034]Can you help?[/QUOTE] Why, yes! This case would be a good one to use the dictionary's get method, which will allow you to determine if the key is already in the dictionary or not, and act accordingly. [CODE]def file_to_dict(fname): f = open("file.txt") d = {} for line in f: columns … | |
Re: [QUOTE=pyguy420;1043071] [CODE] if player[0] != "r" or player[0] != "p" or player[0] != "s": print "incorrect choice entered" [/CODE][/QUOTE] Just a minor logic error. This should be: [CODE] if player[0] != "r" and player[0] != "p" and player[0] != "s": print "incorrect choice entered" [/CODE] | |
Re: If you search the forum you'll see this question has been asked a bajillion times. [URL="http://www.daniweb.com/forums/post1041001.html#post1041001"]Here's an example from earlier today[/URL]. | |
Re: [QUOTE=plzhelp;1043305]is it possible to search a multi dimensional list for a certain letter then print out where that letter is? i know u can use list.index() to find a letter but i could figure out how to do it with multi dimensional lists. thx in advance[/QUOTE] Something like this perhaps: … | |
Re: > Anyone know how to take the readlines() blob and split it up into an array? The function `readlines()` already gives you an "array" (it's list in Python). So I guess that part's solved Welcome to the forum Linky. Please, in the future use code tags (or any other appropriate … | |
Re: Create an empty list: [code=python] my_list = [] [/code] Create a list of specified length with all "blank" values: [code=python] my_list = [''] * my_length [/code] | |
Re: You can stop using IDLE and just run your code in the console, which retains the usage of Ctrl+C (I believe that IDLE tries to catch Ctrl+C) | |
Re: [QUOTE=PixelHead777;1037962]It would be nice if the GUI had color-coded parentheses, to show groupings... or better yet, shading of a similar method...[/QUOTE] Why not use one that does then? I use Notepad++, but I know that even python-specific IDEs like PyScripter have parenthesis highlighting, etc. In fact, PyScripter will have a … | |
Re: Here's some examples of converting between strings, lists, lists of strings, and ints using [ICODE]list[/ICODE], [ICODE]str[/ICODE] and [ICODE]int[/ICODE]: [code=python]>>> usr_inp = '123456' >>> usr_inp2 = '1,2,3,4,5,6' >>> list(usr_inp) ['1', '2', '3', '4', '5', '6'] >>> usr_inp2.split(',') ['1', '2', '3', '4', '5', '6'] >>> list(usr_inp)[4] '5' >>> int(list(usr_inp)[4]) 5 >>> str(int(list(usr_inp)[4])) … | |
Re: [QUOTE=ShadyTyrant;1038086]You are using the input() function instead of using raw_input(). [/QUOTE] He also is using parenthesis in his print statements. I'd surmise that he's using Python 3.0, in which case the use of input is the only option. ([ICODE]input [/ICODE]has been replaced by [ICODE]raw_input[/ICODE]) EDIT: Additionally, I tried his code … | |
Re: [QUOTE=laxter17;1038230]And this what I need to do for the third function: The third function (call it "sum3") computes and prints the answer using a "while" loop. In some respects the code is similar to the "for" loop of part 2 above. However, unlike a "for" loop, you have to initialize … | |
Re: [QUOTE=tdeck;1037090][CODE]result = [(key, value)for key in dict.keys(), value in dict.values()][/CODE] ... [CODE]result = [(dict.keys[i], dict.values[i]) for i in range(1, len(dict))] [/CODE][/QUOTE] Just please note that in Python, we use the built-in function [ICODE]dict()[/ICODE] to convert certain objects to a dictionary. If you used the above code, you would lose this … | |
Re: def main(): print "This program illustrates a chaotic function." print "Please enter two numbers between 0 and 1." x = input ("Enter first number: ") y = input ("Enter second number: ") print print "input", x, y print "-----------------" for i in range (8): x = 3.9 * x * … | |
Re: [QUOTE=P00dle;1037538] The beginning of every record looks like this: MSUBUGA JIMSON P O BOX 21273 GABORONE (Obviously they are all different, but always have 3 values on 3 lines.) The end looks like this: P107.17 P0.00 P225.08 P0.00 P332.25 (The numbers always vary, but there are always 5) [/QUOTE] So … | |
Re: [QUOTE=kisan;1036740]IT was quite helpful. so in case of palindromes how can we remove the symbols and capital letters using .upper and replace function?? for example "Madam, in Eden I'm Adam!"[/QUOTE] Use replace to swap out the punctuation for an empty string (''). Then use upper on both the original and … | |
Re: [QUOTE=ihatehippies;1032799]Here's my problem. I wx application that works great until I close it (leaving the idle editing session open) and then restart it.[/QUOTE] Your real problem is that you're using IDLE. Your best bet is to switch to a better-featured (not to mention better-designed) IDE and run your scripts directly … | |
Re: [QUOTE=PixelHead777;1023070]... It won't even import curses.[/QUOTE] That's because curses isn't available for Windows. The documentation should probably specifically say that, but they actually just tip-toe around that fact:[QUOTE]The curses module provides an interface to the curses library, the de-facto standard for portable advanced terminal handling. While curses is most widely … | |
![]() | Re: How about instead of just generating an arbitrary StaticText object, assign it to a persistent member of your class (let's call it [ICODE]self.login_status_text[/ICODE]). Then instead of generating it each time, simply update the contents to the proper string (It's been years since messing with wxPython but if I remember correctly … |
Re: [QUOTE=sneekula;916520]I am still rather new to the Linux OS and need some detailed advice on the installation of Python3.1 on my Ubuntu machine. Python3.1 is not in the Ubuntu package repository yet.[/QUOTE] If you perform an [ICODE]apt-cache search python | grep -E "31|3\.1" [/ICODE] do you get any hits? Alternately … | |
Re: This code has a lot of room for improvement. It would benefit you to read up on [URL="http://docs.python.org/tutorial/classes.html"]classes [/URL]and [URL="http://docs.python.org/tutorial/controlflow.html#defining-functions"]functions[/URL], as well as [URL="http://www.python.org/dev/peps/pep-0008/"]PEP 8[/URL] (recommended Python coding guidelines). Maybe you should challenge yourself to improve the style of your coding and make your program more readable. Also it would … | |
Re: [QUOTE=thehivetyrant;1027256] [code] def getpos(): r1 = x,y = 100,100 r2 = x,y = 800,800[/code] i get the error: [icode][I][COLOR="Red"]Traceback (most recent call last): r1.step(r2.getPos()[0],r2.getPos()[1]) AttributeError: r2 instance has no attribute 'getPos'[/COLOR][/I][/icode] I have defined getPos() for both r1 and r2 (my objects), so why do i get that error? Any … | |
Re: [QUOTE=Megabyte89;1026776]Thanks to anyone who can lend help with this.[/QUOTE] You've simply forgotten to put the [B]value[/B] of [ICODE]results[/ICODE] into the string. Instead you mistakenly typed the name results. Here's how you should use string formatting to add the value: [CODE=python] db.execute("SElECT fname, lname, phone, address, email from contacts WHERE lname … | |
Re: [QUOTE=twoshots;1024183]No, wait, I'm an idiot :) It's x = Stuff(), not x = Stuff of course. Meh.[/QUOTE] You're not an idiot, you're learning! It's good that you've solved your own problem. I actually just read an article about how a programming instructor put a teddy bear on his desk. When … | |
![]() | Re: [QUOTE=sravan953;1027217]Hey All, I have made a program called "Pymailer", I have even made a GUI version... now what I want to do is to convert it to .exe format... how do I do it? I googled for some info, but in vain![/QUOTE] The same way you create any .exe, using … ![]() |
Re: [QUOTE=iamai;1027076]I want to convert an integer to a hexadecimal string examples lets say i have an integer value 1 i want to convert it to 0001 integer value of 16 => 0010 245 =>00F5 and so on the string must have a length of 4 digits[/QUOTE] It will be way … | |
Re: If you're looking for native-looking windows apps you'll want to go with wxPython. This is a Python class wrapper for the wx.widgets toolkit, which is cross-platform and extremely powerful. There's a sticky in this forum with tons of examples of wx code. And [URL="http://wxpython.org"]here's[/URL] where you can download the modules, … | |
Re: The best beginner's language is Assembly code. It will teach you how amazingly convenient a high level language like Python is. That is sarcasm, but just read my response to this post in the other forum | |
Re: [QUOTE=lrh9;1024753]the poorly coded "if x != 1 and x != 2 and x != 3"[/QUOTE] Why yes, that is poorly coded. Good thing this is Python! [code=python] >>> x != 1 and x != 2 and x != 3 True >>> x not in [1,2,3] True[/code] Data validation is extremely … | |
Re: In the if statement you are using w[i+5]. This means that you need to make sure the maximum i plus 5 does not go out of bounds for the string. Let me demonstrate: [code=python] >>> w = 'aabbccdd' >>> print len(w), len(w) - 5 8 3 >>> range(3) [0, 1, … | |
Re: [QUOTE=lrh9;1025020]OK. I tested calling one object's methods from another object like so. ... Must be a bug in my actual program.[/QUOTE] Can you explain what the bug is? Each call worked and printed "This is Test2's method." What was the expected output? EDIT: I think I understand now. You're saying … | |
Re: [QUOTE=simpatar;1025285]Correct, I use python3. I deleted the float infront of the input since it was causing the program to bug. But shouldnt I be able to tell python to make all variables that i put in the input to be floats? if not, how do I make them float nicely?[/QUOTE] … | |
Re: I don't see any reason why you should skip your high school's C++ classes; however realize that your high school career doesn't really impact your job opportunities as much as your college career. Learning how to program is more about a set of skills than a particular language. There are … | |
Re: [QUOTE=nevets04;1022470]Anyone got any ideas?[/QUOTE] [URL="http://docs.python.org/library/cgi.html#installing-your-cgi-script-on-a-unix-system"]This[/URL] should help. Don't ever underestimate the power of documentation. | |
Re: [QUOTE=mohankumar554;1023977]hi, i want to develop small games using python on mac. so which modules /libraries i have to import . and how to proceed to develop the game... plz tell me.[/QUOTE] You'll find it all [URL="http://www.pygame.org/news.html"]here[/URL]. Download pygame, tutorials, code examples, everything. there's also a number of threads about pygame … | |
Re: [QUOTE=python.noob;1022978]though i'm calling pr() function in the subclass after calling the get_details() function(which assigns value to name) it doesn't print the current value of name.[/QUOTE] Actually, it is printing the current value of name. The employee.name is always a space (' '), because you never change it. In your get_details … | |
Re: You're likely getting an exception. When an exception is raised, the program quits. The best way to catch the traceback is to open your own command prompt and run the program that way. Go to Start -> Run... and enter [icode]cmd[/icode] and press Enter. This opens a windows command prompt. … | |
Re: It would be easier for us to help if you gave us just the barebones code that produces this abnormality. Most users won't have every single one of those modules and it would be cumbersome to traverse around to find and install them all. Are you able to see your … | |
Re: [URL=http://tinyurl.com/yk7stjg]Here, try this[/URL] | |
Re: According to your explanation: [code=python]def step(x,y): pass [/code] If you need something else you'll need to explain what you're trying to do and give some context. If you really want some code written for you you'll need to provide some code for us to work from. We can't just automagically … | |
Re: To do it the way you're trying to do it, use this: [code=python] >>> num = 0 >>> mylist = [] >>> >>> while num < 10: ... num += 1 ... mylist += [num] ... >>> for item in mylist: ... print item ... 1 2 3 4 5 … ![]() | |
Re: Alternately you could use modulus division like this: [code=python] >>> f = 123.456 >>> ipart = int(f) >>> fpart = f % 1 >>> ipart 123 >>> fpart 0.45600000000000307 >>> [/code] | |
Re: Or what about using chr() and then just adding 65?: [code=python] >>> def numtolet(num): ... return chr(num + 65) ... >>> numtolet(1) 'B' >>> numtolet(12) 'M' >>> numtolet(14) 'O' >>> [/code] Or the reverse: [code=python]>>> def lettonum(let): ... return ord(let) - 65 ... >>> lettonum('O') 14 >>> lettonum('A') 0 >>> … | |
Re: On top of jice's comment, you don't need to import string, and it would benefit you to move your top-level code into your main function: [CODE=python]def getdata(data): listing = [] for line in data: new_line = line.split()[:2] listing.append(new_line) number = len(listing) items = listing # This is what jice added … | |
Re: [QUOTE=saikeraku;1013164]I never learned the "in" function yet. What if I wanted to use the index code in my code? Like the lucky[i], is there a way to do it? I haven't learned much about python yet, so I wanted to see if I can use what I've learned to finish … |
The End.