3,386 Posted Topics

Member Avatar for RoqueyB

[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 on the first pass number=float(input("Enter a number: ")) [/code] The abs does not belong in loop, as the number is checked in …

Member Avatar for TrustyTony
0
159
Member Avatar for TrustyTony

Here is class that can be fed directly from split stream like one number per line file (or [URL="http://www.daniweb.com/software-development/python/code/321725"]generator producing 'words'[/URL] from multiple numbers per line).

Member Avatar for Gribouillis
0
610
Member Avatar for Tech B

For calculating the amount of results see: [url]http://www.mathsisfun.com/combinatorics/combinations-permutations-calculator.html[/url] [CODE]import itertools def generate(): """Main program, gets user info and generates alternatives""" ch = sorted(set(raw_input("characters to try: "))) num = int(raw_input("max length: ")) # skip trivial 1..3 letter words for base in itertools.chain(itertools.product(ch, repeat=n) for n in range(4, num+1)): for w in …

Member Avatar for TrustyTony
1
375
Member Avatar for python-noob
Member Avatar for TrustyTony
0
2K
Member Avatar for pelin

1900/4 is not 0 and also 1900/400 is not 0, so the condition is fullfilled, you should use modulo (%).

Member Avatar for pelin
0
121
Member Avatar for TrustyTony

I would like to ask you to share with us your experiences in situations, where you suddenly realized that your approach to solution was stupid. And by taking other angle to problem it became easy. Or you thought one question and realized later that actually you should have asked another …

Member Avatar for GrimJack
0
191
Member Avatar for bigredaltoid

And to make it simpler use built in sum and len method of sequence types. [CODE]data = """ 57 83 99 12 45 81 74 30 29 66 95 87 42 """.strip().splitlines() print('Average is %f' % (float(sum(int(n) for n in data))/len(data))) [/CODE]

Member Avatar for TrustyTony
0
2K
Member Avatar for Ice_Occultism

There is plenty of ways, most recomendable is to use list comprehension:[CODE]splits = [int(n) for n in splits][/CODE] If you only want to sort them numerically, simplest is to use int as key for sorted-function.

Member Avatar for Ice_Occultism
0
380
Member Avatar for Cyl11
Member Avatar for pelin

> HOW CAN I WRITE A PROGRAM THAT CALCULATES THE FIRST N NATURAL NUMBERS THAT USER ENTERS. I SHOULD USE FUNCTIONS. > > > THIS IS WHAT I DID SO FAR. THANKSSS n = eval(input("Enter the value of n: ")) def sumN(n): for i in range(n): sum = n+i return …

Member Avatar for pelin
-1
138
Member Avatar for felix001

If you want small self contained one, most simplest white space only version has been last in DaniWeb in my post: [url]http://www.daniweb.com/software-development/python/threads/385547/1661150#post1661150[/url] That case was simpler in that way that last number of range is known to be (one of the) longest, normally you would need to use max funcition …

Member Avatar for felix001
0
326
Member Avatar for Sprewell184
Member Avatar for Sprewell184
0
263
Member Avatar for algrandjean1
Member Avatar for Panathinaikos22

How about taking in a 3 value vector and sorting the vector,[URL="http://en.wikipedia.org/wiki/MATLAB"] MATLAB is Matrix Laboratory[/URL], isn't it? [url]http://www.mathworks.se/help/techdoc/ref/sort.html[/url]

Member Avatar for Panathinaikos22
0
139
Member Avatar for madtowneast
Member Avatar for Cireyoretihw
Member Avatar for magnum_vf

Grib means this: [CODE]import datetime mydate=['20111025.00', '20111026.12'] fmt = "%Y%m%d.%H" hour = datetime.timedelta(hours=1) start_time, end_time = [datetime.datetime.strptime(d, fmt) for d in mydate] now = start_time while now <= end_time: print now.strftime(fmt) now += hour [/CODE] Doing this manually by normal loop is also not so tough job.

Member Avatar for magnum_vf
0
6K
Member Avatar for kkasp
Member Avatar for bogut

1) Decide algorithm 2) Try it on paper 3) Make test cases 4) Write code incrementally and run tests 5) Run and get results, when tests pass

Member Avatar for FlamingClaw
0
191
Member Avatar for peter_budo

Also maybe the similar match algorithm needs tuning, as I can not understand why my Sudoku code snippet (using Norvig's base) is number one cross refeerence.

Member Avatar for TrustyTony
2
249
Member Avatar for The_Tiger

I would refactor little snipsat's code and remove the unnecessary list comprehension and replace it with generator. I prefer to do calculation to all name again to using temporary variables, as operation is instantaneous. [CODE]def name_input(): name = '' while ' ' not in name: name = raw_input("Please enter your …

Member Avatar for Tech B
0
123
Member Avatar for ohlermsu

It all depends what kind of effort you are showing. You should post your code this far with CODE tags.

Member Avatar for Stefano Mtangoo
0
220
Member Avatar for NickSchade
Member Avatar for effBlam

Help for copy paster not stating origin of the given code and not one line of his own effort can expect similar amount of help.

Member Avatar for TrustyTony
0
216
Member Avatar for Mr.Freak

You prepare time as seconds but test it as it where hours at last partial line. Better not to say what I think of your variable names ;)

Member Avatar for TrustyTony
0
122
Member Avatar for oberlin1988
Member Avatar for blackrobe

I would hash values of S1 and go over s in S2 hashing x-s and checking if that is saved in hash table then s, x-s is one pair for the sum. In Python terms [CODE]s1_set = set(s1) pairs = [(x, x-s) for s in S2 if x-s in s1_set][/CODE] …

Member Avatar for Taywin
0
543
Member Avatar for danethepain83

Of course it does. You do recurcively separating each head by car and then cons them back together, what else it could do. Describe the pseudocode with examples what you are trying and by what algorithm.

Member Avatar for danethepain83
0
2K
Member Avatar for beck4456
Member Avatar for sonicx

You should iterate lines of file, not range. Inside you can test that line is a interesting one or not. Alternately you could split whole file (after reading the first few lines in for loop with next or readline method) to words and iterate them skipping the words that are …

Member Avatar for woooee
0
14K
Member Avatar for terry_bogart
Member Avatar for geekman89

[QUOTE=geekman89;1674377]Please, please explain to me why this is giving me an error: [CODE]class bvo: C = 'Hello' def getC(self): return self.C bvo.getC()[/CODE][/QUOTE] [CODE]# to ease debugging and understandir this should be Bvo (PEP8) class bvo: # this is one per class class variable, not instance variable # to ease debugging …

Member Avatar for geekman89
0
148
Member Avatar for freakvista

Similar example, I gave, but not directly answer in code. [url]http://www.daniweb.com/software-development/pascal-and-delphi/code/369890[/url]

Member Avatar for TrustyTony
0
105
Member Avatar for sofia85
Member Avatar for Tcll

I am uniquely qualified then as I am also OO newbie, even maybe I manage sometimes hide it. Also I have not designed this kind of programs and plugin systems... well [URL="http://www.daniweb.com/software-development/python/threads/325854/1392728#post1392728"]except yours[/URL], if you did not change it. I think my mind would come to have[URL="http://docs.python.org/library/abc.html"] ABC (Abstract Base …

Member Avatar for Tcll
0
165
Member Avatar for giancan
Member Avatar for Cupidvogel

python needs full path of argument or you must start it in the location of the file. Windows searches commands anywhere in the path when file is first in line.

Member Avatar for TrustyTony
0
302
Member Avatar for arson09

You should not retrun the name, you should return dictionary, you have very misleading name. The call parameter has no meaning as you empty the parameter immediately at first line of the function and use it for empty dictionary and after in line 6 use it as list with append, …

Member Avatar for arson09
0
566
Member Avatar for csterling

[QUOTE=woooee;1671414]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: …

Member Avatar for TrustyTony
0
2K
Member Avatar for JOSED10S
Member Avatar for marge0512

Data should be only one table according to principle of relational database and other tables should reference that table.

Member Avatar for marge0512
0
179
Member Avatar for Wilha
Member Avatar for TrustyTony
0
206
Member Avatar for felix001

List is ordered and you mention sorting, so list of lists looks better. Difficult to tell without knowing more of your plans.

Member Avatar for woooee
0
169
Member Avatar for ryufire

[QUOTE]wm_attributes does not take keyword arguments. See issue1500773. something like this should work, though: self.top=Toplevel(master=self.parent) self.top.wm_attributes('-alpha', 0.8)[/QUOTE] from [URL="http://bugs.python.org/issue5569"]transparency in top level tk window[/URL]

Member Avatar for cgsig
0
11K
Member Avatar for vlady

You have everything wrong, study more, and start fresh when you understand basics. I would consider making distance a method of Point class. Repeat especialy these:__init__ method and self parameter.

Member Avatar for vlady
0
128
Member Avatar for dw_user

I do not understand what the used language has to do with this feature. Language does not matter, of course the maker of program does not want it to be too easy to remove the checking routine, so some kind of check against changes in code is in order. Of …

Member Avatar for dw_user
0
215
Member Avatar for Sprewell184

Try with name, points instead of item at line 3. list is built in type so change it to something better describing the purpose. Line 2 does not belong in function as it overwrites the parameter.

Member Avatar for TrustyTony
0
132
Member Avatar for Nirvin M

Apple Lisa OS was written in Pascal. What kind of extensions it had I do not know, but you can look up MacPascal.

Member Avatar for TrustyTony
0
86
Member Avatar for SK6114
Member Avatar for pelin

Do not index lines but iterate over the lines by for. Code I can not check deeper, as indention is off because of missing code-tags Python is indention sensitive.

Member Avatar for TrustyTony
0
237

The End.