Re: [Tutor] Need some help on output

2011-03-04 Thread Gaurav Malhotra
I am beginner...I want to know best book to start with
when it comes with python programming
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need some help on output

2011-03-04 Thread David Hutto
That depends on what your motivation is for learning python. I'd start
with a few hello world tutorial online. like print "hello world"/
python 3.0 print("hello world"), and on that not, decide on the
version you want to use on your system first.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to extract data from text to excel?

2011-03-04 Thread tee chwee liong

bug in the previous code so the final code should be:
 
import xlwt
"""Reads robert.txt
This is the final script that writes by incrementing each row but maintain
one column"""
  
# Create workbook and worksheet 
wbk = xlwt.Workbook() 
sheet = wbk.add_sheet('python')
row = 0  # row counter
f = open('robert.txt')
for line in f:
L = line.strip()
print L
for c in L:
print c
sheet.write(row,0,c)
row += 1

wbk.save('reformatted.data.xls')
 
___ Tutor maillist - 
Tutor@python.org To unsubscribe or change subscription options: 
http://mail.python.org/mailman/listinfo/tutor   
 ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Book Request [Was: Re: Need some help on output]

2011-03-04 Thread Alan Gauld

"Gaurav Malhotra"  wrote


I am beginner...


Welcome.
But when posting messages please start a new 
thread do not use an existing one. Otherwise your 
message is likely to be missed by anyone not 
reading the previous subject.



I want to know best book to start with
when it comes with python programming


Books/tutorials are very subjective. The style of tutorial 
that I like may not suit you. That having been said:


Are you a beginner to Python? Or a beginner to programming?
There are beginners pages for both categories on the 
python.org web site that list online tutorials. Choose a few

and try them out, they are all free.


One thing you will need to decide is whether to learn the 
latest Python Version 3 or to use the older Python Version 2.
They are not compatible and thee is more widespread support 
for V2, but V3 iscatching up. For now I would recommend a 
complete beginner go with Version 3 but if you already know 
another language then go with Version 2 (you will learn 
faster than a beginner and so may run into some current 
limitations of V3) 

Once you find a tutorial that suits your style then stick to it. 
Do the examples don't just read them. Post any questions 
or problems you encounter to this mailing list. Tell us the 
Python version, the tutorial you use and the Operating 
system. Include full error messages in posts, they may 
not look helpful to you but they are very helpful to us! :-).


Enjoy programming in Python.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help! (solution)

2011-03-04 Thread michael scott
I know that the question has long been answered (and probably due today), but I 
solved it and it was great exercise for me (as I'm not in college at the moment 
and I need assignments like these to gauge my skills). I'll probably build a 
gui 
for it tomorrow, just so I can practice at that. I wish I had a comp sci lab... 
(T_T) but I digress

But anyways Andrew here is an alternative way to solve the problem (even if it 
is a long and round about method). And to anyone else who is reading beside 
Andrew, if something about my code could be better, please tell me, as this was 
just as much a learning experience for me as it is for Andrew. I need 
constructive criticism at the moment so I don't develop bad habits.


def production_time():
creation_time = 127
time_till_rest = 18161
items = raw_input("How many items will be produced?\n> ")
item_time = int(items) * creation_time 
rest_times = item_time/time_till_rest
print rest_times
if rest_times > 0:
total = item_time + (313 * rest_times) #313 is 5 min and 13 secs in 
second form
else: total = item_time
time = sec_to_standard(total)
print "It would take %d days %d hours %d minutes and %d seconds to produce 
that many items" %(time[0], time[1], time[2], time[3])


def sec_to_standard(seconds):
day = 86400 #secs
hour = 3600 #secs
mins = 60#seconds
creation_time = 127 #secs
time_till_rest = 18161 #secs
days = 0
hours = 0
minutes = 0
secs = 0
if seconds > day:
while seconds > day:
print "doing days"
seconds = seconds - day
days += 1
if seconds > hour:
while seconds > hour:
print "doing hours"
seconds = seconds - hour 
hours += 1
if hours >= 24:
days += 1
hours -= 24
if seconds > mins:
while seconds > mins:
print "doing minutes"
seconds = seconds - mins
minutes += 1
if minutes > 60:
hours += 1
minutes -= 60
secs = seconds
return days, hours, minutes, secs

production_time()

 
What is it about you... that intrigues me so?





From: Andrew Bouchot 
To: tutor@python.org
Sent: Thu, March 3, 2011 4:28:33 PM
Subject: [Tutor] Help!


okay so this is my comp sci lab
 
Problem: ProductionTime.py It takes exactly 2 minutes and 7 second to produce 
an 
item. Unfortunately, after 143 items are produced, the fabricator must cool off 
for 5 minutes and 13 seconds before it can continue. Write a program that will 
calculate the amount of time required to manufacture a given number of items. 
Output: Output the amount of time D days HH:MM:SS Sample Input : numItems =1340 
Represents the numbers items to be manufactured Sample Output : 2 days 00:03:17 

 
this is the coding i have written for it!
numitems= int(raw_input("Enter the number of items needed to be manufactured:"))
seconds=numitems*127
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print "%d:%02d:%02d" % (h, m, s)
but how would i add the 5 min and 13 seconds after 143 items have been 
produced???


  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Re: Help! (solution)

2011-03-04 Thread Wolf Halton
Put the timing code for one itemt in a while loop and have the variable for
elapsed time incremented by the amount of time the fabricator has to cool
every time the modulus of the loop counter / 127 is 0 AND the count is above
0.

production = 0
time = 127 # seconds
timer = 0
rest = 313
run = input("Enter your run total: ")

while production != run:
timer = timer +  time
if run % production = 0:
timer = timer + rest

print "It took %i  seconds to produce %i items." % (timer, run)

d = 24*60*60
h = 60*60
m = 60
D = timer /  d # how many days
Dsec = timer %  d # seconds left after deducting the days
H = Dsec /  h # how many hours
Hsec = Dsec % h # seconds left after deducting hours.
M = Hsec /  m # how many minutes
Msec  = Hsec % m # seconds left after deducting minutes

print "Production time for %i items: %i Days, %i:%i:%i" % (run, D, H, M,
Msec)

# my 2 cents

-- Forwarded message --
From: "michael scott" 
Date: Mar 4, 2011 7:17 PM
Subject: Re: [Tutor] Help! (solution)
To: 

I know that the question has long been answered (and probably due today),
but I
solved it and it was great exercise for me (as I'm not in college at the
moment
and I need assignments like these to gauge my skills). I'll probably build a
gui
for it tomorrow, just so I can practice at that. I wish I had a comp sci
lab...
(T_T) but I digress

But anyways Andrew here is an alternative way to solve the problem (even if
it
is a long and round about method). And to anyone else who is reading beside
Andrew, if something about my code could be better, please tell me, as this
was
just as much a learning experience for me as it is for Andrew. I need
constructive criticism at the moment so I don't develop bad habits.


def production_time():
creation_time = 127
time_till_rest = 18161
items = raw_input("How many items will be produced?\n> ")
item_time = int(items) * creation_time
rest_times = item_time/time_till_rest
print rest_times
if rest_times > 0:
total = item_time + (313 * rest_times) #313 is 5 min and 13 secs in
second form
else: total = item_time
time = sec_to_standard(total)
print "It would take %d days %d hours %d minutes and %d seconds to produce
that many items" %(time[0], time[1], time[2], time[3])


def sec_to_standard(seconds):
day = 86400 #secs
hour = 3600 #secs
mins = 60#seconds
creation_time = 127 #secs
time_till_rest = 18161 #secs
days = 0
hours = 0
minutes = 0
secs = 0
if seconds > day:
while seconds > day:
print "doing days"
seconds = seconds - day
days += 1
if seconds > hour:
while seconds > hour:
print "doing hours"
seconds = seconds - hour
hours += 1
if hours >= 24:
days += 1
hours -= 24
if seconds > mins:
while seconds > mins:
print "doing minutes"
seconds = seconds - mins
minutes += 1
if minutes > 60:
hours += 1
minutes -= 60
secs = seconds
return days, hours, minutes, secs

production_time()


What is it about you... that intrigues me so?





From: Andrew Bouchot 
To: tutor@python.org
Sent: Thu, March 3, 2011 4:28:33 PM
Subject: [Tutor] Help!


okay so this is my comp sci lab

Problem: ProductionTime.py It takes exactly 2 minutes and 7 second to
produce an
item. Unfortunately, after 143 items are produced, the fabricator must cool
off
for 5 minutes and 13 seconds before it can continue. Write a program that
will
calculate the amount of time required to manufacture a given number of
items.
Output: Output the amount of time D days HH:MM:SS Sample Input : numItems
=1340
Represents the numbers items to be manufactured Sample Output : 2 days
00:03:17


this is the coding i have written for it!
numitems= int(raw_input("Enter the number of items needed to be
manufactured:"))
seconds=numitems*127
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print "%d:%02d:%02d" % (h, m, s)
but how would i add the 5 min and 13 seconds after 143 items have been
produced???
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help - want to display a number with two decimal places

2011-03-04 Thread lea-parker
HelloI have created the following code but would like the program to include two decimal places in the amounts displayed to the user. How can I add this?My code:# Ask user to enter purchase pricepurchasePrice = input ('Enter purchase amount and then press the enter key $')
# Tax ratesstateTaxRate = 0.04countrySalesTaxRate = 0.02
# Process taxes for purchase price
stateSalesTax = purchasePrice * stateTaxRatecountrySalesTax = purchasePrice * countrySalesTaxRate
# Process total of taxestotalSalesTax = stateSalesTax + countrySalesTax
# Process total sale pricetotalSalePrice = totalSalesTax + purchasePrice
# Display the dataprint '%-18s %9d' % ('Purchase Price',purchasePrice)print '%-18s %9d' % ('State Sales Tax',astateSalesTax)print '%-18s %9d' % ('Country Sales Tax',countrySalesTax)print '%-18s %9d' % ('Total Sales tax',totalSalesTax)print '%-18s %9d' % ('Total Sale Price',totalSalePrice)Thank you in advance for your help.Lea___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Shared web host and setting the python environment...

2011-03-04 Thread Modulok
List,

Background:
I'm on a linux based shared web host. They cater to PHP, not python.
(There's no third party python modules even installed!) So I installed
a virtual python in my home directory, plus easy_install and a bunch
of modules. Great!

The Problem:
Unfortunately, the web server uses the system python, not my virtual
python, or my modules. (It does execute my scripts as my uid though.)
I need modify the environment of every one of my python scripts,
perhaps appending to sys.path, so that my third party modules in my
home directory can be found. I have zero control over the web server
process or its environment.

Possible Solutions I thought of:
One way to do this would be to edit 'sys.path' at the top of every
python script. (Sounds ugly.) Another would be to import the 'user'
module and go that route. (Fine, but it's depricated. What replaces
it?) A third idea would be to re-direct all requests via mod_rewrite
to a single python file or shell script, which sets the environment
and calls my python scripts as a sub-process. (Slow, which is fine,
but some security concerns here.)

Given my situation, what's the best way to accomplish this? (Aside
from a new hosting company or a virtual private server.)

Any suggestions appreciated. Thanks!
-Modulok-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help - want to display a number with two decimal places

2011-03-04 Thread Modulok
On 3/4/11, lea-par...@bigpond.com  wrote:
> Hello
>
> I have created the following code but would like the program to include two
> decimal places in the amounts displayed to the user. How can I add this?
>
> My code:
>
>
> # Ask user to enter purchase price
> purchasePrice = input ('Enter purchase amount and then press the enter key
> $')
>
> # Tax rates
> stateTaxRate = 0.04
> countrySalesTaxRate = 0.02
>
> # Process taxes for purchase price
>
> stateSalesTax = purchasePrice * stateTaxRate
> countrySalesTax = purchasePrice * countrySalesTaxRate
>
> # Process total of taxes
> totalSalesTax = stateSalesTax + countrySalesTax
>
> # Process total sale price
> totalSalePrice = totalSalesTax + purchasePrice
>
> # Display the data
> print '%-18s %9d' % ('Purchase Price',purchasePrice)
> print '%-18s %9d' % ('State Sales Tax',astateSalesTax)
> print '%-18s %9d' % ('Country Sales Tax',countrySalesTax)
> print '%-18s %9d' % ('Total Sales tax',totalSalesTax)
> print '%-18s %9d' % ('Total Sale Price',totalSalePrice)
>

Lea,

Notice that 'd' in your string substitution means integers, not
floats. Any decimal places will be truncated when using 'd'. For
floats use 'f' instead. You an also specify a precision, such as
'%.2f' shows two decimal places.

However, to make all of your numbers line up nicely, regardless of how
long they are, you need to look into advanced string formatting via
the builtin string method 'format()'. It takes a little practice to
understand and use, but the results are exactly what you want.
(See: http://docs.python.org/library/stdtypes.html#str.format)

Here's an example you could put at the bottom of your code to see it in action:

print "{0:<18} {1:>18.2f}".format("Country Sales Tax", countrySalesTax)
print "{0:<18} {1:>18.2f}".format("Purchase Price", purchasePrice)
# First prints the first argument (the 0th index) to 'format(),
# left aligned '<', and 18 characters wide, whitespace padded.
# Then prints the second argument, (the 1st index) right aligned,
# also 18 characters wide, but the second argument is specified to
# be a float 'f', that has a precision of 2 decimal places, '.2'.


-Modulok-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Error while using calendar module in Python 2.7

2011-03-04 Thread ranjan das
I ran the following code in python 2.6 and then in python 2.7 (using
calendar module) to manipulate dates and times

The following code works fine in Python 2.6 but throws up an error in Python
2.7. Can anyone please say why?

CODE:


import datetime
import calendar



while monday.weekday() != calendar.MONDAY:

monday -= oneday

oneweek = datetime.timedelta(days=7)

nextweek = today + oneweek

print next week




Error:
Traceback (most recent call last):
  File "C:\Python27\Foursoft\calendar.py", line 33, in 
while friday.weekday() != calendar.FRIDAY:
AttributeError: 'module' object has no attribute 'FRIDAY'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Error while using calendar module in Python 2.7

2011-03-04 Thread David Hutto
Could you paste the whle code, because I get:

>>> import datetime
>>> import calendar
>>>
>>>
>>>
>>> while monday.weekday() != calendar.MONDAY:
...
  File "", line 2

^
IndentationError: expected an indented block
>>> monday -= oneday
  File "", line 1
monday -= oneday
^
IndentationError: unexpected indent
>>>
>>> oneweek = datetime.timedelta(days=7)
>>>
>>> nextweek = today + oneweek
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'today' is not defined
>>>
>>> print next week
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Error while using calendar module in Python 2.7

2011-03-04 Thread David Hutto
>>>
>>> while monday.weekday() != calendar.MONDAY:
... monday -= oneday
... oneweek = datetime.timedelta(days=7)
... nextweek = today + oneweek
... print "next week"
...
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'monday' is not defined
>>>

This means that calender doesn't have an Monday as something you can
access. Look at how you use datetime.timedelta(days=7). If you type
help (datetime), you can see that timedelta is a function you can
access from datetime. However your utilization of calendar doesn't
align with the other. Look at the difference in each using the help
function(datetime.timedelta and calendar.MONDAY).

Also the error you show is a little confusing, because it looks like
you were trying calendar.FRIDAY instead of calendar.MONDAY at that
point.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Error while using calendar module in Python 2.7

2011-03-04 Thread ranjan das
Sorry David

The correctly indented code with the while loop is


while friday.weekday() != calendar.MONDAY:
   MONDAY -= oneday

oneweek = datetime.timedelta(days=7)

nextweek = today + oneweek

nextyear = today.replace(year=today.year+1)

print "Today (year-month-day) =", today

print "Most recent Monday =", monday




Is there any change in the calendar module from Python 2.6 to 2.7.

This example works fine in Python 2.6 but throws up an error in 2.7


On Sat, Mar 5, 2011 at 12:58 PM, David Hutto  wrote:

> Could you paste the whle code, because I get:
>
> >>> import datetime
> >>> import calendar
> >>>
> >>>
> >>>
> >>> while monday.weekday() != calendar.MONDAY:
> ...
>  File "", line 2
>
>^
> IndentationError: expected an indented block
> >>> monday -= oneday
>  File "", line 1
>monday -= oneday
>^
> IndentationError: unexpected indent
> >>>
> >>> oneweek = datetime.timedelta(days=7)
> >>>
> >>> nextweek = today + oneweek
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'today' is not defined
> >>>
> >>> print next week
>



-- 
The inherent vice of capitalism is the unequal sharing of blessings; the
inherent virtue of socialism is the equal sharing of miseries.

~ Winston Churchill
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Error while using calendar module in Python 2.7

2011-03-04 Thread ranjan das
Please consider this corrected  example


The correctly indented code with the while loop is


while monday.weekday() != calendar.MONDAY:
   MONDAY -= oneday


oneweek = datetime.timedelta(days=7)

nextweek = today + oneweek

nextyear = today.replace(year=today.year+1)

print "Today (year-month-day) =", today
print "Most recent Monday =", monday



On Sat, Mar 5, 2011 at 1:20 PM, ranjan das  wrote:

> Sorry David
>
>
>
>
>
>
> Is there any change in the calendar module from Python 2.6 to 2.7.
>
> This example works fine in Python 2.6 but throws up an error in 2.7
>
>
>
> On Sat, Mar 5, 2011 at 12:58 PM, David Hutto  wrote:
>
>> Could you paste the whle code, because I get:
>>
>> >>> import datetime
>> >>> import calendar
>> >>>
>> >>>
>> >>>
>> >>> while monday.weekday() != calendar.MONDAY:
>> ...
>>  File "", line 2
>>
>>^
>> IndentationError: expected an indented block
>> >>> monday -= oneday
>>  File "", line 1
>>monday -= oneday
>>^
>> IndentationError: unexpected indent
>> >>>
>> >>> oneweek = datetime.timedelta(days=7)
>> >>>
>> >>> nextweek = today + oneweek
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> NameError: name 'today' is not defined
>> >>>
>> >>> print next week
>>
>
>
>
> --
> The inherent vice of capitalism is the unequal sharing of blessings; the
> inherent virtue of socialism is the equal sharing of miseries.
>
> ~ Winston Churchill
>



-- 
The inherent vice of capitalism is the unequal sharing of blessings; the
inherent virtue of socialism is the equal sharing of miseries.

~ Winston Churchill
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor