Re: [Tutor] flow problem with a exercise

2010-08-21 Thread Roelof Wobben


 


From: waynejwer...@gmail.com
Date: Fri, 20 Aug 2010 15:07:56 -0500
Subject: Re: [Tutor] flow problem with a exercise
To: rwob...@hotmail.com
CC: tutor@python.org


On Fri, Aug 20, 2010 at 2:48 PM, Roelof Wobben rwob...@hotmail.com wrote:


Oke, 
 
I don''t understand it complety.
 
return not arg%2 
 
Why use not here ?
 
I think that arg%2 is True not makes it false. 



What happens when you replace arg with a value? % is modulo division, so it 
just returns the remainder.


2 % 2 = ?
2%2=0 
 
4 % 2 = ?
4%2=0
 
7 % 2 = ?
 
7%2=1 
 
11 % 2 = ?
 
11%2 =1 

What is the truth value of 0 and 1?


print 'True' if 0 else 'False'
 
False 
 
print 'True' if 1 else 'False'
 
True

So what is the outcome of the following?


result = 2 % 2
print result, not result
if not result:
   print 'Even'
if result:
   print 'odd'
 
0  True
Even 
 
So 1 is True and 0 is False according to Python.


 

 
Another question.
 
How can I round outcome of a calculation.
 
round ( ( t-32)/1.8) does not work because I get a message that there are two 
arguments.
 
Outcome = (t-32)/1.8
outcome2 = round (outcome) does not work because the argument must be a string 
or a number 


What is the type of t?


 In [39]: t = 3


In [40]: round((t-32)/1.8)
Out[40]: -16.0


In [41]: t = 3.0


In [42]: round((t-32)/1.8)
Out[42]: -16.0


Works fine for me.
 
Correct, 
But I see one wierd thing.
 
round ((42-32)/1.8) gives a output -16.0 but (42-32)/1.8) gives also -16.0 
I was expectting that round will give 16 as output because round (32.0) is the 
same as round (32.0, 0) so there will be 0 decimals.
And I see one decimal.
 
Roelof
 


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


Re: [Tutor] flow problem with a exercise

2010-08-21 Thread Evert Rol
  In [39]: t = 3
 
 In [40]: round((t-32)/1.8)
 Out[40]: -16.0
 
 In [41]: t = 3.0
 
 In [42]: round((t-32)/1.8)
 Out[42]: -16.0
 
 Works fine for me.
  
 Correct,
 But I see one wierd thing.
  
 round ((42-32)/1.8) gives a output -16.0 but (42-32)/1.8) gives also -16.0
 I was expectting that round will give 16 as output because round (32.0) is 
 the same as round (32.0, 0) so there will be 0 decimals.
 And I see one decimal.

The rounding doesn't influence how it's printed.
From help(round):


round(...)
round(number[, ndigits]) - floating point number

Round a number to a given precision in decimal digits (default 0 digits).
This always returns a floating point number.  Precision may be negative.


It *rounds* the number to ndigits, not prints. It always returns a floating 
point number though (not an integer).
Since floats are normally represented with at least one trailing digit after 
the decimal dot, you see a zero.
Eg,
 float(1)
1.0

If you want to get rid of the trailing .0, convert it to an integer:
 int(round(1.1))
1

Also consider:
 round(1.15, 0)
1.0
 round(1.15, 1)
1.2
 round(1.15, 2)
1.1499


(Note again how the last rounded 1.15 is represented. It's just a 
representation though, and showing the imprecision you intrinsicaly get when 
dealing with floating point numbers.)

If you really want different behaviour when printing (or using) floating point 
numbers, perhaps have a look at the decimal module: 
http://docs.python.org/library/decimal.html


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


Re: [Tutor] flow problem with a exercise

2010-08-21 Thread Dave Angel
You need to figure out how to get your email program to do quoting.  As 
it stands, there's no good way to tell what part of the following 
message was from you, and what part was from wayne, or maybe others.   
Probably all you need to do is to do a reply-all to the message, and 
it'll mark the existing text with a  symbol.  Then make sure 
everything you type is *not* quoted.


Roelof Wobben wrote:
 



From: waynejwer...@gmail.com
snip

result = 2 % 2
print result, not result
if not result:
   print 'Even'
if result:
   print 'odd'
 
0  True
Even 
 
So 1 is True and 0 is False according to Python.



  
No, True is a specific object that differs from all other objects.  
Likewise False.  However, other objects, including especially integers, 
lists, and strings, can be used in a boolean context, and there are 
specific rules that decide whether a given object will be used as true 
(lowercase) or false.  For integers values, all nonzero items are 
considered true, while zero is considered false.
 

 
Another question.
 
How can I round outcome of a calculation.
 
round ( ( t-32)/1.8) does not work because I get a message that there are two arguments.
 
Outcome = (t-32)/1.8
outcome2 = round (outcome) does not work because the argument must be a string or a number 



What is the type of t?


 In [39]: t = 3


In [40]: round((t-32)/1.8)
Out[40]: -16.0


In [41]: t = 3.0


In [42]: round((t-32)/1.8)
Out[42]: -16.0


Works fine for me.
 
Correct, 
But I see one wierd thing.
 
round ((42-32)/1.8) gives a output -16.0 but (42-32)/1.8) gives also -16.0 
I was expectting that round will give 16 as output because round (32.0) is the same as round (32.0, 0) so there will be 0 decimals.

And I see one decimal.
 
Roelof
 



HTH,
Wayne
You're confusing the operation of round() with the operation of 
converting to a string for printing.  Round changes a floating point 
(binary) value to have a certain quantization, which approximates what 
you might want to print.  It does not, however, affect the way print 
will subsequently deal with that floating point number.   It's no 
different than when you use a numeric literal with varying amounts of 
apparent precision.

x = 4.2
x = 4.20
x = 4.20

all produce precisely the same floating point object.

DaveA

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


Re: [Tutor] flow problem with a exercise

2010-08-21 Thread Roelof Wobben


 

 Subject: Re: [Tutor] flow problem with a exercise
 From: evert@gmail.com
 Date: Sat, 21 Aug 2010 12:39:05 +0200
 CC: tutor@python.org
 To: rwob...@hotmail.com
 
  In [39]: t = 3
  
  In [40]: round((t-32)/1.8)
  Out[40]: -16.0
  
  In [41]: t = 3.0
  
  In [42]: round((t-32)/1.8)
  Out[42]: -16.0
  
  Works fine for me.
  
  Correct,
  But I see one wierd thing.
  
  round ((42-32)/1.8) gives a output -16.0 but (42-32)/1.8) gives also -16.0
  I was expectting that round will give 16 as output because round (32.0) is 
  the same as round (32.0, 0) so there will be 0 decimals.
  And I see one decimal.
 
 The rounding doesn't influence how it's printed.
 From help(round):
 
 
 round(...)
 round(number[, ndigits]) - floating point number
 
 Round a number to a given precision in decimal digits (default 0 digits).
 This always returns a floating point number. Precision may be negative.
 
 
 It *rounds* the number to ndigits, not prints. It always returns a floating 
 point number though (not an integer).
 Since floats are normally represented with at least one trailing digit after 
 the decimal dot, you see a zero.
 Eg,
  float(1)
 1.0
 
 If you want to get rid of the trailing .0, convert it to an integer:
  int(round(1.1))
 1
 
 Also consider:
  round(1.15, 0)
 1.0
  round(1.15, 1)
 1.2
  round(1.15, 2)
 1.1499
 
 
 (Note again how the last rounded 1.15 is represented. It's just a 
 representation though, and showing the imprecision you intrinsicaly get when 
 dealing with floating point numbers.)
 
 If you really want different behaviour when printing (or using) floating 
 point numbers, perhaps have a look at the decimal module: 
 http://docs.python.org/library/decimal.html
 
 


Oke, 

 

Thank you. Learned another thing about Python.

 

Roelof

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


Re: [Tutor] flow problem with a exercise

2010-08-20 Thread Alan Gauld


Roelof Wobben rwob...@hotmail.com wrote 


Others have pointed out the lack of indentation.
I'll point out some shortcuts you can use...

def is_even(argument):
   remainder= argument%2
   if remainder == 0 :
   return True
   else :
   return False

You can abbreviate this to just

def is_even(arg):
   return not arg%2

def is_odd(argument):
 uitkomst=is_even(argument)
return uitkomst

and this to:

def is_odd(arg):
  return not is_even(arg)

HTH,


--
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] flow problem with a exercise

2010-08-20 Thread Roelof Wobben

Oke, 
 
I don''t understand it complety.
 
return not arg%2 
 
Why use not here ?
 
I think that arg%2 is True not makes it false. 
 
Another question.
 
How can I round outcome of a calculation.
 
round ( ( t-32)/1.8) does not work because I get a message that there are two 
arguments.
 
Outcome = (t-32)/1.8
outcome2 = round (outcome) does not work because the argument must be a string 
or a number 
 
Roelof
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] flow problem with a exercise

2010-08-20 Thread Wayne Werner
On Fri, Aug 20, 2010 at 2:48 PM, Roelof Wobben rwob...@hotmail.com wrote:

  Oke,

 I don''t understand it complety.

 return not arg%2

 Why use not here ?

 I think that arg%2 is True not makes it false.


What happens when you replace arg with a value? % is modulo division, so it
just returns the remainder.

2 % 2 = ?
4 % 2 = ?
7 % 2 = ?
11 % 2 = ?

What is the truth value of 0 and 1?

print 'True' if 0 else 'False'
print 'True' if 1 else 'False'

So what is the outcome of the following?

result = 2 % 2
print result, not result
if not result:
   print 'Even'
if result:
   print 'odd'




 Another question.

 How can I round outcome of a calculation.

 round ( ( t-32)/1.8) does not work because I get a message that there are
 two arguments.

 Outcome = (t-32)/1.8
 outcome2 = round (outcome) does not work because the argument must be a
 string or a number


What is the type of t?

 In [39]: t = 3

In [40]: round((t-32)/1.8)
Out[40]: -16.0

In [41]: t = 3.0

In [42]: round((t-32)/1.8)
Out[42]: -16.0

Works fine for me.

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


Re: [Tutor] flow problem with a exercise

2010-08-20 Thread ALAN GAULD
Please use ReplyAll when responding to posts from the tutor list.

  I don''t understand it complety.

 return not arg%2 
  
 Why use not here ?
 
 I think that arg%2 is True not makes it false. 

For an even number arg % 2 will be 0 which Python considers to be False.
So for a True result when arg%2 is zero, you must negate the logic

  

Another question.
 
How can I round outcome of a calculation.
 
round ( ( t-32)/1.8) does not work because I get a message that there are two 
arguments.

Check your code because thre is only one arg here. Therefore the actual code 
giving the error must be different in some way.


Outcome = (t-32)/1.8
outcome2 = round (outcome) does not work because the argument must be a string 
or a number 



Assuming t is a number the outcome will be a number so it should work.

It helps if you cut n paste the real code and the real error message, 
do not just summarise. Python error messages are extremely helpful, 
but only if we can see them.

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


Re: [Tutor] flow problem with a exercise

2010-08-19 Thread Joel Goldstick
On Thu, Aug 19, 2010 at 3:01 PM, Roelof Wobben rwob...@hotmail.com wrote:

  Hello,

 I have this exercise:


 Now write the function is_odd(n) that returns True when n is odd and 
 Falseotherwise. Include doctests for this function as you write it.
 Finally, modify it so that it uses a call to is_even to determine if its
 argument is an odd integer.

 So I thought of this :

 def is_even(argument):
 remainder= argument%2
 if remainder == 0 :
 return True
 else :
 return False

 def is_odd(argument):
   uitkomst=is_even(argument)
 return uitkomst


NOTE that your return statement is not indented properly.  It must line up
under uitkomst


 even=is_odd(1) ;
 if even==True :
   print Even getal
 if even==False:
 print Oneven getal


 But now I get this error message :

 return uitkomst
 Syntax error : return outside function.


 In my opinon even calls is_odd , then uitkomst calls is_even which gives a
 true or false to uitkomst. So return uitkomst gives the outcome to even.
 But the intepreter thinks otherwise.

 I work on a Win7 machine with Python 2.7

 Roelof


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




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


Re: [Tutor] flow problem with a exercise

2010-08-19 Thread Wayne Werner
On Thu, Aug 19, 2010 at 2:01 PM, Roelof Wobben rwob...@hotmail.com wrote:

 snip

def is_odd(argument):
   uitkomst=is_even(argument)
 return uitkomst

 even=is_odd(1) ;
 if even==True :
   print Even getal
 if even==False:
 print Oneven getal


 But now I get this error message :

 return uitkomst
 Syntax error : return outside function.


Check your indention. In the email your return is at a different level than
your function. Always use 4 spaces. If your editor can't convert, you need a
better editor!


  In my opinon even calls is_odd , then uitkomst calls is_even which gives a
 true or false to uitkomst. So return uitkomst gives the outcome to even.
 But the intepreter thinks otherwise.


Well, good thing opinions don't count for much when talking to a computer ;)

You are correct that uitkomst contains the result of is_even, but your
return is on a different indention level. Whitespace is significant here.

If I were you, I would define is_odd this way:

def is_odd(argument):
uitkomst = not is_even(argument)
return uitkomst


  I work on a Win7 machine with Python 2.7


Good job providing both the traceback and your system. They make every
problem easier to debug.

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


Re: [Tutor] flow problem with a exercise

2010-08-19 Thread Alex Hall
On 8/19/10, Roelof Wobben rwob...@hotmail.com wrote:

 Hello,



 I have this exercise:



 Now write the function is_odd(n) that returns True when n is odd and False
 otherwise. Include doctests for this function as you write it.
 Finally, modify it so that it uses a call to is_even to determine if its
 argument is an odd integer.



 So I thought of this :



 def is_even(argument):
 remainder= argument%2
 if remainder == 0 :
 return True
 else :
 return False


 def is_odd(argument):
   uitkomst=is_even(argument)
 return uitkomst
The above line, the return statement, has to be indented; it did not
appear to be in your email. Also, maybe you want to return the
opposite of is_even in is_odd? Right now it looks like you are going
to get something like:
is_odd(3): is_even(3)=False, so is_odd(3) will echo that False. Maybe
return !uitkomst instead. I could have read it wrong, though.



 even=is_odd(1) ;
 if even==True :
   print Even getal
 if even==False:
 print Oneven getal



 But now I get this error message :



 return uitkomst

 Syntax error : return outside function.





 In my opinon even calls is_odd , then uitkomst calls is_even which gives a
 true or false to uitkomst. So return uitkomst gives the outcome to even.

 But the intepreter thinks otherwise.



 I work on a Win7 machine with Python 2.7



 Roelof


   


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] flow problem with a exercise

2010-08-19 Thread Dave Angel

Roelof Wobben wrote:
snip


def is_odd(argument):
  uitkomst=is_even(argument)
return uitkomst
   
snip
 



  
You forgot to indent the return statement to match the other 
statement(s) in the function.


DaveA

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