2,190 Posted Topics
| |
Re: Some info on what you are trying to do [Reading Files](http://www.freenetpages.co.uk/hp/alan.gauld/tutfiles.htm) [Lists and Dictionaries](http://hetland.org/writing/instant-python.html) or http://www.diveintopython.net/native_data_types/lists.html and http://www.diveintopython.net/native_data_types/index.html#odbchelper.dict See if this code helps get you started f = open('profiles.txt', 'r').readlines() for line in f: line = line.strip() print line if "," in line: line_as_list = line.split(',') if len(line_as_list) > 1: … | |
Re: > Traceback (most recent call last): File "compundids.py", line 13, in<module> record = Entrez.read(handle) There is something wrong with the read. Does your input file have any empty records? Add a print statement to print "line" before the error to see which record is causing this. | |
Re: > I want to either figure out how to open the individual python files when I click on a button That is too much code to go through, but you would include the "open the file" code in the call back function. ~~~ try: import Tkinter as tk ## Python … | |
Re: We don't have the code that creates "maze" so have no idea what kind of object it is, but you should access it the same way whatever the object. if maze[row-1,col] == 'O' or 'F': --> this doesn't make sense to me and will always be "True" if maze(row,col) == … | |
Re: [Dive Into Python](http://www.diveintopython.net/scripts_and_streams/command_line_arguments.html) has a good command line tutorial IMHO. | |
Re: When I run the code, it results in this error > File "./test_1.py", line 110, in <module> l,w=eval(input("Please enter length and width of room #"+str(x))) TypeError: eval() arg 1 must be a string or code object eval() is used to evaluate formulas or code so is not appropriate here and … | |
Re: > the problem is when i click the vnc button, this app has own windows - for ask password and show messages like "unable to connect" - and this stay under the fullscreen main window. You can use focus_set, i.e self.parent.focus_set(), self.password_entry.focus_set(). Start by reading [this page](http://effbot.org/tkinterbook/tkinter-dialog-windows.htm) | |
Re: Receive the variables as a tuple and then use each item as your variable. [code]def test_num_variables(*args): print "number of variables =", args, type(args), len(args) os = -1 if None in args: os=args.index(None) print "finding None =", os, args[os] else: print "None was not found" return ## assumes that the check … | |
Re: >So how would I replace "-" with the correctly guessed letter There are 3 containers here, the "secret word", the hangman list, and the letter quessed. You would iterate over the "secret word" and compare letters. If the letters match then replace the "-" at the same postion in the … | |
When clicking the "Edit" button on an existing post, you are presented with the post and just an "Edit Post" and "Cancel" button at the bottom, neither of which post the edited version. Is there a FAQ for the new methods? If not then there should be. Also posting a … | |
Re: If you want to use split (and how do you post code that is not indented, like the following. If we have to add 4 extra space to every freaking line of code that we post, I'm finding a new forum) test_line="Youngstown, OH[4110,8065]115436" # your way for item in test_line.split(): … | |
Re: Start by testing your functions. This is very basic stuff. So in the function "table", print tableString at the end. Also you should be getting a syntax error on this line if name = 'main': Try [one of the many tutorials](http://hetland.org/writing/instant-python.html) on the web. The correct way for "name = … | |
Re: [quote]I need to find the records that contain duplicate <id> tags.[/quote]Which duplicate records do you want to delete, the first or last? In any case, with a 3GB file you probably want to iterate over the file one record at a time instead of trying to read it into memory. … | |
Re: Run this code with some added print statements and see if it helps. [CODE]for i in range(n): y = input("Enter number: ") print "after input y =", y y = y + y print "after adding y =", y, "\n" [/CODE] | |
Re: Copied to preserve the code tags. [code]def program(List, Name, Detail): for entry in List: if entry[0] == Name: ## changed to add to the entry containing "Name" not ## entry[1] as it is [Detail, Counter] so would become ## [Detail, Counter, [Detail_2, Counter_2]] entry.append([Detail,Counter]) ## "Counter" has not been declared … | |
Re: For future reference there isn't much response for someone who can solve there own problem with a few print statements. This will get you started but there are errors in your logic. [code]for x in Alpha: y = 25- count print "first x, y", x,y while x in I > … | |
Re: [code] if maxLength(num) > MAXLEN: MAXLEN = maxLength(num) MAXNUM = num print ("\n%d has the longest chain of %d\n") % (MAXNUM, MAXLEN) [/code] The maxLenth function does not process the number more that once because of the "return num" statement, so the largest number will always be at the end … | |
Re: Usually import statements are at the beginning of the program, and you then call a function within the imported program. [code]import _level1 elif choice in ['go north', 'Go north', 'Go North', 'n', 'N', 'north', 'North']: print " " _level1.function_to_run() [/code] | |
Re: That can be a permission problem. Make sure that user has access to program file and the directory it is in. | |
Re: [quote]i[I]'ve managed to read in the file, and store it as a dictionary,[/quote]Probably not in the way you think. Print the dictionary and see what it contains. The dictionary key is the respondant's number, not the question number so at best you can count how many questions the respondant voted … | |
Re: Use a class and instance variables. You can call separate functions within the class using multiprocessing and they will all have access to the instance variables. | |
Re: Use a list [code]some_dict={} for key in ["a", "b", "a", "c", "c"]: if key not in some_dict: some_dict[key]=[] some_dict[key].append(key) print some_dict [/code] | |
Re: Also, you should check that numbers were entered on input and ask the use to enter again if there was an error. The program now will error out if you try to eval an entry that is not a number. | |
Re: Start with the simple basics; print big_list and make sure it contains what you think it does and is in the form you expect to be in. You can also add a print statement after for item in big_list: to print the item, but it is basically the same thing. | |
Re: Print some of the details, like the date. [code] line_of_list = line.split() date_port = ' '.join(line_of_list[0:2]) ## month and day date_list = date_port.split(':') ## no ':' in date_port [/code]And has_key is deprecated so use "in" instead. [code]## if desc_ip.has_key(ip_address): if ip_address in desc_ip: [/code] | |
Re: First, you use the button variable for 2 different things [code] button1=Button(win,Point(2.5,2.5),3,2,"Button 1") button1=1 [/code]It can't be both at the same time. Next, find a tutorial for the graphics module so you know how to spell the text widget, and how to add it to the window. Next, each of … | |
Re: This should give you an idea. There is also a problem with get_code(). You should first test what you post as much as possible. [code]def decode(code_dict,message): coded_message = [] for ch in message: if ch in code_dict: coded_message.append(code_dict[ch]) else: coded_message.append(ch) print "".join(coded_message) decode({"t":"x", "b":"y"}, "the quick brown fox") [/code] | |
Re: IMHO the only time to use the keyword "is" is when you want to test that 2 objects point to the same block of memory, otherwise you can get unexpected results. This is a simple example of equal returning a True and is returning a False for the same comparison. … | |
Re: The only time you can do multiple things at once is when you use a tool for that, like multiprocessing or parallelpython, otherwise you have to do things one at a time. | |
Re: The else statement will execute for all input except 'n'. That includes upper case 'N'. You want something more along the lines of the following. Also, use a while statement to call the function multiple times. When level() calls itself you can reach the recursion limit. [code] choice = choice.lower() … | |
Re: There are two ways to do this. 1. Use a list of lists. The inner list contains the cut off grade and an int which is incremented each time a grade is found in each category. You can loop through the grades once and find the first grade in the … | |
Re: .join() is more efficient than string concatenation, so I would expect to see at least one join(). Also, don't use "i", "l", or "O" in an interview. [code]for outer in range(1,13): print '\t'.join(str(outer*inner) for inner in range(1, 13)) [/code] | |
Re: Convert them to sets and use the intersection. | |
Re: First, test that row1 < row2 and the same for columns. Also you should test for them being in the correct range. Then you can iterate using a for loop for rows and a sub-loop for columns and this code assumes that "board" is a list of lists. [code]def is_occupied(row1, … | |
Re: Replace this [CODE]def show(comp_board): print """_____________________________________________ |===|_0_|_1_|_2_|_3_|_4_|_5_|_6_|_7_|_8_|_9_| |_A_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|""" %(comp_board[0][0], comp_board[0][1], comp_board[0][2], comp_board[0][3], comp_board[0][4], comp_board[0][5], comp_board[0][6], comp_board[0][7], comp_board[0][8], comp_board[0][9]) print "|_B_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|" %(comp_board[1][0], comp_board[1][1], comp_board[1][2], comp_board[1][3], comp_board[1][4], comp_board[1][5], comp_board[1][6], comp_board[1][7], comp_board[1][8], comp_board[1][9]) print "|_C_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|" %(comp_board[2][0], comp_board[2][1], comp_board[2][2], comp_board[2][3], comp_board[2][4], comp_board[2][5], comp_board[2][6], comp_board[2][7], comp_board[2][8], comp_board[2][9]) print "|_D_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|_%s_|" %(comp_board[3][0], comp_board[3][1], comp_board[3][2], comp_board[3][3], comp_board[3][4], comp_board[3][5], … | |
Re: You can simplify this as follows (continue and sometimes break often signifies a better logic exists). [code]import random #i [I] think you wanted strings here #r = "rock" #p = "paper" #s = "scissors" #a list of the weapons weapons = ["rock", "paper", "scissors"] ## separate variables are not necessary … | |
Re: You have to know what you are coding before you can write the code. What solution are you trying to write. Just throwing code against the wall to see what sticks doesn't work. If you draw it out, it becomes somewhat clear. Hint: it has to do with the length … | |
Re: You did not state any error so there is no question to answer. In Python we generally use the "in" operator instead of iterating. Also, sets will allow you to get the difference between string2 and string1 which is what you want.[code]# Pseudo code list_2 = [] for ch in … | |
Re: Key in "python first last middle name" in the Daniweb search box. You are one of many asking this question. | |
![]() | Re: Data length is data length and will be the same no matter where you split. If you read one record at a time instead of the entire file it should not be a problem. More info is necessary though. What code are you using and how big is the file. … |
Re: For starters you can almost always use a list or dictionary instead of many if/elif statements. Also, globals are not necessary for this problem and point to a lack of understanding. [code]## replace of of these if/elif with a list of lists """ if amount = 4: riskCode = 1 … | |
Re: You have to return the list that stores the textvariables and pass it to the next function. Also, you should really be using a [url=http://www.diveintopython.net/object_oriented_framework/defining_classes.html]class structure[/url] for this. [code]from Tkinter import * from functools import partial data = [ ('a', 0, 'liters soda'), ('b', 0, 'liters beer'), ('c', 0, 'liters … | |
Re: You don't test for distance becoming negative (which is also a "True" condition-only zero is False) and you want to count the number of steps. As it is now, "steps" will always be slightly greater than 1000 since you add steplen every time. Figure it out by printing where necessary. … | |
Re: There isn't enough data to be exact on the extraction process, but you can use a dictionary to test for "100", "245", etc. and call a function based on the key. I have used a common function to extract the data. If this is possible on all data types then … | |
Re: If you have 5 rooms shaped as a plus sign, with room #5 in the middle, you would create the relationships with a dictionary as follows. [code]def room_function(room_no): print "running function for %s" % (room_no) """ room #1=North clockwise so room #4=West and room #5=the middle rooms_dict is keyed on … | |
Re: You would substitute a record from a file in place of the raw_input and send it to the function named "change". [url=http://www.freenetpages.co.uk/hp/alan.gauld/tutfiles.htm]Opening and reading files[/url]. | |
Re: You really only need to use the first and last space, but count can be used to find how many spaces to index. [code]fullname = "one two three four five" ct = fullname.count(' ') idx=0 idx_list=[] for ctr in range(ct): idx=fullname.find(" ", idx) idx_list.append(idx) idx += 1 print idx_list first=fullname[:idx_list[0]] … | |
Re: Isn't "a boad" a place where you live (sorry couldn't pass that up). An easy to understand example follows. You will have to substitute the variable for the hard-wired ">", and come up with some decent code to input things, as well as "or print Invalid board! if the board … | |
Re: Vegaseat has a nice example in the "Python GUI Programming" sticky, but I refuse to be someone else's research assistant. If memory serves it is somewhere in the middle of the thread's pages. |
The End.