2,190 Posted Topics
Re: This is a common exercise where you group records based on some indicator, an empty line in this case. This code prints the contents but you can also output to a file using .join)() and append a newline, "\n". [code]lit="""a=1 b=2 c=3 a=4 b=5 c=6 a=7 c=8 a=9 b=10 c=11""" … | |
Re: Your indentation is incorrect so it is difficult to tell what you are trying to do. "i" will never equal -1 (and you should not use "i", "l" or "O" as they can look like numbers) [code] elif i==-1:[/code] And this statement does not make sense because you seem to … | |
Re: It is explained in [url=http://www.freenetpages.co.uk/hp/alan.gauld/tuttext.htm]this tutorial[/url]. Also a web search for "python string split" will turn up more examples. You can use a split and specify the character, or replace and then split() with the default=space(s). | |
Re: You can also use a list of lists [code]letters_to_numbers= [['ABC', 2], ['DEF', 3]] for test_it in ["A", "E", "C"]: print for group in letters_to_numbers: print "testing %s in %s" % (test_it, group[0]) if test_it in group[0]: print " the number for %s is %d" % (test_it, group[1]) # # or … | |
Re: This statement will always yield True [code] elif decision == "north" or "east" or "west" # #It breaks down to elif decision == "north" or "east" or "west"[/code] and both "east" and "west" are not empty strings so either will evaluate to True. Instead use [code] elif decision in ["north", … | |
Re: If you are using Python3.X input is fine. If you are using Python2.x then input captures numbers, and raw_input captures strings. | |
Re: Based on the sample data you provided, it would be simpler to find the row that starts with a number greater than the one you are looking for and check the previous row. [code]def compare_columns(matrix, number): for row in range(1, len(matrix)): if number <= matrix[row][0]: ## number is less than … | |
Re: You only have to update the "display" variable and keep track of misses as in this snippet.[code]def new_display(secret, display, guess, misses): """Displays the replacement of underscores with letters that the user has guessed""" index=secret.find(guess) display_list=[ eachltr for eachltr in display ] if (index > -1) and (display_list[index]=="_"): ## not already … | |
Re: You should usr "r" instead of guess, and you have to add the letters guessed to letter-g. [code]r= input("How many rounds do you want to play? (even number only) ") r=int(r) print (" You only get %d turns to guess the letter" % (d)) print ("Player 1's turn to play … | |
Re: 2**218 = 421249166674228746791672110734681729275580381602196445017243910144 and 2**50 = 1125899906842624 Either one is going to take a long time. As Tony said you should be using the decimal module. [quote]this is my program maybe you can find a solution it works but when numbers very big such as 2**50 it don't[/quote]Not unless there … | |
Re: Use GetValue().decode('utf-8')) or what ever character set you wish. Generally, when using a non-English language, you have to use .decode(). AFAIK-which isn't much when it comes to this- # -*- coding: utf-8 -*- means the program code can be written in utf-8 characters, so you can use Greek letters in … | |
Re: 2 text fields requires 2 Entry (Dialogs). Although TextCtrl is generally used. A standard example for entering user name and password. [code]import wx class TextFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, 'Text Entry Example', size=(300, 100)) panel = wx.Panel(self, -1) basicLabel = wx.StaticText(panel, -1, "Basic Control:") basicText = wx.TextCtrl(panel, -1, "I've … | |
Re: You possibly add to the dictionary on line 50, although we can't tell from the code if it is adding a new key or updating an existing one. FID_GC_dict[Cur_FID] = lr_Value You possibly want to use a copy of the dictionary and should add some print statements to see what … | |
Re: You check self.hit and self.miss, not the remaining empty squares (so there is some discrepancy between the lists). [code] def check_hit_miss(self, coodinate): '''return True if coodinate in board.hit or board.missed, return False otherwise. ''' if (coodinate in self.hit) or (coodinate in self.missed): return True else: return False # # change … | |
Re: The simplest is to use a dictionary [code]classes_taken={classlistA:["CMSC 201", "CMSC 202", "CMSC 203"], classlistB:["MATH 151", "MATH 152"]} etc. [/code]If you want to use lists then you can just transfer the classes taken to the list, so you would have 3 classes_taken lists or one list with 3 sublists. You can … | |
Re: You have to know the relative coordinates of the square and create text usually at a middle location. You will probably want to set the font to use and it's size. To modify your drawing [code] X1 X2 X3 X4 Y1 | | | | Y2 _____|_____|_____ | | | … | |
Re: [QUOTE=;][/QUOTE] Print "word" within the bottom for loop, and print "s" within IsPalindrome. It is considered poor style to have one isolated call to main() at the end of the program, which is just a function that calls statements that are better left outside, because in large programs you look … | |
Re: You should also check for values between a certain range, i.e. someone can not enter one million or a letter. | |
Re: Most of the online tutorials cover [url=http://www.freenetpages.co.uk/hp/alan.gauld/tutinput.htm]input and conversion[/url] from on data type to another. | |
Re: Assuming "How high" means the number of rows to print, use a for() loop instead: [code] for ctr in range(num2): print "row number", num+1 [/code]A link to [url=http://www.freenetpages.co.uk/hp/alan.gauld/tutinput.htm]getting input from the user[/url]. It's for Python 2.x but can be easily modified for 3.x. | |
Re: Dive into Python has a chapter on [url=http://www.diveintopython.net/installing_python/windows.html]installing Python[/url] for both the Python.org and ActiveState versions. | |
Re: "board" contains references to the same list, not separate lists. The following code lists the id's which are the same. One item in one list in then changed. The print statement shows that all lists reflect the change. To generate one row, see the last line and then use a … | |
Re: A duplicate of this post [url]http://www.daniweb.com/software-development/python/threads/412751[/url] | |
Re: Quoted to include [noparse][code] tags[/noparse].[QUOTE=blindg35;1761034]Hello guys, just joined the community. Found it through google. :) I am basically new to python, only couple of days of knowledge. I am trying to make all the words in a txt file appear in 4 or 5 in a row. so the txt … | |
Re: [quote]Some of those codes you posted I have not learned yet. The teacher wants us to post the first middle and last names on 3 separates lines using the whitespaces as the indexes[/quote] You did not state any limitations in your question, so how are we to know what you … | |
Re: That's because of the while() loop in the function. Instead keep track of the number of times the button is clicked. [code]from Tkinter import * class App: def __init__(self,master): self.master=master self.returned_values = {} frame = Frame(master) frame.grid() self.Answer_entry = Entry(master) self.Answer_entry.grid(row = 0, column = 1, sticky=W) self.Answer_entry.focus_set() User_Answer=self.Answer_entry.get() self.attempts … | |
Re: "f" is a local variable and does not exist outside of the function unless you return it (which you don't). Also, line 218, print updateprimepage(), will print None because nothing is returned from the function. | |
Re: That is impossible because we don't know what code makes it appear and we don't even know which one of the GUI toolkits you are using. Also include the version of Python you are using and the operating system. | |
Re: Try it and see for yourself. If you have problems post the code you are using for assistance. "When you create a new text widget, it has no contents. To insert text into the widget, use the insert method and insert text at the INSERT or END indexes:" [url]http://effbot.org/tkinterbook/text.htm[/url] | |
Re: [quote]The objective is to list the occurrences of vowels in a given word, such that for (word = yellow) and index spits out (e = 1, o = 1)[/quote]This (e = 1, o = 1) is not the position/occurrences so I am assuming you want to count the number of … | |
Re: You would send the values to a function and return the amounts. [code]def payment(principle, monthly_interest_rate, amount_paid): """ assumes a payment is made every month so there are no penalties """ if principle > 0: ## loan is not paid off interest_due = monthly_interest_rate * principle if amount_paid < interest_due: print … | |
Re: This should work (note the single quote at the beginning and end-Python uses both/either) but print "soxxy" to be sure. soxxy = '"tools\sox.exe" "%s.wav" "%ssox.wav" compand 0.3,1 6:-60,-50,-30 -14 -90 0.2 gain -n -1' % (name, name) | |
Re: Lists are the easiest and quickest way to work with data IMHO. You can then join the elements into one string [url]http://www.diveintopython.net/native_data_types/joining_lists.html[/url]. [code]a = "01101000011001010111100100000000000000000000" b = "011000100111100101100101" combined_list=[] combined_list.append(b) combined_list.append(a[len(b):]) ## slice off length of "b" print "".join(combined_list) ## result 01100010011110010110010100000000000000000000[/code] | |
Re: You would use a while() loop or function for this, Search the web for either as there are many examples of password entry. Some pseudo-code[code]def get_pw(): while 1: pw=raw_input() if pw is correct: return [/code] | |
Re: Another way is to use a third class as a wrapper. The buttons in the following code print the contents of the variable from the other class. Also, it is never a good idea to create two or more instances of a GUI at the same time. It can cause … | |
Re: It's a trick question because you don't store the values in a separate variable (that's why we have lists), you use short_list[0].strip() short_list[1].strip() short_list[2].strip() or strip all of the values in short_list if you don't want to strip() each time. [CODE]short_list = ['a\n' , 'b\n', 'c\n'] for x in range(len(short_list)): … | |
Re: You would have to store all of the initial values and change the values back when the button in pressed. Or, generally you use a class to run a Tkinter application so you can destroy the class instance and create it again. Some example code would help us help you … | |
Re: You have not declared Button1 in the code you posted so there is nothing to disable. But generally it is [code]b.config(state=DISABLED)[/code] where "b" is the button's ID as returned by Tkinter. Also, you import Tkinter in two different ways (pick one only) [code]import Tkinter from Tkinter import *[/code] Do not … | |
Re: Generally you use GetLabel() or event.GetEventObject().GetLabel(). Search on those for some examples. | |
Re: You can condense all of the print statements if you want. [code]print """Welcome to The Great Text Adventure! One day you wake up, in a room, locked in by yourself. Could there be a key? In the room you see.""" [/code] You can also [url=http://www.diveintopython.net/native_data_types/lists.html#d0e5623]use a list[/url] to eliminate redundant … | |
Re: The palindrome function only compares the first and last numbers. You have to compare all of them. To reverse an integer without converting to a string you divide by ten. [code]def palindrome(num_in): reversed_num = 0 whole = num_in while whole > 0: whole, remain = divmod(whole, 10) reversed_num = reversed_num*10 … | |
Re: In the OnPlus function, the program will sleep for 1*5000 seconds which is about 1 1/2 hours. In the OnMinus function also, you add/subtract one from value but it has no meaning i.e is not displayed. You should test each function individually as you create it, otherwise you will be … | |
Re: Everything should go under the if statement (indents count in Python), or copy the file you want to process to a list and process the list. [code]def treeDir (arg, dirpath, basenames): workspace = dirpath roads_list = [] for f in basenames: if f.startswith('roads.'): fullf = os.path.join(dirpath,f) function_to_process_this_file(fullf) ## ## or … | |
Re: As Tony stated, you can iterate over a file [code]#f = open('lorumipsum.txt'); # simulate the file f=["Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim\n", "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea … | |
Re: Quoted to show indentation. Use "count" instead. [code]S1="""AACAAGAAGAAAGCCCGCCCGGAAGCAGCTCAATCAGGAGGCTGGGCTGGAATGACAGCG CAGCGGGGCCTGAAACTATTTATATCCCAAAGCTCCTCTCAGATAAACACAAATGACTGC GTTCTGCCTGCACTCGGGCTATTGCGAGGACAGAGAGCTGGTGCTCCATTGGCGTGAAGT CTCCAGGGCCAGAAGGGGCCTTTGTCGCTTCCTCACAAGGCACAAGTTCCCCTTCTGCTT CCCCGAGAAAGGTTTGGTAGGGGTGGTGGTTTAGTGCCTATAGAACAAGGCATTTCGCTT CCTAGACGGTGAAATGAAAGGGAAAAAAAGGACACCTAATCTCCTACAAATGGTCTTTAG TAAAGGAACCGTGTCTAAGCGCTAAGAACTGCGCAAAGTATAAATTATCAGCCGGAACGA GCAAACAGACGGAGTTTTAAAAGATAAATACGCATTTTTTTCCGCCGTAGCTCCCAGGCC AGCATTCCTGTGGGAAGCAAGTGGAAACCCTATAGCGCTCTCGCAGTTAGGAAGGAGGGG TGGGGCTGTCCCTGGATTTCTTCTCGGTCTCTGCAGAGACAATCCAGAGGGAGACAGTGG ATTCACTGCCCCCAATGCTTCTAAAACGGGGAGACAAAACAAAAAAAAACAAACTTCGGG TTACCATCGGGGAACAGGACCGACGCCCAGGGCCACCAGCCCAGATCAAACAGCCCGCGT CTCGGCGCTGCGGCTCAGCCCGACACACTCCCGCGCAAGCGCAGCCGCCCCCCCGCCCCG GGGGCCCGCTGACTACCCCACACAGCCTCCGCCGCGCCCTCGGCGGGCTCAGGTGGCTGC GACGCGCTCCGGCCCAGGTGGCGGCCGGCCGCCCAGCCTCCCCGCCTGCTGGCGGGAGAA ACCATCTCCTCTGGCGGGGGTAGGGGCGGAGCTGGCGTCCGCCCACACCGGAAGAGGAAG TCTAAGCGCCGGAAGTGGTGGGCATTCTGGGTAACGAGCTATTTACTTCCTGCGGGTGCA CAGGCTGTGGTCGTCTATCTCCCTGTTGTTC""" for lit in ["GAAT", "CAA"]: print lit, S1.count(lit) [/code] [QUOTE=sainitin;1747579]I have text file as follows seq.txt >S1 AACAAGAAGAAAGCCCGCCCGGAAGCAGCTCAATCAGGAGGCTGGGCTGGAATGACAGCG CAGCGGGGCCTGAAACTATTTATATCCCAAAGCTCCTCTCAGATAAACACAAATGACTGC GTTCTGCCTGCACTCGGGCTATTGCGAGGACAGAGAGCTGGTGCTCCATTGGCGTGAAGT CTCCAGGGCCAGAAGGGGCCTTTGTCGCTTCCTCACAAGGCACAAGTTCCCCTTCTGCTT CCCCGAGAAAGGTTTGGTAGGGGTGGTGGTTTAGTGCCTATAGAACAAGGCATTTCGCTT CCTAGACGGTGAAATGAAAGGGAAAAAAAGGACACCTAATCTCCTACAAATGGTCTTTAG TAAAGGAACCGTGTCTAAGCGCTAAGAACTGCGCAAAGTATAAATTATCAGCCGGAACGA GCAAACAGACGGAGTTTTAAAAGATAAATACGCATTTTTTTCCGCCGTAGCTCCCAGGCC … | |
Re: [quote]SELECT * FROM PLAYERS WHERE NAME = ''pete' OR '1'='1'' with an error message [/quote] There is a syntax error here that has nothing to do with MySQL or injection. Your query breaks down into '' pete ' OR ' 1 '=' 1 ' In the future please include the … | |
Re: You should start a new thread with one of the questions only, and the code that you have tried. For example, where is the data coming from in the first question? If from a file, then the file has to be read. | |
Re: All tutorials contain information on how to work with strings. [url]http://www.pasteur.fr/formation/infobio/python/ch01.html#d0e109[/url] [url]http://docs.python.org/library/stdtypes.html#string-methods[/url] [url]http://wiki.python.org/moin/BeginnersGuide/Programmers[/url] | |
Re: Come up with a test suite, with known numbers and known results to test with. Also, you don't know if multiplication or division is done first so this line a= (sum1 / r*1.0) can lead to unexpected results. And I think that multiplication and division would evaluate left to right, … | |
Re: It would be an else statement after the blankline code [code] if line.startswith('\n'): blankline = blankline + 1 else: if len(line.strip()) < shortest: shortest = len(line.strip()) # or better yet line = line.strip() if len(line) == 0: blankline = blankline + 1 else: if len(line) < shortest: shortest = len(line) … |
The End.