3,386 Posted Topics
Re: [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 … | |
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). | |
Re: 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 … | |
Re: You did not give the correct name, check the parameter of function. | |
Re: 1900/4 is not 0 and also 1900/400 is not 0, so the condition is fullfilled, you should use modulo (%). | |
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 … | |
Re: 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] | |
Re: 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. | |
Re: > 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 … | |
Re: 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 … | |
Re: If you have Python3 instead of Python2 you use input, it is same as raw_input in Python2. | |
Re: You must set starting state in init. | |
Re: 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] | |
Re: Post your regex code, and give example of failure. | |
Re: Groupby needs sorted input, yours is not. | |
Re: 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. | |
Re: I do not understand purpose of ' 1 6 ' < = c h < = ' 2 5 ' | |
Re: 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 | |
Re: 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. | |
Re: 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 … | |
Re: It all depends what kind of effort you are showing. You should post your code this far with CODE tags. | |
Re: All indention is lost from the code snippets. | |
Re: 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. | |
Re: 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 ;) | |
Re: 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] … | |
Re: 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. | |
Re: You must show effort first. | |
Re: 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 … | |
Re: What you mean, last number goes up, second down all time. | |
Re: [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 … | |
Re: Similar example, I gave, but not directly answer in code. [url]http://www.daniweb.com/software-development/pascal-and-delphi/code/369890[/url] | |
Re: How about [code]sorted(set(open('f1')))[/code] | |
Re: 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 … | |
Re: Yes, because the 1,1 would be close to 0,0 and not be chosen. | |
Re: 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. | |
Re: 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, … | |
Re: [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: … | |
Re: You forgot your pseudocode and code. Do not forget to push CODE | |
Re: Data should be only one table according to principle of relational database and other tables should reference that table. | |
Re: You should use raw_input, and compare to string value 'a'. | |
Re: List is ordered and you mention sorting, so list of lists looks better. Difficult to tell without knowing more of your plans. | |
Re: [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] | |
Re: 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. | |
Re: 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 … | |
Re: 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. | |
![]() | Re: Apple Lisa OS was written in Pascal. What kind of extensions it had I do not know, but you can look up MacPascal. |
Re: Why wrong? You have the function defined at line 4. | |
Re: 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. |
The End.