Re: [Tutor] Having Unusual results

2015-05-06 Thread Jag Sherrington
Excellent all working good, thank you.
Regards, Jag BraveArt Multimedia


 


 On Saturday, 2 May 2015, 22:28, Dave Angel  wrote:
   

 On 05/02/2015 04:36 AM, Peter Otten wrote:
> Jag Sherrington wrote:
> With that the calculation becomes
>
 buns = 20
 package_size = 8
 whole_packages, missing_buns = divmod(buns, package_size)
 total_packages = whole_packages
 if missing_buns: total_packages += 1
> ...
 total_packages
> 3
>

And that can be simplified:

buns = 20
package_size = 8
total_packages = (buns + package_size - 1) // package_size

#desired answer 3


Or, to take better advantage of the Python library:

import math
total_packages = math.ceil(buns/package_size)

This is exactly what the ceiling and floor mathematical concepts are 
needed for.

Note, I'm using the fact that the OP is writing in Python 3.  If not, 
one should probably add
    from __future__ import division
.

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


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


[Tutor] Fwd: Re: Adding consecutive numbers

2015-05-06 Thread Alan Gauld

Please use ReplyAll to include the list members.


 Forwarded Message 
Subject:Re: [Tutor] Adding consecutive numbers
Date:   Wed, 6 May 2015 21:13:15 +1000
From:   Whom Isac 
To: Alan Gauld 



Thanks for the reply. I am sorry that I did not notice the mail. I am 
actually using the latest version of python (3.5) in windows 7 operating 
system. I have already made certain changes in the code. I understood my 
mistake. The correction's are not finished yet,though. You can have a 
look at it, because, I donot know what I have written is already in 
right syntax or not.


Here are my code:
##Goal: Building a math program.
## two nums will be asked by the user
## they will be added
## condition: num >=o:
## num will continue to be added into a list untill the second number
## For your information, a consequitive sequence of num : num-->1 
num1--> num+1...+n


if __name__=='__main__':
interact()

def interact():
print('''Welcome to My new Math program!!
With this program, you can find the sum of any consequitive 
numbers.''')

print('So Just add your numbers in following spaces')
## If anybody complaining about this function. I will have to say, 
that the coding is incomplete so
## I will first define all my function then def interact() when I 
am finishing.



def getting_numbers(first_num, second_num):
x = [] #This is a empty list to store data
y = [] #This is a empty list to store data
"""Getting the user values:"""
first_num =int(input('Please enter your first number: '))
x.append(first_num) # adding the input in x#
second_num =int(input('Please enter your second number: '))
y.append(second_num) # adding the input in x#
z =(x,y) # This is a touple containing both x and y value.
return z

def adding_all(x):
total = 0
for num in x:
total +=num
return total
def remove_letter(x):
if x != len(x):
print('You did not enter a number')
elif x != adding_all(x):
print("Please, donot include letters")
else:
return x
## I think using a while True function to iterate all item in x 
would be better.




def adding_number(x,y):
start = x[0]
end = y[0]
new_x = 0
new_x_1 = 0
while x[0]<=y[0] or x[0]<= 0:
if x[0]==0:
new_x+=1
return new_x
elif x[0]>0 or x[0]print("You have not input a digit in order, check your 
digits\n")

print("I donot know what you mean?")

On Mon, Apr 27, 2015 at 10:16 PM, Alan Gauld > wrote:


   On 27/04/15 11:37, Whom Isac wrote:

   num1 entry =1 & num2 entry = 100 , the program should be adding
   number from
   1 to 100 together(which should equal to 5050).


   I have uploaded my code file below.


   Infortunately we can't see it.
   Since it is presumably quite short please just send it
   in the body of your email.

   Also include the full error text that you refer to,

   Also tell us the Python version and OS you are using.

   -- 
   Alan G

   Author of the Learn to Program web site
   http://www.alan-g.me.uk/
   http://www.amazon.com/author/alan_gauld
   Follow my photo-blog on Flickr at:
   http://www.flickr.com/photos/alangauldphotos


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




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


Re: [Tutor] Fwd: Re: Adding consecutive numbers

2015-05-06 Thread Mark Lawrence



 Forwarded Message 
Subject: Re: [Tutor] Adding consecutive numbers
Date: Wed, 6 May 2015 21:13:15 +1000
From: Whom Isac 
To: Alan Gauld 

I am actually using the latest version of python (3.5) in windows 7 operating
system.


Why are you using a version of Python that hasn't had a beta release 
yet?  Or have you simply mistyped the version number?


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: [Tutor] key detection

2015-05-06 Thread Dave Angel

On 05/06/2015 12:02 AM, Jim Mooney Py3.4.3winXP wrote:


actually worked in windows instead of using their awful screen
copy. What a surprise:


Many people don't realize that you can turn on a better screen copy 
feature for the CMD window (DOS box) in Windows.


I've given up Windows, and no longer remember how, but the feature is 
called something like auto-copy and can be turned on for all DOS box 
windows.


Once on, you select by dragging with the mouse, and insert by 
right-click.  Still has to be a rectangle, but better than nothing when 
redirection lets you down.



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


Re: [Tutor] Fwd: Re: Adding consecutive numbers

2015-05-06 Thread Dave Angel

On 05/06/2015 07:51 AM, Alan Gauld wrote:

Please use ReplyAll to include the list members.


 Forwarded Message 
Subject: Re: [Tutor] Adding consecutive numbers
Date: Wed, 6 May 2015 21:13:15 +1000
From: Whom Isac 
To: Alan Gauld 



Thanks for the reply. I am sorry that I did not notice the mail. I am
actually using the latest version of python (3.5) in windows 7 operating
system. I have already made certain changes in the code. I understood my
mistake. The correction's are not finished yet,though. You can have a
look at it, because, I donot know what I have written is already in
right syntax or not.

Here are my code:
##Goal: Building a math program.
## two nums will be asked by the user
## they will be added
## condition: num >=o:
## num will continue to be added into a list untill the second number
## For your information, a consequitive sequence of num : num-->1
num1--> num+1...+n

if __name__=='__main__':
 interact()


You get an error right there, since interact() isn't defined yet.  Move 
the above two lines to the end of the file.




def interact():
 print('''Welcome to My new Math program!!
 With this program, you can find the sum of any consequitive
numbers.''')
 print('So Just add your numbers in following spaces')
 ## If anybody complaining about this function. I will have to say,
that the coding is incomplete so
 ## I will first define all my function then def interact() when I
am finishing.


def getting_numbers(first_num, second_num):
 x = [] #This is a empty list to store data
 y = [] #This is a empty list to store data
 """Getting the user values:"""
 first_num =int(input('Please enter your first number: '))
 x.append(first_num) # adding the input in x#
 second_num =int(input('Please enter your second number: '))
 y.append(second_num) # adding the input in x#
 z =(x,y) # This is a touple containing both x and y value.
 return z


Why are you so enamored with lists?  You're returning a tuple containing 
two lists each of which has exactly one value?  Why not just return a 
tuple of first_num and second_num ?





def adding_all(x):
 total = 0
 for num in x:
 total +=num
 return total


Good function.


def remove_letter(x):
 if x != len(x):


What do you think that statement does?  It can't possibly do anything 
useful since the right side assumes that x is a collection or 
equivalent, and the left side assumes that x is a number.



 print('You did not enter a number')
 elif x != adding_all(x):
 print("Please, donot include letters")
 else:
 return x
 ## I think using a while True function to iterate all item in x
would be better.



Considering that after you call each input() function, you immediately 
call int(), I'd figure that checking for "you did not enter a number" is 
superfluous.






def adding_number(x,y):
 start = x[0]
 end = y[0]
 new_x = 0
 new_x_1 = 0
 while x[0]<=y[0] or x[0]<= 0:
 if x[0]==0:
 new_x+=1
 return new_x
 elif x[0]>0 or x[0]

I can't make any sense out of anything in this function.

I think you need to write one function and include descriptive comments 
in it, and write code that tests it against those comments.  Then when 
you have one function that successfully runs, write a second one.





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


Re: [Tutor] Fwd: Re: Adding consecutive numbers

2015-05-06 Thread Whom Isac
Thanks, Steven. I think you are right about those mistake. But you could
tell that the code was incomplete so the interact() was not defined. I have
updated some parts (basically writing from the scratch). I am busy with a
new project and learning how to create GUI app in python, although there
are not enough source to aid me. I will just post the code here for now:
Thanks, Steven. I think you are right about those mistake. But you could
tell that the code was incomplete so the interact() was not defined. I have
updated some parts (basically writing from the scratch). I am busy with a
new project and learning how to create GUI app in python, although there
are not enough source to aid me. I will just post the code here for now:


##Goal: Building a math program.
## two nums will be asked by the user
## they will be added
## condition: num >=o:
## num will continue to be added into a list untill the second number
## For your information, a consequitive sequence of num : num-->1 num1-->
num+1...+n

if __name__=='__main__':
interact()

def interact():
print('''Welcome to My new Math program!!
With this program, you can find the sum of any consequitive numbers.''')
print('So Just add your numbers in following spaces')
## If anybody complaining about this function. I will have to say, that
the coding is incomplete so
## I will first define all my function then def interact() when I am
finishing.


def getting_numbers(first_num, second_num):
x = [] #This is a empty list to store data
y = [] #This is a empty list to store data

"""Getting the user values:"""

first_num =int(input('Please enter your first number: '))
x.append(first_num) # adding the input in x#
second_num =int(input('Please enter your second number: '))
y.append(second_num) # adding the input in x#
z =(x,y) # This is a touple containing both x and y value.
return z

def adding_all(x):
total = 0
for num in x:
total +=num
return total

def remove_letter(x):
if x != len(x):
print('You did not enter a number')
elif x != adding_all(x):
print("Please, donot include letters")
else:
return x
## I think using a while True function to iterate all item in x
would be better.



def adding_number(x,y):
start = x[0]
end = y[0]
new_x = 0
new_x_1 = 0
while x[0]<=y[0] or x[0]<= 0:
if x[0]==0:
new_x+=1
return new_x
elif x[0]>0 or x[0]https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Re: Adding consecutive numbers

2015-05-06 Thread Alan Gauld

On 06/05/15 15:01, Dave Angel wrote:


def adding_all(x):
 total = 0
 for num in x:
 total +=num
 return total


Good function.


Except for the fact that the built-in sum() function
does the same thing with a lot less typing...


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] doctest: how to test a single function?

2015-05-06 Thread Slater Joseph C , PhD, PE
I'm an admitted newb trying to enter the Python community and use Python 
significantly (versus occassionally). dockets seems to be much more powerful 
than I can figure out how to tap. 

I have a function inside a file that's an embedded test that (currently) works 
fine. However, the package has a ton of these, and running all of the tests on 
all functions just to check the changed function is very time consuming. I 
can't make heads or tails out of what the manual means. I've read section 25.2 
and think I understand it, but clearly do not. 

What I do is:
>>> import doctest
>>> doctest.run_docstring_examples("functionname",globs[name="filename.py"])
^
SyntaxError: invalid syntax

I've also tried 
import filename
help(filename.functionname)
(works fine)
doctest.run_docstring_examples("filename.functionname")
Traceback (most recent call last):
  File "", line 1, in 
TypeError: run_docstring_examples() missing 1 required positional argument: 
'globs'

Now, if I do
>>> doctest.run_docstring_examples("sigp.frfestH1",globs=None)
nothing happens. 

Everything works fine from a command line:
bash:> python filename.py (I have the end of the file set to run doctest per 
easily found numerous examples)

except I have a ridiculous lag due to running tests that are unnecessary. 
Trying to send options doesn't work either

bash:> python -m 
'doctest.run__docstring_examples("filename.functionname",globs="")'  filename.py

/opt/local/bin/python: Error while finding spec for 
'doctest.run__docstring_examples("sigp.frfestH1",globs="")' (: No module named 'doctest.run__docstring_examples("sigp'; 
'doctest' is not a package)

or
bash:> python -m 'doctest.run__docstring_examples("functionname",globs=None)'  
filename.py
/opt/local/bin/python: Error while finding spec for 
'doctest.run__docstring_examples("sigp.frfestH1",globs="")' (: No module named 'doctest.run__docstring_examples("sigp'; 
'doctest' is not a package)

I appreciate your efforts to address my cluelessness. 
Thank You,
Joe



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


Re: [Tutor] key detection

2015-05-06 Thread Jim Mooney Py3.4.3winXP
On 5 May 2015 at 21:51, Mark Lawrence  wrote:

> On 06/05/2015 05:30, Jim Mooney Py3.4.3winXP wrote:
>
>> On 5 May 2015 at 18:32, Steven D'Aprano  wrote:
>>
>>  https://code.activestate.com/recipes/577977-get-single-keypress/
>>>
>>
>>
>> That only has a stub for Linux,  but I found one that does both. Although,
>> alas, no IOS version:
>>
>>
>> http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/
>>
>> Anyway, I set up msvcrt for now until I install linux - I'm not interested
>> in the program I mentioned, per se, just the error message - mainly to
>> know, generally, what sort of thing it's choking on. Errors are useful to
>> know.
>>
>>
> I went a further step from the recipes linked to above and got here
> https://pypi.python.org/pypi/readchar


I think that's the one that failed for me but I found out why. I just wrote
the simple snippet below to try msvcrt out. I can add a Linux try block
when I install Linux and actually find a wifi driver for it ;')

I reproduced the error that puzzled me  almost immediately. It was from
hitting a function key. The snippet below worked fine for letters and such,
but died when I hit a function key (although not all of them). I was
decoding since msvcrt sends byte strings, but there was nothing in the
utf-8 map for that key.  The 2> redirect is sure handy for dos console
error messages - something I'll have to remember ;')

from msvcrt import *

while True:
if kbhit():
key = getch()
if key == b'\xe0' or key == b'\000':
print('special key follows')
key = getch()
print(str(key, encoding='utf-8')) #got rid of this decode after
a function key error
else:
print('The key is: ', str(key, encoding='utf-8'))

Traceback (most recent call last):
  File "keyget.py", line 9, in 
print(str(key, encoding='utf-8'))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 0:
invalid start byte



-- 
Jim

"What a rotten, failed experiment. I'll start over. Maybe dogs instead of
monkeys this time." --God
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] key detection

2015-05-06 Thread Jim Mooney Py3.4.3winXP
On 6 May 2015 at 10:41, Jim Mooney Py3.4.3winXP 
wrote:

> I went a further step from the recipes linked to above and got here
>> https://pypi.python.org/pypi/readchar
>
>
> I think that's the one that failed for me
>

Addendum. That only failed in python 3.4. It worked fine in python 2.7 -
but I rarely use that.
-- 
Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] key detection

2015-05-06 Thread Dave Angel

On 05/06/2015 01:41 PM, Jim Mooney Py3.4.3winXP wrote:


from msvcrt import *

while True:
 if kbhit():
 key = getch()
 if key == b'\xe0' or key == b'\000':
 print('special key follows')
 key = getch()
 print(str(key, encoding='utf-8')) #got rid of this decode after
a function key error
 else:
 print('The key is: ', str(key, encoding='utf-8'))

Traceback (most recent call last):
   File "keyget.py", line 9, in 
 print(str(key, encoding='utf-8'))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 0:
invalid start byte





I don't know why you would be expecting to get a utf-8 character for the 
second byte of a function key code.  It's an entirely arbitrary byte 
sequence, and not equivalent to anything in Unicode, encoded or not.



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


Re: [Tutor] key detection

2015-05-06 Thread Jim Mooney Py3.4.3winXP
On 6 May 2015 at 14:08, Dave Angel  wrote:

> I don't know why you would be expecting to get a utf-8 character for the
> second byte of a function key code.  It's an entirely arbitrary byte
> sequence, and not equivalent to anything in Unicode, encoded or not


I just didn't think of accounting for function keys until I hit one -
experimental learning. The program I'm working on doesn't require F keys,
but I tried one just to see what would happen ;')  It's worth making the
error to reinforce unicode in my head. One item - once I dropped decoding
for special keys, some were printed as hex codes but some as letters. i.e.
F11 was b'\x85', but F9 and F10 were b'C' and b'D', so I assume the second
byte of some function keys just happens to map to utf-8 letters. Sure
enough, when I put in decoding again, F9 and F10 second bytes printed as C
and D, but the program bailed on F11.



-- 
Jim

"What a rotten, failed experiment. I'll start over. Maybe dogs instead of
monkeys this time." --God
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] key detection

2015-05-06 Thread Steven D'Aprano
On Tue, May 05, 2015 at 09:30:12PM -0700, Jim Mooney Py3.4.3winXP wrote:
> On 5 May 2015 at 18:32, Steven D'Aprano  wrote:
> 
> > https://code.activestate.com/recipes/577977-get-single-keypress/
> 
> 
> That only has a stub for Linux, 

Er, look again, more closely.

I happen to know the author very well *wink* and know that he run that 
specific code under Linux and it works fine. He's never tested it under 
Windows, so if it erases your hard drive don't blame him. Blame the 
authors of msvcrt.getch.

The general structure of the code goes:

try:
import tty, termios
except ImportError:
# handle Windows and other platforms without 
# the tty and termios modules
else:
# define Unix/Linux version of getch


> but I found one that does both. Although,
> alas, no IOS version:

If iOS supports tty and termios, it will just work.


> http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/

Ewww. Talk about unnecessary use of classes.

http://www.youtube.com/watch?v=o9pEzgHorH0


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


Re: [Tutor] key detection

2015-05-06 Thread Steven D'Aprano
On Wed, May 06, 2015 at 03:24:20PM -0700, Jim Mooney Py3.4.3winXP wrote:
> On 6 May 2015 at 14:08, Dave Angel  wrote:
> 
> > I don't know why you would be expecting to get a utf-8 character for the
> > second byte of a function key code.  It's an entirely arbitrary byte
> > sequence, and not equivalent to anything in Unicode, encoded or not
> 
> 
> I just didn't think of accounting for function keys until I hit one -
> experimental learning. The program I'm working on doesn't require F keys,
> but I tried one just to see what would happen ;')  It's worth making the
> error to reinforce unicode in my head. 

I'm not entirely sure how dealing with something that has nothing to do 
with Unicode will reinforce Unicode in your head. That sounds a bit like 
saying "I love to cook, I find chopping vegetables really helps my 
Python programming..."  :-)


> One item - once I dropped decoding
> for special keys, some were printed as hex codes but some as letters. i.e.
> F11 was b'\x85', but F9 and F10 were b'C' and b'D', so I assume the second
> byte of some function keys just happens to map to utf-8 letters. 

It would be more correct to say they happen to map to ASCII letters.

[Aside: I still don't know whether I like or dislike the (mis)feature 
where bytes are displayed by default as if they were ASCII strings.]

It's not quite fair to say that it is an "accident" that it maps to a 
UTF-8 character. After all, UTF-8 was carefully designed to be ASCII 
compatible, and maybe there was some specific reason why the second byte 
of F9 is 0x43 (ASCII 'C'). But perhaps we can say that it is an accident 
of history that it partially matches UTF-8. It certainly isn't intended 
to match UTF-8.

Think of it this way: if you open a JPEG file in binary mode, to get a 
bunch of bytes, and manage by trial and error to find a sequence 
somewhere inside the file that decodes as UTF-8 without error, that 
doesn't mean that the inventor of the JPEG image format had UTF-8 in 
mind when he designed it.



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