e-papa commented: Thanks +1
Use
range(2, key_length)
Buy everything but Lenovo!
These folks install Superfish malware at the factory!
Buy one where the hard drive is not infected with spyware at the factory level.
To run older Python2 code examples with Python3 usually takes just a few modification. Python3 actually has a program called 2to3.py that will do that for you. Sooner or later it will be mostly Python3. So get version 3 of Python and a good IDE and start coding.
To see where you went wrong take a looky at:
inventwithpython.com/chapter14.html
PySide comes with drag and drop designer.
West coast all the way!
Seahawks best thing after chicken wings and cheap beer!
Linux sounds more and more interesting. Seems to have a lot of nice tools.
Try self.program_buttons
Andreas,
in any forum you will have people that don't know how to ask a proper question. I share with you the destain of folks that make their problem a guessing game for the helpers, slowly revealing what they actually want.
The with statement was introduced with Python 2.5 and the dictionary and set comprehensions with Python 2.7.3
So, what will this do?
with open('numbers.txt') as f:
for line in f:
print(line, type(line))
Ruby does not support keyword arguments in functions, something I have been using with Python a lot.
Stacks are used in assembly language a lot to store data. Python is a much higher language and stacks are rather transparent in use.
Python has a module called deque (double_ended_que) with methods appendleft() and popleft() which behave like the push and pop of a stack.
The built-in Python functions sorted() and sort() are highly optimized and a good choice to use.
I think you made a very good effort!
But you need to let folks know what you are after.
There is also a code snippet here at DaniWeb:
http://www.daniweb.com/software-development/python/code/216538/removing-outdated-files-python
Actually, once you get past the beer sold by the big makers, the best beers in the US are made by the many local Microbreweries.
Try this:
def lap_average(lap1, lap2):
# get minutes, seconds, centiseconds
m1, s1, c1 = [int(x) for x in lap1.split(':')]
m2, s2, c2 = [int(x) for x in lap2.split(':')]
# form a list of centisecond values
tlist1 = [m1*60*100, s1*100, c1]
tlist2 = [m2*60*100, s2*100, c2]
# get the total centiseconds
centis = sum(tlist1) + sum(tlist2)
# take integer average
centis = centis // 2
# get minutes, seconds from centiseconds
seconds, centis = divmod(centis, 100)
minutes, secs = divmod(seconds, 60)
print('-'*33)
print("Given lap times %s %s" % (lap1, lap2))
print("Average time = %02d:%02d:%02d" % (minutes, secs, centis))
# test times
lap_average('03:40:00', '05:20:00')
lap_average('03:00:02', '02:00:00')
lap_average('02:25:50', '06:50:75')
lap_average('00:02:00', '00:03:00')
lap_average('00:02:20', '00:04:40')
lap_average('02:40:40', '03:30:30')
lap_average('02:60:30', '60:40:40')
''' result ...
---------------------------------
Given lap times 03:40:00 05:20:00
Average time = 04:30:00
---------------------------------
Given lap times 03:00:02 02:00:00
Average time = 02:30:01
---------------------------------
Given lap times 02:25:50 06:50:75
Average time = 04:38:12
---------------------------------
Given lap times 00:02:00 00:03:00
Average time = 00:02:50
---------------------------------
Given lap times 00:02:20 00:04:40
Average time = 00:03:30
---------------------------------
Given lap times 02:40:40 03:30:30
Average time = 03:05:35
---------------------------------
Given lap times 02:60:30 60:40:40
Average time = 31:50:35
'''
The console screen can be cleared with multiple print():
for x in range(40):
print('')
Or simply:print('\n'*40)
If you have the Windows OS, maybe you should look into Portable Python and run your code from a USB flashcard, and also use the PyScripter IDE that comes with it.
For the free download and info see:
http://www.portablepython.com/releases/
Since you are a beginner I would download Python version 2.7.3 it makes working with older code examples much easier.
The average would be
(lap1 + lap2)/2
you would have to add the lap times as milliseconds then take the result and
recreate the minutes:seconds:milliseconds format
This may give you a few hints:
''' csv_rread102.py
find duplicate email addresses in a csv file
'''
# csv type test data
# name, surname, job_title, company, email
csv_data = '''\
Arden,Adam,clerk,ACME Tools,aaden@gmail.com
Bison,Bert,manager,Ideal Plumbing,bertbison@idel.com
Clark,Clara,assistant,ACME Tools,clarkc@gmail.com
Arden,Adam,supervisor,ACME Tools,aaden@gmail.com
Clark,Clara,receptionist,ACME Homes,clarkc@gmail.com
'''
import csv
fname = "aaa_test7.csv"
# write the test data file
with open(fname, "w") as fout:
fout.write(csv_data)
# read the test data file back in
with open(fname, "r") as fin:
reader = csv.reader(fin)
# create a dictionary of email:frequency pairs
email_freq = {}
for row in reader:
print(row) # test
name, surname, job_title, company, email = row
email_freq[email] = email_freq.get(email, 0) + 1
print('-'*70)
# refresh reader object
with open(fname, "r") as fin:
reader = csv.reader(fin)
# make a list of all rows that have duplicate emails
duplicate_emails = []
for row in reader:
name, surname, job_title, company, email = row
# add row to list if email frequency is above 1
if email_freq[email] > 1:
duplicate_emails.append(row)
# show results
import pprint
pprint.pprint(email_freq)
print('-'*70)
pprint.pprint(sorted(duplicate_emails))
''' my result >>>
['Arden', 'Adam', 'clerk', 'ACME Tools', 'aaden@gmail.com']
['Bison', 'Bert', 'manager', 'Ideal Plumbing', 'bertbison@idel.com']
['Clark', 'Clara', 'assistant', 'ACME Tools', 'clarkc@gmail.com']
['Arden', 'Adam', 'supervisor', 'ACME Tools', 'aaden@gmail.com']
['Clark', 'Clara', 'receptionist', 'ACME Homes', 'clarkc@gmail.com']
----------------------------------------------------------------------
{'aaden@gmail.com': 2, 'bertbison@idel.com': 1, 'clarkc@gmail.com': 2}
----------------------------------------------------------------------
[['Arden', 'Adam', 'clerk', 'ACME Tools', 'aaden@gmail.com'],
['Arden', 'Adam', 'supervisor', 'ACME Tools', 'aaden@gmail.com'],
['Clark', 'Clara', 'assistant', 'ACME Tools', 'clarkc@gmail.com'],
['Clark', 'Clara', 'receptionist', 'ACME Homes', 'clarkc@gmail.com']]
'''
Evolutionists:
An almost chicken laid the egg the first real chicken came out off.
Creationists:
God made the chicken and then it laid an egg.
Putting up with relatives and missing my friends.
Oh my goodness, you are mixing a recursive fibonacci number function with an attempt to make a sliced list. A recipe for disaster.
You need to update the canvas as shown. Otherwise the Tkinter event loop just waits till time.sleep(5) is done.
import ImageTk
import tkMessageBox
from Tkinter import*
from turtle import *
import time
root = Tk() #main window
canvas = Canvas(root, width=800, height=480, bg="white")
canvas.grid(column=1, rowspan=6, row=0, columnspan=5)
start=ImageTk.PhotoImage(file="start.gif")
after_prymase2a=ImageTk.PhotoImage(file="after_prymase2a.gif")
canvas.create_image(200,400,image=start)
canvas.update() # needed to do one pic at a time
time.sleep(5)
canvas.create_image(100,100,image=after_prymase2a)
root.mainloop()
Don't jump the gun and download Python 3.3 yet, it still has a few bugs.
Download the much more stable Python 3.2
Just a note, Python3 does not contain string.letters
Now I have a few questions to out international friends.
This is what I get from my computer here in the USA:
# run on a computer set to English language
import string
eng_letters = string.letters
print(eng_letters)
'''my result -->
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
'''
Here are my questions:
What does string.letters look like in your country?
Does isalpha() include the alphabet of your country?
Could you post the results of string.letters so we can use that constant?
(Maybe they are published somewhere?)
pyTony's code does not work on my US computer, it gives False for his name.
Maybe this will help you:
import string
allowed_alpha = string.ascii_letters + string.whitespace
# a test name
name = "Mark Zumkoff"
# gives False because of space
print(name.isalpha())
# this test allows spaces
if all(c in allowed_alpha for c in name):
print(name)
else:
print("not an allowed name")
I am glad you could make it work! Nice job and interesting project!
This might help:
# sum some data of a csv file
raw_data = '''\
Station Name,Lat,Long,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
Test 1,45.125478,-105.623154,3.12,0.15,0.08,0.61,0.67,1.24,2.32,1.06,0.64,0.07,0.32,1.02
Test 2,42.854123,-106.321587,0.09,3.15,1.61,0.03,0.84,1.62,3.01,1.51,0.81,0.02,0.23,1.09
Test 3,43.974532,-105.896214,2.65,2.01,0.05,3.02,1.05,0.08,0.08,1.06,0.43,0.65,0.12,1.06
'''
# create the test data file
fname = "mydata.csv"
with open(fname, "w") as fout:
fout.write(raw_data)
# read the data file
data_list = []
for line in open(fname):
# remove trailing newline char
line = line.rstrip()
# create a list
line_list = line.split(',')
data_list.append(line_list)
# create a months dictionary with month:index pairs
mdict = {}
for ix, item in enumerate(data_list[0]):
print(ix, item) # test
if ix > 2:
mdict[item] = ix
print(mdict) # test
print('-'*70)
month_start = 'Apr'
month_end = 'Jul'
new_list = []
for item in data_list[1:]:
#print(item) # test
station = item[0]
lat = item[1]
long = item[2]
start = mdict[month_start]
end = mdict[month_end]+1
plist = [float(x) for x in item[start : end]]
print(plist) # test
mysum = sum(plist)
new_list.append([station, lat, long, mysum])
print('-'*70)
print("Result:")
for item in new_list:
print(item)
Mutable objects can change their value but keep their id().
Pick Java so Oracle can sue you if you make money like they did with Google?
Nice clean start!
The Python frog module is a more advanced turtle module with sound and such:
http://pypi.python.org/pypi/frog/0.87
Should be very interesting for children.
You can use woooee's code, just replace raw_input with input
Execution speed is probably of limited interest for a beginner. Concentrate on learning the basics of Python rather thoroughly. Any program that has display updates and disk access in it, is limited a lot by those operations anyway! Either Python 2.6 or Python 2.7 will do fine for you. Why not use Python 2.7, the most advanced of the Python2 versions.
Are brothel sprouts the children of prostitutes?
Looks like this thread is solved.
Oh boy, Jaro's tab indented code looks like dung on a shingle.
Why would you need to update?
Also, Python32 works just fine!
Take a quick look at:
http://www.daniweb.com/software-development/python/threads/191210/867414#post867414
Here is a typical example of multiple radiobuttons using the Tkinter GUI toolkit:
# exploring multiple Tkinter radio buttons
# radio buttons only allow one item to be selected/checked
# ene
try:
# Python2
import Tkinter as tk
except ImportError:
# Python3
import tkinter as tk
def rb_selected():
# show checked/selected radio button item
ix = rb_v.get()
label['text'] = 'you selected %s' % mylist[ix]
root = tk.Tk()
# used by the radio buttons as index
rb_v = tk.IntVar()
mylist = [
'apple',
'orange',
'banana',
'pear',
'apricot'
]
# list(range()) needed for Python3
rb = list(range(len(mylist)))
for ix, text in enumerate(mylist):
# command is optional and responds to any rb changes
rb[ix] = tk.Radiobutton(root, text=text, value=ix,
variable=rb_v, command=rb_selected)
rb[ix].grid(row=ix, column=0, sticky='w')
label = tk.Label(root, width=20)
label.grid(row=ix+1, column=0, pady=5, sticky='w')
# you can preset one radio button
# default is first button (ix=0)
rb_v.set(2)
# show initial selection
rb_selected()
root.mainloop()
I am sure you can adapt it to your purposes easily. Just play around with the code a little.
Change
branch = "%s" % lines[0][1].strip(': ')
to
branch = "%s" % lines[0][1].strip()
to get rid of the newline character
Python has a pretty fast algorithm built-in called an adaptive merge sort. Here is an example:
# sort a list of names without changing the original list
# uses the high speed sorting algorithm built-into Python
name_list1 = ['Frank', 'Mark', 'Hank', 'Zoe', 'Bob', 'Carl', 'Al']
name_list2 = sorted(name_list1)
print "Original:"
print name_list1
print "Sorted:"
print name_list2
"""my output -->
Original:
['Frank', 'Mark', 'Hank', 'Zoe', 'Bob', 'Carl', 'Al']
Sorted:
['Al', 'Bob', 'Carl', 'Frank', 'Hank', 'Mark', 'Zoe']
"""
If you have to create your own sorting algorithm, go with a selection sort. It is relatively slow, but rather easy to understand.
A selection sort of a list of numbers is pretty simple. You start
with two lists, let's call the original unsorted list the start_list
and you have another list call it the end_list which is empty at
the start.
If you want to sort ascending (lowest value first) you get the lowest
value of start_list using lowest = min(start_list) and append it to
the end_list with end_list.append(lowest). Now remove this value from
the start_list with start_list.remove(lowest) and repeat until the
start_list is empty and return the end_list with the sorted values. The repeat can be done with a while loop.
I would say your indentations are a little screwy. Indentations are very important for Python to designate statement blocks.