2,190 Posted Topics
Re: What exactly are you trying to do? It is not obvious from the program. There are other problems in addition to indentation: while 1>0: The above is an infinite loop that you never exit. You open the file '/var/log/kern.log' at least 3 times without previously closing it. You mix pack() … | |
Re: Start with a tutorial on MySQL for Python, as your code does not contain enough to comment on. For starters, you don't ever call the function connect_to_database, and the function itself does not connect to any database. Start with opening a database correctly and inserting records, which is in every … | |
Re: > I keep getting the error: "TypeError: expected a character buffer object" We can't help without the complete error message; which tells where the error is in the program among other things. a[i].lower() Take a look at this page http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/strings3.html beginning at the paragraph that starts with "Strings are immutable, … | |
Re: It appears that the classes are part of the cause of your confusion, so since you do not use them, I have just deleted them and the code runs until it encounters the next error. Note that you will have a problem if you try to split anything after line … | |
Re: And generally, the input is separate from the rest of the program. def get_input(): option = 99 while option not in [0, 1, 2, 3, 4]: print("0 - clear out scores") print("1 - input more scores") print("2 - display all scores") print("3 - get average scores") print("4 - end program") … | |
Re: Possibly I to L = 3 letters, L to P = 4 letters 5 to 9 = 4, 9 to 14 = 5 so I 9 P 20 =6+14 5 L 14 U =P+5 letters | |
Re: > It gives me that error am I doing something wrong We don't know what the error is so there is no way to answer other than to ask what happens if nothing is found in the data base. Try printing the look up before adding to the combobox. | |
Re: Use "SELECT" and "WHERE" to only return record(s) that match from the SQLite database [See "Named Placeholders"](http://zetcode.com/db/sqlitepythontutorial/) | |
Re: Start by getting the input from the user, i.e. Name, number of hours, etc. and then work out the math on paper and convert to Python. Post back with problems. | |
Re: FWIW, you can also use divmod instead of two statements (divide and mod), but I doubt that this code would be accepted from a beginning student, and only prints the plural form of the words. x = input('Enter the before-tax price (in cents): ') y = input('Enter your painment (in … | |
Re: > So this is a case of "two wrongs make you write"? Ba dum bum... I will have to use that somewhere myself. | |
Re: To end the game if there is a tie, set the last "while True" to some varialble instead that goes to False when there are no more empty spaces, like empty_space=True while empty space: input = raw_input("Select a spot: ") input = int(input) ## rest of code empty_space=test_for_empty(board) To get … | |
Re: Your list contains "option 1", etc, not "1". options = [ 'option 1', 'option 2', 'option 3' ] while True: playerOpt = int(input('input ')) list_test = "option %d" % (playerOpt) if list_test in options: break ## exit while() print("\n Not a valid entry\n") | |
Re: > the checking starts with 0 (list indeces start with 0) Lists don't have indexes, lists use offsets. So "some_list[5]" means offset 5 from the beginning, or skip over the first 5 elements=beginning of the sixth. It comes from the beginning days of computers when programmers had to do everything … | |
Re: Try splitting the original on the newline and working with the resulting list. split_str=orig_string.split("\n") | |
Re: You should also get errors for n = raw.input("Terms? ") and for b = m * m + m first_term = ( ( n * n + n ) / 2 ) - 1 and the same as the above comment if m != 0: will always be true the … | |
Re: Just to see what it would take, modifying Ene's code to draw a right angle triangle of unknown dimensions is just a little more tedious import turtle as tu import random tu.title('draw a right angle triangle') # set at 50% of original size tu.setup(width=0.5, height=0.5) tu.up() tu.goto(60, 100) tu.down() # … | |
Re: There are many examples on the web so no reason to duplicate answers here. Start with this link: http://www.daniweb.com/software-development/python/code/216557/turtle-graphics-python | |
Re: > But this doesn't do exactly what I want... anyone help? How are we supposed to know what this means? In general, you want to compare words to words. Your code will also find sub-words, ie. "the" is in "then" and "and" is in "random", so use == instead for … | |
Re: It depends on what is in "letters". If your OS is set to ASCII then the latin1 characters will not be found. | |
Re: You would use len() print sline[5], type(sline[5]), bool(len(sline[5])) or eval(), although eval will do anything, so if someone puts "rm *" in the file that is being read, eval will delete the files. for lit in ["False", "True"]: x = eval(lit) print x, type(x) | |
Re: > I can't seem to find a solution for writing the data into a .csv format where all keys and values are separated by a delimiter, e.g. 11,2.5,2.7,32.6 ? Perhaps it would be a better idea to write it out into a formatted .txt file instead? Using a txt file … | |
Re: Note that the list you posted is not a list of tuples. You can use the older method to do this, although it is slower. def return_lower(x, y): return cmp(x[0][1:], y[0][1:]) test_list=[('a070918_215953.SFTC', '1369.000'), ('a070917_220602.SFTC', '1369.000'), ('a070915_221210.SFTC', '1369.000'), ('r080323_044006.SFTC', '1369.000')] print sorted(test_list, cmp=return_lower) | |
Re: Simple debugging will point out the error. Try running the code with these additional print statements. def occurrence(e, input_list): for item in input_list: print "checking" item, e ## test for checking every item s = 0 if item == e: print " found, s =", s ## this will clarify … | |
Re: > I ran into some trouble today while trying to launch idle from it from tkinter import * For starters, you should not run apps that use Tkinter from Idle since Idle is written in Tkinter. The results are unpredictable, so try again without using Idle and post back if … | |
Re: Giving my niece and nephews the old "we had it tough back in my day" routine, I told them that it was so tough that if we wanted to change the TV channel we had to get up off the couch, walk across the room, and turn the knob. They … | |
Re: Generally you pass as arguments like this simplified code:[CODE]## program run.py def print_args(name, passwd): print name print passwd ## calling program import run input_name = raw_input("Enter name ") input_passwd = raw_input("Enter password ") run.print_args(input_name, input_passwd) [/CODE] | |
Re: Use os.path.isdir() and os.path.islink() to identify which is which and then compare. As far as splitting goes, I would use something along these lines import os test_path="C:/Pyth/Shortcuts/1000.lnk" first, second = os.path.split(test_path) print first, second print second.split(".") | |
Re: You would bind <Button-3> to the item [Click Here](http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm). For whitespace, you would have to test event.x and event.y to see if you are over white space or not. This uses a left click to determine the location (event.x, event.y) to draw the snowman, but the principle is the same. … | |
Re: Test for length after you strip the line, and note that "new" is already used by Python. for line in keep: if len(line.strip()): new_fp.write(line) | |
Re: Generally you pass the value to the function as you want the code "encapslated", i.e. the class does everything. class Foot: def __init__(self,mov,posx,posy,targx,targy): # self.mov is soldier's movement speed, currently not implemented self.mov = mov # self.x and self.y are soldier's position self.x = posx self.y = posy # self.targx … | |
Re: What does happen? You don't know, so add a print statement to see what is happening def main(): List=[] infile = open ("words_dos.txt","r") words = infile.read() wordList=List.append(words) print "wordList =", wordList Note the difference between read() and readlines() [Click Here](http://docs.python.org/tutorial/inputoutput.html) Also, take a look at the "functions" heading [Click Here](http://hetland.org/writing/instant-python.html) … | |
Re: ln has a -f (force option). See man ln. Also, try this instead as it may not be a symlink [CODE]if not os.path.exists(path):[/CODE] | |
Re: You have two instances of Tk running which is never a good idea. Use a Toplevel instead. The following is a very simple example. But it will not solve the problems. The problem is that you have somewhere around 100+ lines of code that have not been tested so you … | |
Re: I would use a dictionary with the category as key, and a list of all words in the subset as the value [QUOTE=jancho1911;717752]Let V be the vocabulary of all words in the documents in D For each category ci ----->ci=dictionary key Let Di be the subset of documents in D … | |
Re: If you have a <b> then you should also have a </b>, etc. Doing this yourself usually involves a split() and iterating over each item, and a good habit to form is to try to iterate over the text as few times as possible, which means looking for each tag … | |
Re: UpperCaseSentence = (myList[myString]) UpperCaseSentence is a tuple that only contains the final item in the list "myList", as you have written it, which is an empty string if the text that is entered ends with a period. mySentenceList = mySentence.split('.') Add some print statements and see for yourself. Also, as … | |
Re: There is no way to tell without a complete error message which the contains the errant line. Also, do you instantiate the class and then call the setupUI function with a parameter? As the code stands now, nothing will happen even if the code does not contain errors. | |
Re: The datetime module will do micro-seconds [code]import datetime print datetime.datetime.now() time_now = datetime.datetime.now() print time_now[/code] | |
Re: You should test for whatever in the program, and if true then call the function or class in the imported module. As stated previously, this is a design problem not a programming problem. A simple example from you would help. | |
Re: You read the file into a list but write the file as one string. Instead write one tuple per record. And delete loadTextString() as that is just confusing things. [code] save_text(listOfOldScores, "high scores.txt") # def save_text(scores_list, file_name): file_handler = open(file_name, "w") for tup in scores_list: file_handler.write("%s, %d\n" % (tup[0], tup[1])) … | |
Re: You have the range endings reversed and you want to start at +1 on the inner loop. ~~~ for x in range(0, len(a)-1): for j in range(x+1, len(a)): ~~~ Also you swap every time there is a difference instead of once per loop. ~~~ import random a=[random.randint(1, 100) for ctr … | |
Re: [Dive Into Python](http://www.diveintopython.net/toc/index.html) covers all aspects, but if you are beginning there are other tutorials at the [Python Wiki](http://wiki.python.org/moin/BeginnersGuide/Programmers). | |
Re: Since it is a count, fetchone will not return anything. You could also fetchall, on a straight select, and check the length of the returned list if that is more straight forward. | |
Re: You call randomNumGenerator() twice, once in each of the two functions and so are working with two different values (althought it is possible to return the same value twice). If you are going to tutor then you should try to follow the [Python Style Guide](http://www.python.org/dev/peps/pep-0008/) and stop using the half-camelCase … | |
Re: ~~~ if color in "rgb" and color != "": ~~~ if color is in "rgb" it can not be equal to "" or any other character, so this is redundant. | |
Re: You also have to convert from strings in the file, to integers. A read and convert example ~~~ test_data="""1 4 5 3 1 0 2 4 18 5 2 0 0 9""" # ##f = open(inputfile, "r") f = test_data.split("\n") # output_list = [] for rec in f: numbers_as_strings = … | |
Re: Your program works fine for me, after correcting some indentation errors, on Python2.7. Show us what the program prints. ~~~ def enter_names(): fn=raw_input("please select your first name ") ln=raw_input("Please select your Last Name ") return fn, ln def enter_numbers(): fnbr=input("please select your favorite number ") sfnbr=input("please select your second favorite … | |
Re: You don't check the last element, so possibly want to use ~~~ for x in range(0,len(mylist)-1): ## don't compare "x" to itself and go to the end of the list for y in range (x+1,len(mylist)): ~~~ This will give you a count for each item, so you will get three … | |
Re: You have to append a newline, "\n" ~~~ for record in ['<table border="1" cellpadding="15" cellspacing="5" width="100%">', '<tr>', '<th colspan="2">Vysvedcenie</th>', '</tr>', '<tr>', '<td width="50%">Meno, Priezvisko:</td>\<td>Skolsky rok:</td>', '</tr>'] ## number of lines truncated above vytvor.write("%s\n" % (record)) ~~~ |
The End.