324 Posted Topics
Re: In Python3 to get just a new line you have to use print("") | |
| |
Re: Python forces you to do proper indentations. Yes, it does make the code easier to maintain. Comments only if needed for clarity. | |
Re: Good args for isinstance() vs. type() http://www.siafoo.net/article/56 | |
Re: For repetitive things use functions. | |
Re: I like http://projecteuler.net/problem=59 it's actually practical. | |
Explore the Python module bisect to search a sorted list and insert elements in the proper location in a sorted list. | |
Re: I don't like bloated IDEs like Eclipse. Even Eric (written in Python and PyQT) is rather large and sometimes sluggish. The small IDLE IDE that comes with Python is remarkably elegant for its size. On my Windows machine I use PyScripter (an exe written in Delphi). | |
Re: You might tell us what GUI toolkit you are using. What is repeatY in line 10? | |
Re: Something like **C:\Python33\Lib\site-packages** is a directory Python will automatically look into. | |
Re: Googling gets you to http://factspy.net/top-10-most-polluted-places-around-the-world/ | |
| |
Re: With the Tkinter GUI toolkit it is much simpler to use GIF files: '''Base64encodedGIF.py create a base64 encoded image string from a .gif image file changes the bytes of an image file into printable char combinations You can then copy and paste the result into your program. With Tkinter you … | |
Re: Something else: class HoldAttr(object): pass a = HoldAttr() print(hasattr(a, 'myattr1')) # False # create the attribute externally # unless the class uses __slots__ a.myattr1 = [] print(hasattr(a, 'myattr1')) # True # test it a.myattr1.append(1) print(a.myattr1) # [1] | |
Re: import random # by default seeds with the system time random.seed() # random.randrange([start], stop[, step]) mylist = [] for k in range(10): # produce integers between 0 and 99 n = random.randrange(0, 100) mylist.append(n) print(mylist) print("sum = %d" % sum(mylist)) print("avg = %f" % (sum(mylist)/len(mylist))) '''possible result >>> [32, 23, … | |
Re: Using a oneliner seems to be very pythonic, but may be hard to comprehend for mere mortals. So here is an alternative with comments to help you: # each line has these space separated data # first last ID credit-hours quality-points raw_data = ''' Max Medium 12345678 58 152 Jane … | |
Re: Where do you find module pytest? | |
Re: I agree with rrashkin, the csv module is overkill here. Here is an example how you can do this with simple Python stuff: # test data showing depth, time and temperature using ';' as a delimiter raw_data = """\ Depth (m);15.08.2012 15:39:09;15.08.2012 16:09:10;15.08.2012 16:39:10;15.08.2012 16:43:36 0;53.218;52.804;52.865;51.202 0.128;53.107;52.709;52.414;52.141 1.143;52.205;51.88;51.664;51.234 2.159;51.026;50.846;50.842;51.258 3.174;50.061;50.055;50.457;50.19 … | |
Re: Example: http://www.daniweb.com/software-development/python/threads/20774/starting-python/17#post1892126 | |
Seconds since epoch should be zero, but are not: '''timetuple1.py create a time tuple for epoch 1/1/1970 00:00:00 however, seconds since epoch does not give 0 ''' import time timetuple = time.strptime("01/01/1970 00:00:00", "%m/%d/%Y %H:%M:%S") print(timetuple) print('-'*40) # seconds since epoch 1/1/1970 00:00:00 secs = time.mktime(timetuple) print(secs) '''my result >> … | |
Method onPaint does not change the color. Does anyone know why? (there are no errors received) # explore the wxChoice widget # a simple ComboBox (dropdown box) that combines a ListBox with an EditBox # can't get the self.onPaint method to work!!!!! import wx ID_CHOICE = 120 ID_PANEL = 130 … | |
Re: Your sample data file has errors in it. Let's assume this is a corrected file: # uses comma + space as data separator test_data = '''\ ID, Last, First, Lecture, Tutorial, A1, A2, A3, A4, A5 10034567, Smith, Winston, L01, T03, 6, 5.5, 8, 10, 8.5 10045678, Lee, Bruce, L02, … | |
Re: Here is an example of how it should be done: # initial value total_sold = 0 for x in range(3): sold = int(input("Enter number of items sold: ")) total_sold += sold print(total_sold) # test If you put the initial value in the loop, it would continually reset. | |
Re: Like Schol-R-LEA (Joseph Osako) pointed out the rukes have to be ''' Let's assume the rules are as follows Word length: < 3: 0 points 3-6: 1 point per character in word 7-10: 2 points per character in word 11+: 3 points per character in word ''' for you to … | |
| |
Re: Sounds like a fun project! Another possibility: text = """\ arguing with a software engineer is like wrestling with a pig in the mud after a while you realize the pig is enjoying it """ print("Longest word = {}".format(max(text.split(), key=len))) | |
Re: It might be simpler to have the user enter the Fahrenheit value as 32F. Your program finds the 'F' and knows that the result has to be Celsius. Conversely for Celsius values entered ...... ts = raw_input("Enter temperature like 32F or 36C : ").upper() if 'F' in ts: tf = … | |
Re: It might be better to use a record than a deeply nested list, see: http://www.daniweb.com/software-development/python/code/431887/the-employee-class-record-revisited The class can be originally something like this: class Movie(): """ mimics a C Structure or Pascal Record """ def __init__(self, title, year, director, cast): self.title = title self.year = year self.director = director self.cast … | |
| |
Re: People that buy guns should first pass a mental competency test, and so should Politicians. | |
Re: This might help to get you started: http://www.daniweb.com/software-development/python/threads/32007/projects-for-the-beginner/14#post1840242 | |
Re: Note that you can only join a list of strings. | |
![]() | Re: Mild corrections to make this work: var_file = raw_input('nof$ ') #nof, number of files. try: text = raw_input('ttwtf$ ') #ttwtf, text to write to files. print text, ">>> ", var_file except ValueError: print "Error: Not a string" #main() |
Re: Here is a related example using the Python dictionary: # to simplify things create a comma separated value (CSV) file data = """\ keith,100,50,puppy bill,300,32,cat frank,200,9x9,mouse """ fname = "mydata.csv" # write the test data file with open(fname, "w") as fout: fout.write(data) # read the data in line by line … | |
Re: Check out your python documentation under 'ctypes' or use: import ctypes help('ctypes') | |
Re: Since all the troublemakers have to be imported, I would change 'os' to 'import' like this: print("A math expression is something like 3+5+7 (no spaces)") math_expression = raw_input("Enter a math expression: ") if 'import' not in math_expression: result = eval(math_expression) print("Result of %s is %s" % (math_expression, result)) | |
Re: You can use the Tkinter GUI toolkit that ships with Python: # KeyLogger.py # show a character key when pressed without using Enter key # arrow keys are 'Up' 'Down' 'Left' 'Right' in event.keysym # hide the Tkinter GUI window, only console shows # Python3 change Tkinter to tkinter (now … | |
![]() | Re: run help('import') My question, why would you want to remove an imported module? |
Re: You can quickly test these things out: data_text = """\ one two three """ # write the data text to a text file fname = "mydata1.txt" with open(fname, "w") as fout: fout.write(data_text) # read data back in as a text string # the with block will close the file for … | |
Re: A small test code: line = 'abcdefghijklmnopqrstuvwxyz' print line print line[17] if line[17] == 'r': line = line[0:17] + 'R' + line[18:len(line)] print line '''result >> abcdefghijklmnopqrstuvwxyz r abcdefghijklmnopqRstuvwxyz ''' | |
Re: If you use the wxPython GUI toolkit, there is also the wxFormBuilder program, very nice and free. See the examples at: http://www.daniweb.com/software-development/python/threads/128350/starting-wxpython-gui-code/8#post1802067 http://www.daniweb.com/software-development/python/threads/259746/wxformbuilder-support-wxpython#post1129943 | |
Re: If you need several integer values in your file, do this: # write a test file using several integers data = """\ 177 233 567 876 """ with open('money.$','w') as fout: fout.write(data) # now read the test file back in as a list money_list = [] for line in open('money.$','r'): … | |
Re: Well, water is not hydrogen. It takes a large amount of energy to create hydrogen from water. All Toyota Prius owners hate to see the time their super expensive battery dies on them a few years down the road. | |
Re: Some programs have problems with spaces in path names. | |
Re: Once you get the hang of using the languages required indentations for blocks of code, Python is much like a pseudo language and rather powerful. Simple example: [code]food = 'fish', 'fowl', 'fruit', 'veggies', 'meat' # show all food items for item in food: print(item) # print 10 dashes print('-'*10) # … | |
Re: This may help: # a basic recursive function # there is a limit to recursions # sys.getrecursionlimit() --> usually 1000 # can be changed with # sys.setrecursionlimit(x) def factorial(n): """ just an example, would be poor choice because of the many calls, each call uses another stack frame """ # … | |
Re: You can keep the import local in a function: def mylocals(): import math print(vars()) mylocals() | |
Re: # you have to give b an initial value # so you can increment by 1 in the loop b = 0 while b <= 10: print (b) b += 1 | |
Re: What layout manager are you using? For instance the **grid** layout manager: # looking at the Tkinter grid() layout manager # # The size of a grid cell in a given row or column is # determined by its largest widget. # # If you give a grid layout more … | |
Re: # a very simple template to test PyQT widgets # PyQT free from: # http://www.riverbankcomputing.co.uk/software/pyqt/download # used Windows installer PyQt-Py3.2-x86-gpl-4.8.4-1.exe # tested with PyQT 4.8 and Python 3.2 from PyQt4.QtCore import * from PyQt4.QtGui import * app = QApplication([]) # ----- start your widget test code ---- # create the … |
The End.