2,190 Posted Topics
Re: There is more than one way, but you could use a for() loop with step=4. We don't have the file so you will have to test and correct this yourself.[code]def block_generator(): with open ('test', 'r') as lines: data = lines.readlines ()[5::] for ctr in range(0, len(data), 4): block = [] … | |
Re: [QUOTE=;][/QUOTE] [quote]My biggest problem is figuring out where to start. Even the pseudo code doesn't help me very much[/quote][quote]PA 5 Pseudocode 0. Read file, strip lines, split lines on commas, create list of lists or tuples, etc. 1. Process the list printing the state name if its length is >=11 … | |
Re: Do you want to do this in the console, as the post above, or did you intend on using a GUI. Some sample input and output would also help. | |
Re: Recursion = a function that calls itself and is along the lines of [code]def recursion_funct(list_in): print "list is now", list_in if len(list_in) > 1: recursion_funct(list_in[1:]) else: return ## out of entries in the list test_list=[13, 9, 15, -10, 11] recursion_funct(test_list) [/code]First figure how to use a function that calls itself … | |
Re: First, you have to call "hanlderf()" and create the text widget. Since I don't have the file, the text inserted into the widget is different. [code]from tkinter import * def handler(event): print("Clicked at", event.x, event.y) class Application(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.grid() for r in range(6): self.master.rowconfigure(r, weight=100) #Creating … | |
Re: [QUOTE=;][/QUOTE] You have to return the values, see [url]http://www.penzilla.net/tutorials/python/functions/[/url] for starters. [code]def fcn1(record): do some stuff write result to a new file return data_from_this_function def fcn2(data_from_fcn1): do some stuff write result to a new file return data_from_this_function def fcn3(data_from_fcn2): do some stuff write result to a new file return result … | |
Re: [code]myFile = open("inn.txt", "r") for line in myFile: lines +=1 if line == ('\n'): blanklines +=1 for char in myFile:[/code]When you get to "for char in myFile" you are already at the end of the file (because "for line in myFile" went through the entire file) so there is no … | |
Re: [quote]Write a program that creates a linked list of bunny objects[/quote]This is just a list of objects, because to create a linked list you have to link one to the other in some way but the link was not given (and I did not read it carefully so may have … | |
Re: I you want to use Tkinter then you should also install the [url=http://pmw.sourceforge.net]Pmw extension[/url]. It makes things much easier IMHO, and is a collection of Python/Tkinter files that extend Tkinter. That means it is easy to install as it is installed by copying to anywhere in your PYTHONPATH (sys.path). This … | |
Re: .txt files do not support bold or underlining. To do that you must use a mark-up file like postscript, html, or XML. But each one uses a different method. If you are viewing the file in a text or word processor you must use the markup style that it expects, … | |
Re: You should be passing line to the function. [code]def percount(line): periods = 0 for char in line: if char == '.': periods +=1 return periods for line in myFile: ... ## for char in line: p = percount(line) total_periods += p [/code]Also, in the real world if line == ('\n'): … | |
Re: f is never declared and so is not a character buffer. | |
Re: Everything should be under the while loop. [code]number = int(raw_input("Please enter a number: ")) largest = number while (number>0): number = int(raw_input("Please enter anohter number: ")) if (largest>number): ## this will give the smallest number = largest ##if (number<0): not necessary as the while loop takes care of this print … | |
![]() | Re: First search for "Python anagram" at Daniweb using the search box at the top of the page. That will give you several techniques for using a list of words and will not require us to answer the same question over and over. [code] ## this will always be true on … ![]() |
Re: Your original code would look something like this, but take note of the try/except as that is usual way of getting input. [code]number=0 while number>=0 and number<=100: ## will only add to total if the input from the previous loop is 0 <= number <= 100 total+=abs(number) ## adds number==zero … | |
Re: 8 is a leap year. 104 is also a leap year but your code will not catch it. In pseudocode: 1. Is the year evenly divisible by 4 2. Is the year evenly divisible by 100. If divisible by 4 and not by 100 it is not a new century … | |
Re: A basic principle is that you have bytes of data. In your case you want to modify and store the bytes several times. It does not matter how you store them. You can create files to store them, as in this program, or you can store them in a list … | |
Re: You would first have to store the rows so you can find the largest number in each column. Post the code for that and we would then have enough info to be able to help with the final formatting. | |
Re: If you use a loop then you should also use "update_idletasks" or no updates will happen until the loop exits. Note that this code is for Python2.x [code]from Tkinter import * import time class MyGui(): #state = 1 ---> label is green #state = 0 ---> label is red #state … | |
Re: First find the summing numbers. You have no "num" because you have no sums. For the number 8 for example, 4+4 or 6+2 will not work, so it has to be 5+3 or 7+1. To test the number 8, you would have to test 1, 2, 3, 4, 5, 6, … | |
Re: You can push each letter onto the stack and then pop each letter off into a new string. Since stacks are last-in-first-out, the new string will be a reverse of the original string. The two strings can then be compared [url]http://greenteapress.com/thinkpython/thinkCSpy/html/chap18.html[/url] | |
Re: Everyone pays 10%. If income is greater than $8500, add 5%. If income is greater than $34,500 then add 10% more, etc. Deductions are a separate process and would be calculated before the tax is calculated as it reduces income. For more assistance, post your code so far. | |
Re: [quote]1. I am having an issue giving values to each card, ex assinging FaceCards=10 Aces=11 and cards 2-10 to their number values.[/quote]What is in the card deck? Are they 1-52, in which case every group of 13 would be a suit, so 1=2 Clubs, 14=2 Diamonds, etc. or are they … | |
Re: Using == is always dicey. Add a print statement to see what happens, and instead of ==190, use > 189. [code] for i in range (100000): print x, y # move the circle circle.move(dx, dy) x = x + dx y = y + dy # when x reaches 190, … | |
Re: I don't know about anyone else, but I'm getting a little tired of the "I goggled for this" lie [url]http://tinyurl.com/3ldotwl[/url] Perhaps we should boycott the too obvious fibs and point them out as above. Edit: But then I guess that's the same thing, as that's doing the search effort which … | |
Re: You can slice off the first two records. [code]f = open('data.txt', 'r') # Open file data = f.readlines() data = data[2:] f.close() print max(data), min(data) [/code] but this may or may not give you what you want depending on the values and type of values in data. | |
Re: [QUOTE=;][/QUOTE] [quote]but I want to try dev-ing a GUI w/o using even Tkinter[/quote]A book that is quite dated, but on the other hand probably inexpensive as a result, that describes how to access video memory and put pixels on the screen one at a time as any of the GUI … | |
Re: Since you did not include the entire error message we have to guess as to which line it is referring to (and that is quite possibly an information message, not an error message), so adding a print statement after the for() loop is the obvious place to start. A [url=http://www.freenetpages.co.uk/hp/alan.gauld/tutfiles.htm]tutorial … | |
Re: You don't save the original file name so there is no "copy from" name. Be sure to back up the directory before testing any rename code. [code]import os #read in file from the directory for filename in os.listdir("."): # f = open(filename, "w") # i = True # while i: … | |
Re: [QUOTE=;][/QUOTE] If you are too lazy to read [url=http://www.daniweb.com/software-development/computer-science/threads/573]the rules of the forum[/url] and have waited until the very last minute, then there is little chance of a reply. | |
Re: You can iterate through the list, taking each sub-list and printing the individual items within that sub-list. [code]test_list=[['123', 19, '13', 'Fa0/19', '100000000'], ['13', 22, '13', 'Fa0/22', '100000000'], ['123', 19, '123', 'Fa0/19', '100000000']] for sub_list in test_list: print sub_list for ctr in range(len(sub_list)): print " ", ctr, "-->", sub_list[ctr] print [/code] | |
Re: You have to define w1...wn, and g. They should be in the list "words". You can see what is in "words" with this code. Note that arithmetic only works with numbers, not strings. See [url=http://www.greenteapress.com/thinkpython/html/book003.html#toc12]this page[/url] for starters. [code]#FileName = input("Enter the name of the file") ## not used by … | |
Re: strip() the line and then split() it and test the length. The ones you want should have a length of 2 if I understand correctly. | |
Re: Use a return from the function (untested code). [CODE]def callcard (): IOU="0" while IOU not in ["1", "2", "3", "4"]: print print "1. For $5 or $10." print "2. For a $25 charge, you get $3 of extra phone time." print "3. For a $50 charge, you get $8 of … | |
Re: [url=http://pmw.sourceforge.net]Pmw[/url] has a nice set of extensions to Tkinter. I would take a look at ScrolledText for starters, as it has a [url=http://pmw.sourceforge.net/doc/ScrolledText.html]get method[/url], as do TextDialog and other similar widgets (I think). Pmw also has curselection and getcurselection methods, in the combobox and notebook, but I have not used … | |
Re: reverse=True means that it is sorted in reverse order, (largest to smallest), and [:3] only prints the first 3 items. To print them all in ascending order, use: [code]repeat = {"c":1, "b":2, "a":1} print sorted([ (freq,elem) for elem, freq in repeat.items()]) [/code] You can not sort by descending order for … | |
Re: You can just use "command=search" for the next button, and add a get-a-file-name routine to the top of the function. | |
Re: Your indentation is funky, probably using tabs instead of spaces. First, "print processed_line.split()" at the end of your code. Lists do not have a split() method. You can use split() on the original string, but then you still have to iterate letter by letter and test for alpha as you … | |
Re: Usually the GUI or text processor that you are using to view the data has a word wrap limit. You can set that to 20 characters or what ever you want. | |
Re: If you want to return the original list, then modify it's contents. If not, return a new list (you only return one number, not the entire list). [code]def square_each(nums): for ctr in range(len(nums)): nums[ctr] **= 2 return nums def square_new_list(nums): return_list = [] for num in nums: return_list.append(num**2) return return_list … | |
Re: Google is your friend. There are several tools depending on what you want to do. | |
Re: Nothing will happen until the for() loop exits for i in range(10000000000): Tkinter (graphics.py is a subset of Tkinter) has the after keyword, so it would create a new snowflake after x amount of time, while the rest of the program continues to run normally. I don't know if graphics.py … | |
Re: Use itertools. [code]import itertools x = [['foo'], ['bar', 'baz'], ['quux'], ("tup_1", "tup_2"), {1:"one", 2:"two"}] print list(itertools.chain(*x)) [/code] | |
Re: You can pack them all into a Frame and destroy the Frame. [code]try: import Tkinter as tk ## Python 2.x except ImportError: import tkinter as tk ## Python 3.x class DestroyTest(): def __init__(self, top): self.top=top self.top.geometry("+10+10") self.frame=tk.Frame(self.top) self.frame.grid() test_label=tk.Label(self.frame, text="Label") test_label.grid(row=1, column=0) destroy_button=tk.Button(self.frame, text="Destroy Frame", \ command=self.destroy) destroy_button.grid(row=10, column=0) exit_button=tk.Button(self.top, … | |
Re: Damn pyguy62, that's quite a bit of effort. Hopefully it will be appreciated. Look at the indentation after line 55. I hope you read this before the 30 minute limit is up. To sort by value can also be done with [code]from operator import itemgetter print sorted(word_count.items(), key=itemgetter(1)) [/code] but … | |
Re: [quote]Need urgent help on python coding![/quote]It is not our fault that you waited until the last minute. [quote]I do not know where to go from here[/quote]Neither do I as I do not know what your assignment says you should code. [quote]pleaseeeeeeeeeeeee HELP ME[/quote]And most especially, no one wants to see … | |
Re: The "standard" way is to use a while() loop. [code]question=0 while question not in ['1'. '2']: question = input("Press 1 or 2") print("1 or 2 entered", question) [/code] | |
Re: Please make the post "Solved" so you aren't rude to those who would otherwise read this unnecessarily. | |
Re: Print the value for "i" each time through the while() loop. And you should not use "i", "l", or "O" as single digit variable names as they can look like numbers. [code]for x in text:[/code] Also the logic is bass-ackwards so print the value for "x" each time through the … |
The End.