Help is here:
http://web.chem.ucsb.edu/~kalju/MonteCarlo_3.html
sneekula 969 Nearly a Posting Maven
My 1997 Dodge Ram pickup truck still runs pretty well, but the rust is getting to it.
Strange, pyserial has a class Serial()
Did you accidentally save your code file as serial.py?
Python is case sensitive, so try:
ser = serial.Serial("COM5", 9600)
Couldn't you simply do this:
digits = '234'
size = len(digits)
sum_odd = 0
for ix in range(1, size, 2):
sum_odd += int(digits[ix])
prod_even = 1
for ix in range(0, size, 2):
prod_even *= int(digits[ix])
print("sum of odd indexed integers = {}". format(sum_odd))
print("product of even indexed integers = {}". format(prod_even))
Here is a typical example:
def funk(arg1, arg2, arg3):
pass
arg1 = 77
# call function with only one argument
funk(arg1)
''' error -->
Traceback (most recent call last):
File "<module1>", line 5, in <module>
TypeError: funk() takes exactly 3 arguments (1 given)
'''
At this time PyScripter will not work with Python34, too bad!
Looks like I have to bring up IDLE or IDLEX for each version of Python.
Ninja looks great, but again fails the input test, at least on a Windoze machine. The output console does not show the prompt or what you type in until Enter is pressed. Pretty bad!
Since Python has this miserable version two-three mess right now, it would be nice to have an IDE that let's the user select the version to run.
Editra that comes in the wx folder allows you to easily do that, but it does not handle input() well at al.
The newer Portable Python 2.7.6.1 package contains the following applications/libraries:
PyScripter v2.5.3
PyCharm Community Edition 3.1.2 (Thanks to cebik)
NymPy 1.8.1
SciPy 0.13.3
Matplotlib 1.3.1
PyWin32 218
Django 1.6.2
PIL 1.1.7
Py2Exe 0.6.9
wxPython 3.0.0.0
NetworkX 1.7
Lxml 3.3.4
PySerial 2.7
PyODBC 3.0.7
PyGame 1.9.1
PyGTK 2.24.2
PyQt 4.10.4
IPython 1.2.1
Pandas 0.11.0
Dateutil 2.2
PyParsing 2.0.1
Six 1.6.1
XLRD 0.9.2
XLWT 0.7.5
XLUtils 1.7.0
OpenPyXL 1.8.5
The best way to run Python on a Windoze machine! Since MS tries so hard to discourage Python users.
Now you have three IDEs available:
Pyscripter (PyScripter-Portable.exe)
Pycharm (PyCharm-Portable.exe)
IDLE (IDLE-Portable.exe)
There is also the old Pythonwin.exe in the site-packages folder.
Well, it is time to plan for Christmas 2014!
I would use this approach, use whatever .gif file you have, sorry this is from my iPad and it does not work well with DaniWeb commands:
,,,
display an image using Tkinter
for Python2
'''
import Tkinter as tk
root = tk.Tk()
image_file = "Vacation2013.gif"
photo = tk.PhotoImage(file=image_file)
root.title(image_file)
label = tk.Label(root, image=photo)
label.pack(padx=5, pady=5)
root.mainloop()
Majid, please do not hijack a thread with an unrelated question!
Which version of Python?
Santa has no religious meaning. He is a jolly soul that represents gift giving, flying sleds and obesity. Let's name the whole thing Santaday.
As an exercise you could create all those buttons in a loop.
You may want to help the user in case of an invalid integer entry.
There are a number of functions that need integer arguments.
How do you run PyPy?
Hint:
# assume this is your data file (CSV type)
# each line has last name, first name, grade
data = '''\
Miller,Jean,75
Paulson,Peter,47
Tanner,Abraham,88
Norton,Sheila,33
'''
filename = "grades123.txt"
# write a test file
with open(filename, "w") as fout:
fout.write(data)
# now read your test file back in and process the data
with open(filename, "r") as fin:
for line in fin:
# remove trailing newline char
line = line.rstrip()
# split the line at the commas
line_list = line.split(",")
print(line, line_list) # test
# unpack the line
last, first, grade = line_list
# now check if int(grade) <= 50
# and tell person (last first) if they failed
You will have to work with more than one grade in your data line.
Here would be one number of hints:
'''
file data1.txt looks like this:
A233;Apple;FOOD;1;5.00
A345;Blanck CD 10;DATA;10;0.25
B453;Pasta;FOOD;1;12.00
T545;USB-Memory 16GB;DATA;1;25
each line represents:
identity, name, category, quantity, price
'''
class Billing(object):
def __init__(self, identity, name, category, quantity, price):
self.identity = identity
self.name = name
self.category = category
self.quantity = quantity
self.price = price
# create a list of instances of class Billing
bill_list = []
for line in open("data1.txt"):
#print(line) # test
#print(line.split(';')) # test
identity, name, category, quantity, price = line.split(';')
quantity = int(quantity)
# remove trailing new line char from price
price = price.rstrip()
price = float(price)
#print(identity, name, category, quantity, price) # test
bill_list.append(Billing(identity, name, category, quantity, price))
print("------------------ F O O D -------------------")
header = ('Product', 'Qty', 'Price/each', 'Price total')
print("%-18s %-5s %-6s %-10s" % (header))
total_food = 0
for bill in bill_list:
if bill.category == "FOOD":
totalprice = bill.quantity * bill.price
sf = "%-18s %2d %10.2f %12.2f"
print(sf % (bill.name, bill.quantity, bill.price, totalprice))
total_food += totalprice
print("Total food = %0.2f" % total_food)
print('-'*48)
print("------------------ D A T A -------------------")
header = ('Product', 'Qty', 'Price/each', 'Price total')
print("%-18s %-5s %-6s %-10s" % (header))
total_data = 0
for bill in bill_list:
if bill.category == "DATA":
totalprice = bill.quantity * bill.price
sf = "%-18s %2d %10.2f %12.2f"
print(sf % (bill.name, bill.quantity, bill.price, totalprice))
total_data += totalprice
print("Total data = %0.2f" % total_data)
You still have to bring in your identity file and then search for bill.identity matches.
You need to change your first data file format to something like this, where for instance the ';' character is the separator:
A233;Apple;FOOD;5.00
A345;Blanck CD 10;DATA;2.50
B453;Pasta;FOOD;12.00
T545;USB-Memory 16GB;DATA;25
Another option is to make a DailyExpenses.pth text file that contains the directory name where you can find the DailyExpenses.py file. You can save the path file in Python's "site-packages" directory.
Global Warming is also used for all sorts of bad legislation. Another Ice Age will solve this.
Mutation to a real chicken would happen in the egg, so the egg comes first.
Note that wxPython is the Python wrapper for the C/C++ based wxWindows.
You could also use a list of instances of class Persons like shown in the example here:
# format: ni_number firstname lastname age
data_str = '''\
55512 Bart Simpson 45
45622 John Smith 58
46231 Alicia Sands 27
'''
fname = "data123.txt"
# write test data file
with open(fname, "w") as fout:
fout.write(data_str)
class Persons:
def __init__(self, ni_number, name, age):
self.__ni_number = ni_number
self.__name = name
self.__age = age
def show(self):
'''for a quick test'''
# formating string
sf = " ni_number = {}\n name = {}\n age = {}\n"
return sf.format(self.__ni_number, self.__name, self.__age)
# read the data file back in and process
with open(fname, "r") as fin:
person_list = []
for line in fin:
mylist = line.split()
print(mylist) # test
# unpack the list
ni_number, first, last, age = mylist
name = first + ' ' + last
# create a list of instances of class Persons
person_list.append(Persons(ni_number, name, age))
print('-'*20)
# test one of the instances
inst1 = person_list[0]
print(inst1.show())
''' console output -->
['55512', 'Bart', 'Simpson', '45']
['45622', 'John', 'Smith', '58']
['46231', 'Alicia', 'Sands', '27']
--------------------
ni_number = 55512
name = Bart Simpson
age = 45
'''
Hint:
class Sentence:
def __init__(self, text):
self.text = text
self.word_list = self.text.split()
#print(self.word_list) # test
def get_first_word (self):
return self.word_list[0]
def get_all_words(self):
"I let you work out what to return"
pass
# create an instance of class Sentence(text)
# this is your input text
text = "I'm going back"
sent1 = Sentence(text)
print(sent1.get_first_word()) # I'am
# get the whole text
print(sent1.text) # I'm going back
# a hint for module sent1.get_all_words()
# create the words from self.word_list
for word in sent1.word_list:
print(word)
'''
I'm
going
back
'''
Your input to the class is your sentence/text, so put that into the class constructor __init__()
as a parameter after self. Note that by convention class names are capitalized, it makes code much more readable:
class Sentence:
def __init__(self, text):
self.text = text
def get_first_word (self):
word_list = self.text.split()
return word_list[0]
# create an instance
# Sentence(text)
# this is your input text
text = "Listen to the whisper, or wait for the brick."
sent1 = Sentence(text)
print(sent1.get_first_word()) # Listen
# set up another instance
text = "My karma ran over your dogma."
sent2 = Sentence(text)
print(sent2.get_first_word()) # My
At this point you may want to actually study up on Python classes, you are making some major mistakes.
class Schedule:
def __init__(self, course, points, level):
self.course= course
self.points = points
self.level = level
def get_course(self):
return self.course
# create instances
# Schedule(course, points, level)
sched1 = Schedule("Programming101", 4, 1)
sched2 = Schedule("British103", 5, 3)
sched3 = Schedule("Biology102", 2, 1)
print(sched3.get_course()) # Biology102
# or
print(sched3.course)
print(sched1.points) # 4
Some observations are due:
class Patient(object):
# by convention class names are capitalized
def __init__(self, ID, name, bdate, gender, phone, address):
# assing class parameters to instance self
self.ID = ID
self.name = name
self.address = address
# and so on
def get_name(self):
"this is a class method, first arg is self"
print("Patient name = %s" % self.name)
def set_name(self, newname):
"method to change the name"
self.name = newname
# create an instance of the class
# data have to be in this order
# Patient(ID, name, bdate, gender, phone, address)
john = Patient("5640","John","8/8/88","m","334-456-7898","60 Hilltop")
# get ID
print(john.ID)
# use a method of the class instance
john.get_name()
# change the name
john.set_name("Johnathan Miller")
# now
john.get_name()
'''
5640
Patient name = John
Patient name = Johnathan Miller
'''
Did you correct your code to this?
def inn():
print position.get()
if position.get()=='1':
xx,yy=10,10
elif position.get()=='2':
xx,yy=60,60
elif position.get()=='3':
xx,yy=110,110
window.create_image(xx,yy,image=pol3)
top.destroy()
Example:
class AirlineTicket:
# by convention capitalize class names
def __init__(self, name, orig, dest, travel_date, travel_class, price):
# assign parameters to the instance self
self.name = name
self.origination = orig
self.destination = dest
self.travel_date = travel_date
self.travel_class = travel_class
self.price = price
def info(self):
# format the info string
info_str = " %s travels %s class\n from %s to %s on %s"
info = info_str % (self.name, self.travel_class, self.origination,
self.destination, self.travel_date)
return info
# AirlineTicket(name, orig, dest, travel_date, travel_class, price)
# gether data
name = "Ted Trump"
orig = "JFK"
dest = "LAX"
travel_date = "12/23/2012"
travel_class = "first"
price = 1820.50
# create unique instance for this traveler
tedtru = AirlineTicket(name, orig, dest, travel_date, travel_class, price)
# test this instance
print(tedtru.info())
'''my result >>>
Ted Trump travels first class
from JFK to LAX on 12/23/2012
'''
Sorry, rrashkin (Robert Rashkin) is correct.
If you use Tkinter you can check this nice example:
http://www.daniweb.com/software-development/python/threads/191210/python-gui-programming/11#post1902460
First of all, let us know which GUI toolkit you are using and give us some of your pertinent code.
from connect4cell2 import Connect4Cells
make sure the file you are importing is saved as connect4cell2.py
self.cells = Connect4Cells(rows, columns, 3)
self.cells can now use any of the instances methods like
row_flag = self.cells.checkRow(rowIndex)
you have to supply the rowIndex value
line 15
return estimate
should be outside the loop
You can insert a print statement for testing the progress, and see how the higher values bubble to the end and lower values bubble to the start of the list:
myList=[43,21,12,80,3,2,35]
print(myList)
print('-'*30)
end = len(myList)-1
while (end != -1):
swapped = -1
for i in range(0, end):
if myList[i] > myList[i+1]:
temp = myList[i]
myList[i] = myList[i+1]
myList[i+1]= temp
print(myList) # test print to follow progress of sort
swapped = i
end = swapped
print('-'*30)
print(myList)
'''
[43, 21, 12, 80, 3, 2, 35]
------------------------------
[21, 43, 12, 80, 3, 2, 35]
[21, 12, 43, 80, 3, 2, 35]
[21, 12, 43, 3, 80, 2, 35]
[21, 12, 43, 3, 2, 80, 35]
[21, 12, 43, 3, 2, 35, 80]
[12, 21, 43, 3, 2, 35, 80]
[12, 21, 3, 43, 2, 35, 80]
[12, 21, 3, 2, 43, 35, 80]
[12, 21, 3, 2, 35, 43, 80]
[12, 3, 21, 2, 35, 43, 80]
[12, 3, 2, 21, 35, 43, 80]
[3, 12, 2, 21, 35, 43, 80]
[3, 2, 12, 21, 35, 43, 80]
[2, 3, 12, 21, 35, 43, 80]
------------------------------
[2, 3, 12, 21, 35, 43, 80]
'''
Also the C type swap
temp = myList[i]
myList[i] = myList[i+1]
myList[i+1]= temp
can be replaced with a Python tuple swap
myList[i+1], myList[i] = myList[i], myList[i+1]
To pass an argument in the button command use lambda:
try:
# Python2
import Tkinter as tk
except ImportError:
# Python3
import tkinter as tk
def click(s):
"""called with lambda"""
if s == "one":
root.title("Button one has been clicked")
else:
root.title("Button two has been clicked")
root = tk.Tk()
b1 = tk.Button( text="one", command=lambda: click("one") )
b1.pack(padx=150, pady=10)
b2 = tk.Button( text="two", command=lambda: click("two") )
b2.pack(pady=10)
root.mainloop()
You could create your buttons in a loop, but it would be quite cumbersome. It would be difficult to maintain a meaningful variable name for each button. Since you only have 6 buttons it wouldn't be worth it.
# find the version of Python you are using
import platform
print(platform.python_version())
This is really a question of what version of Python you are using.
If you use Python2 then input() is for numeric input, but with Python3 you will get strings.
With Python27 this will work:
# Python27
def main():
print("Adding:")
a = input("Please select a number:")
b = input("And another one:")
c = input("One more please:")
d = a+b+c
print(d)
main()
'''possible result ...
Adding:
Please select a number:1
And another one:2
One more please:3
6
'''
What does this little test do?
import csv
csv_fn = 'out.csv'
out_csv = csv.writer(open(csv_fn, 'wb'))
print(out_csv)
So we have no private attribute as C++ in python.yes?
has python any thing about machine for hardware managmente or some thing like?
for example if we want to write a program that works on a set such as CNC or other sets such as this?
Yes, Python has private attributes, but it uses "name mangling" to implement it. For all practical purposes this is good enough!
Looks like you need the following information:
customer name
shipping address
unique tracking number
item
number of items
cost per item
method of payment
Need to calculate:
total cost of all items
shipping cost
total to be paid
Has it been paid?
Has it been shipped?
I would create a class Order
with the following methods:
new_order
update_order
show_orders
how can I develop a program in PASCAL that counts the number of words in a paragraph
Pascal is a very old style language. I don't think it would come even close to Python's modern syntax and features.
You could try the Delphi/Python forum at DaniWeb.
The closest thing you could use to take advantage of modern language concepts is Python for Delphi:
http://mmm-experts.com/Products.aspx?ProductID=3