3,386 Posted Topics
Re: Check this my code in the end of one thread about circles. Play with the code [url]http://www.daniweb.com/forums/post1193986.html#post1193986[/url] | |
Here tool functions, which came up during discussions earlier. Expanded slightly, added elimination of traceback prints by try..except. Added dd.mm.yy routine as that is format in my home country Finland. Optimized docstring beginnings to be informative in editor tooltip (at least IDLE shows, but code must be run or imported … | |
Re: Yes you can: [CODE]mydict = {} mydict['key1'] = 2000 mydict['key2'] = 3000 mydict['key3'] = 6000 for x in range(1000,8000,1000): print x,(x in mydict.values()) and "is" or "is not", "in set" [/CODE] | |
Re: I would use split, map and join again, if I do not want to keep exact whitespace [CODE] >>> a="the book of my dreams" >>> a.capitalize() 'The book of my dreams' >>> a.split() ['the', 'book', 'of', 'my', 'dreams'] >>> map(lambda x: x.capitalize(), a.split()) ['The', 'Book', 'Of', 'My', 'Dreams'] >>> " … | |
Re: Try to find a use for fd() command and left/right moving instead of goto in your program. [CODE] from turtle import * circle(100) up() left(90) fd(100) left(90) down() circle(80) mainloop() [/CODE] [I](If one number in this program is changed and one command is changed, this program draws two concentric circles.)[/I] | |
Re: You forgot to attach the images to your post, so we can not test the code (easily). | |
Re: Standard way is to have seconds since some starting time or day with floating point number (say Visual Basic). [CODE] #!/usr/bin/env python episode_date=[] epdatestart = '10/1/2009' epdate = epdatestart.split('/') epdateend = epdate[2]+'-' + epdate[0]+'-' + epdate[1] print str(epdatestart) print str(epdateend) """ Output: >>> 10/1/2009 2009-10-1 >>> """ [/CODE] | |
![]() | Re: Here is adapted to you and my own use version of timestr function. Notice that normal use of time for logging etc becomes easier when can put only timestr() for time values and have fuction to make it human understandable. Use example [icode]line+='Processing took '+timestr(t2)+'.\nFinnishing time :' +time.asctime()+'.\n'[/icode] [CODE] import … |
![]() | Re: [CODE]print_list( input ): ## ??? you can not call input in parameter list f = open('pickle.dat', "r") L = cPickle.load(f) f.close() for i in range(len(L) ): ## you mean len(L) don't you print 'L[i] = ', L[i] [/CODE] [CODE]>>> def do_something(a=None): if not a: a=len(L) a=min(len(L),a) if a: for i … |
Re: Attack the task line by line in python command line. Don't get confused with define, as python has not definition, you just pick up one small number from your head for the variable After success in command line write the successfull lines to file and prove to run it and … | |
Re: This is that da Vinci code I listened the other day? This genius used mirror writing so that nobody could understand his documentation. Microsoft security chief wasn't he? | |
Re: [QUOTE=ITgirl2010;1190915]I need to calculate the fuel cost for a whole trip with keeping the three parameters (distance, econome and the price of fuel ) changeable[/QUOTE] Maybe use parameters in the function like this (pseudo code) [CODE] def roadtrip(what1,what2 etc): #no inputs, no prints #do my calculation return result_of_my_calculation #instead of … | |
Re: OK, here still my try with command line interface. File output by redirecting output to file. [CODE] ## sorted unique lines printer (redirect to file to save output) ## inside python use wordlist function import os,string from sys import argv from collections import defaultdict def wordlist(f): d = defaultdict(bool) if … | |
Re: [CODE] ## file.txt has: """ Larry, Brocker, 12, 4, 11 Sally, Strocker, 8, 9 Barry, Crocker, 17, 19, 30, 18 """ ## or keep it simple lines=open("file.txt").readlines() print lines lines=[x.split() for x in lines] print lines print 'Last number in each line is:' for i in lines: print i[-1] print … | |
Re: Post with code tags again, indentation lost. | |
Re: I can not read your code, but does this code help you. I define average for rows and printing function with less desimals in order to print matrix more clearly. I make random matrix x, take copy of it named y, add averages to end of rows of y and … | |
Re: Also [CODE] >>> n=10 >>> a=[list(range(n)) for y in range(n)] >>> from pprint import pprint as pp >>> pp(a) [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, … | |
Also demonstrate changing the text of the widgets label dynamically. | |
I optimized very carefully program I was producing by producing the main tight loop functions half a dozen times with various implementation styles. Also i considered amount of passing information in call hierarchy in parameters or letting functions to trust proper initialization of the global variables. I got few interesting … | |
Re: See comments! [QUOTE=pythonnewbie10;1191435][code=syntax] import random def main(max): ## max is poor parameter name as it is used by python and you can ## not use max function after this ## parameter is never used print "\tWelcome to 'Guess my number'!" print "\nI'm thinking of a number between 1 and 100." … | |
Re: [QUOTE=teg1989;1191095]I sent a pm with what this is regarding. Is there anyone I can contact who can remove or edit this?[/QUOTE] Could you not just mark the thread SOLVED as the starter of the thread? | |
Re: [CODE] import time import random from math import * from graphics import * win = GraphWin("Bounce", 500, 500) win.setCoords(0.0, 0.0, 500, 500) from graphics import * import random class Bounce(object):#class def __init__ (self, circle):#construtor self.circle = circle self.wins = 0 self.speed = 2 def ball(self): #Ball, creates the ball for … | |
Re: [QUOTE=jice;1188998]You can do a list of lists... [CODE] rows=[[],[],[]...] # you call a particular cell by doing rows[3][4] [/CODE][/QUOTE] You can also think if it makes sense to divide the rows by 3 cell limits, you can also use tuples instead of lists. [QUOTE]( ((1,2,3)(4,5,6)(7,8,9)) ((4,5,6)(7,8,9)(1,2,3)) ...... ((5,6,4)(8,9,1)(2,3,7)) ) [/QUOTE] … | |
Re: Some comments. (Propably many times longer than the program you need to write) [CODE] ## Your code with comments import math import string def main(): ifileName = raw_input('What file are the numbers in? ') ofileName = raw_input('What is the output file? ') inFile = open(ifileName, 'r') outFile = open(ofileName, 'w') … | |
Re: > Hey, import matplotlib import numpy import pylab lam = pylab.linspace(0.1,100,1000)*10**-6 pylab.xlabel('Wellenlaenge (mM)') pylab.ylabel('Temperatur (T)') pylab.title('Mac Plank') c1 = 0.374*10**-15 c2 = 1.439*10**-2 p= lamda x,t : (c1/(x**5))*(1/(2.7183**(c2/x*t))-1) pylab.plot (lam*1e6,p(lam,300)) for t in (300,400,600,800,1000): pylab.plot(lam*1e6, p(lam,t) > end quote. You have unterminated parenthesis in the last line at least. I … | |
Re: Maybe something along these lines (prompt when running, after exit manage() command). It does bring up the terminal window. [CODE] # run_me.py import os def manage(): s=' ' print 'Finnish manage command by entering empty line.' while s<>'': os.chdir('d:\\Python Projects\mysite') s=raw_input('#') if s: os.system('python manage.py '+s) manage() [/CODE] or you … | |
Re: The problem analysis: [CODE] # -*- coding: utf-8 -*- import string x = [] a='ąóęśłżźćńĄÓĘŚŁŻŹĆŃ' ## utf8 has variable number of bytes ##a='boeslzzcnAOESLZZCN' #only changes b to a to test the end of the solution b='aoeslzzcnAOESLZZCN' print a,len(a),b,len(b) ## here is why t = string.maketrans(a,b) text = raw_input("> ").translate(t) print … | |
Re: OK, let's enter this beauty contest ;) I am working still in python2, so sorry to you python3 users! [CODE] ## My style of coding def letter_width(a,b): wd=-1 ## initial value which is not in value range try: ## Taking care of value range we want "while with purpose" while … | |
Re: Here some short try on this, but I am only beginning to learn myself the the turtle module. It does do some rudimentary scaling adapting to number of students and number of their marks. If you use this code, send me your final code in return and clearly mark the … | |
Re: Maybe you could adapt this my code [url]http://www.daniweb.com/forums/thread274273.html[/url] or [url]http://www.daniweb.com/forums/thread273625.html[/url] Basically you can use the built in functions max or min, but must take the part of list which has the numbers only. Greetings, Tony Veijalainen | |
Re: If you would like to save your poor computer from overwork and get head ache from splitting ;). Instead put that energy for checking that file is there: [CODE] import os, sys def fileCount(filename): """ Error checking version of text statistics count. Returns the ready to print or put to … | |
Re: [B]16.15.1.16. Incomplete Types[/B] Incomplete Types are structures, unions or arrays whose members are not yet specified. In C, they are specified by forward declarations, which are defined later: struct cell; /* forward declaration */ struct { char *name; struct cell *next; } cell;The straightforward translation into ctypes code would be … | |
Re: Reformulated program to function only without knowing specific use case: [CODE] import os from Tkinter import Tk import tkFileDialog toplevel = Tk() toplevel.withdraw() filename = tkFileDialog.askopenfilename() if os.path.isfile(filename): for line in open(filename,'r'): print line, else: print 'No file chosen' [/CODE] | |
Re: Interesting way, I had not noticed the second parameter of split before. To my mind came however another method which I find intuitive using the partition method with space as separation, I do not know if it is slow or fast, but looks similar to method of split with 1 … | |
Re: [QUOTE=hondros;1182396]The escape character "\t" for tabs works wonders ;)[/QUOTE] You mean like this: [CODE]import random suits = "CDHS" cvalues = list("AKQJ") + list(str(i) for i in range(2, 11)) deck = ([cv + cs for cs in suits for cv in cvalues]) columns =[list() for i in range(10)] ## real dealing … | |
Re: You posted this long ago and just because you did not send final answer to your problem, I coded this, based on some receipt from internet for the single callback functions ([url]http://www.astro.washington.edu/users/rowen/TkinterSummary.html#CallbackShims[/url]) [CODE] #!/usr/local/bin/Python import Tkinter import tkMessageBox import random def doButton(buttonName): global but but=button[buttonName] c = v,kind = deck.pop() … |
The End.