Re: [Tutor] Phyton script for fasta file (seek help)

2013-04-08 Thread kartik sundarajan
My guess is you are using Python version >2.6 where "set" is a built-in.
You can fix this by doing the following.

1) Remove "from sets import Set"
2) And replace "alphabet = list(Set(stList))" with "alphabet =
list(set(stList))"

This should work straight away.

P.S Its "Python" not phyton

Cheers
Kartik



On Sun, Mar 24, 2013 at 12:20 PM, michelle_low wrote:

> **
>
> Hi everyone,
>
>
> Can someone please help me with the following phyton script? I received
> the error message  DeprecationWarning: the sets module is deprecated
>   from sets import Set.
>
> After googling, I have tried the methods others suggest:  change sets to
> set or delete the from sets import Set but none of them works.
>
> Can someone suggest me how to modify the following codes so that the input
> file is read from standard input?
> I'd like to execute them with unix command
>
> script.py <  sequence.fna
>
>
> Thanks a bunch.
>
>
>
> #!/usr/local/bin/python
>
> import math
> from sets import Set
>
>
> line = file("sequence.fna", "r")
>
> for x in line:
>   if x [0] == ">" :
>
> #determine the length of sequences
> s=line.next()
> s=s.rstrip()
> length = len(s)
>
> # determine the GC content
> G = s.count('G')
> C = s.count('C')
> GC= 100 * (float(G + C) / length)
>
>
> stList = list(s)
> alphabet = list(Set(stList))
>
> freqList = []
> for symbol in alphabet:
>   ctr = 0
>   for sym in stList:
> if sym == symbol:
> ctr += 1
> freqList.append(float(ctr)/len(stList))
>
> # Shannon entropy
>   ent = 0.0
>   for freq in freqList:
> ent = ent + freq * math.log(freq, 2)
>   ent = -ent
>
>   print x
>   print "Length:" , length
>   print "G+C:" ,round(GC),"%"
>   print 'Shannon entropy:'
>   print ent
>   print 'Minimum number of bits required to encode each symbol:'
>   print int(math.ceil(ent))
>
> ___
> 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


Re: [Tutor] FYI: An Introduction to Interactive Programming in Python

2013-04-08 Thread Len Conrad


wrong teaching language!  :)

Computer scientists develop video game that teaches how to program in Java

http://phys.org/news/2013-04-scientists-video-game-java.html


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


Re: [Tutor] FYI: An Introduction to Interactive Programming in Python

2013-04-08 Thread Dave Angel

On 04/08/2013 07:58 PM, Steven D'Aprano wrote:

On 09/04/13 06:38, brian arb wrote:

An Introduction to Interactive Programming in PythonJoe Warren, Scott
Rixner, Stephen Wong and John Greiner

This course is designed to be a fun introduction to the basics of
programming in Python. Our main focus will be on building simple
interactive games such as Pong, Blackjack and Asteroids.

https://www.coursera.org/course/interactivepython


Thanks for the link Brian.

As an aside, I wonder, apart from Conway's Game of Life, which isn't
actually a game at all, what's a non-interactive game?




Perhaps a game where the interaction is not real-time, like Adventure 
and other early RPG's.


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


Re: [Tutor] FYI: An Introduction to Interactive Programming in Python

2013-04-08 Thread Don Jennings

On Apr 8, 2013, at 7:58 PM, Steven D'Aprano wrote:

> On 09/04/13 06:38, brian arb wrote:
>> An Introduction to Interactive Programming in PythonJoe Warren, Scott
>> Rixner, Stephen Wong and John Greiner
>> 
>> This course is designed to be a fun introduction to the basics of
>> programming in Python. Our main focus will be on building simple
>> interactive games such as Pong, Blackjack and Asteroids.
>> 
>> https://www.coursera.org/course/interactivepython
> 
> Thanks for the link Brian.
> 
> As an aside, I wonder, apart from Conway's Game of Life, which isn't
> actually a game at all, what's a non-interactive game?

Watching someone else play Pong? ;>)

Take care,
Don

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


Re: [Tutor] FYI: An Introduction to Interactive Programming in Python

2013-04-08 Thread Steven D'Aprano

On 09/04/13 06:38, brian arb wrote:

An Introduction to Interactive Programming in PythonJoe Warren, Scott
Rixner, Stephen Wong and John Greiner

This course is designed to be a fun introduction to the basics of
programming in Python. Our main focus will be on building simple
interactive games such as Pong, Blackjack and Asteroids.

https://www.coursera.org/course/interactivepython


Thanks for the link Brian.

As an aside, I wonder, apart from Conway's Game of Life, which isn't
actually a game at all, what's a non-interactive game?



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


[Tutor] FYI: An Introduction to Interactive Programming in Python

2013-04-08 Thread brian arb
An Introduction to Interactive Programming in PythonJoe Warren, Scott
Rixner, Stephen Wong and John Greiner

This course is designed to be a fun introduction to the basics of
programming in Python. Our main focus will be on building simple
interactive games such as Pong, Blackjack and Asteroids.

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


Re: [Tutor] Reading Program

2013-04-08 Thread bob gailer

On 4/8/2013 2:58 PM, Donald Dietrich wrote:

I am just new to python programing
welcome. what other programming experience do you have? What version of 
python? and what OS?
would like some help developing a program for "flow reading"  What I 
would like the program to do is use any  .txt file like micorsoft word 
.txt file and flow the words across the screen one at time until the 
file is come to the end. I would also like to control the rate (time) 
of flow.

Do you have any experience developing GUI applications?

Do you have any experience with reading files?

I would start simple, and add pieces one at a time. Pseudo code:

open the file
grab a line
  split it (by default this will give you a list of "words" - meaning 
text strings that are separated from one another by

  whitespace)
  write  a word to the console (terminal)
  sleep for a short time
  repeat for each word in the list
repeat for each line in the file.

Then take a look at the various GUI packages that have a Python version.

tkinter comes with the Python distribution.

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


[Tutor] Reading Program

2013-04-08 Thread Donald Dietrich
I am just new to python programing, but would like some help developing a 
program for “flow reading”  What I would like the program to do is use any  
.txt file like micorsoft word .txt file and flow the words across the screen 
one at time until the file is come to the end. I would also like to control the 
rate (time) of flow. Do you think you can help me. Sincerely, Donald 
Dietrich



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


Re: [Tutor] Socket Programming

2013-04-08 Thread Lolo Lolo
on socket programming. if as a client or server, the information being sent has 
a buffer size on 2048, will this retrieve all data below 2KB? or can it be 
exceeded with say 2100? or does 2100 fall under 3KB?___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2013-04-08 Thread Alan Gauld

On 08/04/13 08:06, Mousumi Basu wrote:


import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
 s=s.bind(('172.18.2.11',2213))
 print 'socket bind is complete'
except socket.error,msg:
 print 'bind failed'
 sys.exit()


Your first error is that when developing you don't want to hide the 
error messages so add a raise statement to the except block so you get 
the full traceback with all of its helpful details.




but when i am writting;-

s=s.bind(('localhost',2213))

the output is;-
socket bind is complete


Which suggests that the 172... IP address you are using above is not 
recognised... or maybe has a firewall around it, or some other 
restrictions on access.



Can you please tell me where i am making the error?
(The computers are connected as the ping operation is executing properly
in command prompt)


It is most likely a network configuration issue but only you can fix 
that since we can't see how the network is set up. Rather than using 
ping try using telnet to reach the IP address and port and see what 
response you get.


--
Alan G
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] How to extract a float from an instancemethod call

2013-04-08 Thread Flynn, Stephen (L & P - IT)
Aha - another EdX 6.00 Python course student.

> Traceback (most recent call last):
>File "/Users/Sydney/Documents/6.00x Files/Problem
> Sets/ProblemSet7/ps7 copy.py", line 303, in 
>  testRobotMovement(StandardRobot, RectangularRoom)
>File "ps7_verify_movement.py", line 12, in testRobotMovement
>File "/Users/Sydney/Documents/6.00x Files/Problem
> Sets/ProblemSet7/ps7 copy.py", line 285, in updatePositionAndClean
>  while self.room.isPositionInRoom(self.position) == False:
>File "/Users/Sydney/Documents/6.00x Files/Problem
> Sets/ProblemSet7/ps7 copy.py", line 163, in isPositionInRoom
>  return self.room[(x,y)] in self.room
> KeyError: ( 0x4699490>>,  0x4699490>>)
>  >>>
> 
> The program text referred to is the following, I give the whole
module,
> which is part of a larger program.
>   def isPositionInRoom(self, pos):
>  """
>  Return True if pos is inside the room.
> 
>  pos: a Position object.
>  returns: True if pos is in the room, False otherwise.
>  """
>  x = pos.getX
>  y = pos.getY
>  return self.room[(x,y)] in self.room


You extract X and Y from the Position object using it's accessor
methods. All you then need to do is check to ensure that both X and Y
are >= zero and that both of them, when converted to an into, are less
than room. width and room. height.




This email and any attachment to it are confidential.  Unless you are the 
intended recipient, you may not use, copy or disclose either the message or any 
information contained in the message. If you are not the intended recipient, 
you should delete this email and notify the sender immediately.

Any views or opinions expressed in this email are those of the sender only, 
unless otherwise stated.  All copyright in any Capita material in this email is 
reserved.

All emails, incoming and outgoing, may be recorded by Capita and monitored for 
legitimate business purposes. 

Capita exclude all liability for any loss or damage arising or resulting from 
the receipt, use or transmission of this email to the fullest extent permitted 
by law.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2013-04-08 Thread Kushal Kumaran
Mousumi Basu  writes:

> I want to perform binding between two computers having ip addresses
> 172.18.2.11 and  172.18.2.95.So i wrote the following code(on the computer
> having IP address 172.18.2.95):-
>
>
> import socket
> import sys
> s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
> try:
> s=s.bind(('172.18.2.11',2213))

The bind method does not do what you think it does.

To bind to an IP address, the address must be assigned to one of the
interfaces on the host where the code is running.  So your bind call
will not work on 172.18.2.95.  It will only work on 172.18.2.11.  To
make a connection to a different host, you call the connect method.

Since you are using SOCK_DGRAM, an explicit connect call is not
required.  You can just call the sendto method on the socket.  On the
other end, the socket will need the bind method to be called.  It can
then call recvfrom to receive data.

You should read an introductory sockets programming text if you intend
to use the python socket module.
This page: http://beej.us/guide/bgnet/output/html/multipage/index.html
which showed up in my web search, seems simple.  You will have to
translate C code to python, but the basic concepts will apply.

> print 'socket bind is complete'
> except socket.error,msg:
> print 'bind failed'
> sys.exit()
>
>
> The output is :-
> bind failed
>
>
> but when i am writting;-
>
> s=s.bind(('localhost',2213))
>
> the output is;-
> socket bind is complete
>
>
>
> Can you please tell me where i am making the error?
> (The computers are connected as the ping operation is executing properly in
> command prompt)

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


Re: [Tutor] How to extract a float from an instancemethod call

2013-04-08 Thread Dave Angel

On 04/08/2013 08:40 AM, Sydney Shall wrote:

Hi,
I am  learning Python.

I use MAC OSX 10.6.8
Python 2.7.3

I have been given a project to write a program involving random walks.
I have drafted a program which has passed all the static tests, but on
testing my program i get the following error message:


Traceback (most recent call last):
   File "/Users/Sydney/Documents/6.00x Files/Problem
Sets/ProblemSet7/ps7 copy.py", line 303, in 
 testRobotMovement(StandardRobot, RectangularRoom)
   File "ps7_verify_movement.py", line 12, in testRobotMovement
   File "/Users/Sydney/Documents/6.00x Files/Problem
Sets/ProblemSet7/ps7 copy.py", line 285, in updatePositionAndClean
 while self.room.isPositionInRoom(self.position) == False:
   File "/Users/Sydney/Documents/6.00x Files/Problem
Sets/ProblemSet7/ps7 copy.py", line 163, in isPositionInRoom
 return self.room[(x,y)] in self.room
KeyError: (>, >)
 >>>

The program text referred to is the following, I give the whole module,
which is part of a larger program.
  def isPositionInRoom(self, pos):
 """
 Return True if pos is inside the room.

 pos: a Position object.
 returns: True if pos is in the room, False otherwise.
 """
 x = pos.getX
 y = pos.getY


You never show the code for Position.getX and Position.getY, but I'd 
expect they're methods that take no arguments.  in that case, you need 
parens in order to call them.


   x = pos.getX()
   y = pos.getY()


 return self.room[(x,y)] in self.room



a simple print of x and y could have revealed this as well.


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


[Tutor] How to extract a float from an instancemethod call

2013-04-08 Thread Sydney Shall

Hi,
I am  learning Python.

I use MAC OSX 10.6.8
Python 2.7.3

I have been given a project to write a program involving random walks.
I have drafted a program which has passed all the static tests, but on 
testing my program i get the following error message:



Traceback (most recent call last):
  File "/Users/Sydney/Documents/6.00x Files/Problem 
Sets/ProblemSet7/ps7 copy.py", line 303, in 

testRobotMovement(StandardRobot, RectangularRoom)
  File "ps7_verify_movement.py", line 12, in testRobotMovement
  File "/Users/Sydney/Documents/6.00x Files/Problem 
Sets/ProblemSet7/ps7 copy.py", line 285, in updatePositionAndClean

while self.room.isPositionInRoom(self.position) == False:
  File "/Users/Sydney/Documents/6.00x Files/Problem 
Sets/ProblemSet7/ps7 copy.py", line 163, in isPositionInRoom

return self.room[(x,y)] in self.room
KeyError: (0x4699490>>, 0x4699490>>)

>>>

The program text referred to is the following, I give the whole module, 
which is part of a larger program.

 def isPositionInRoom(self, pos):
"""
Return True if pos is inside the room.

pos: a Position object.
returns: True if pos is in the room, False otherwise.
"""
x = pos.getX
y = pos.getY
return self.room[(x,y)] in self.room

The module that made the call is;
 def updatePositionAndClean(self):
"""
Simulate the raise passage of a single time-step.

Move the robot to a new position and mark the tile it is on as 
having

been cleaned.
"""
steps = 0
self.position = self.room.getRandomPosition()
print self.position

self.x = self.position.getX
self.y = self.position.getY
print self.room

self.room.cleanTileAtPosition((self.x, self.y))

self.direction = self.getRobotDirection()

print 'Initial direction in degrees is ' , self.direction

self.position = self.position.getNewPosition(self.direction, 
self.speed)

steps += 1
while self.room.isPositionInRoom(self.position) == False:
self.direction = Robot.getRobotDirection()
self.position = Position.getNewPosition(self.direction, 
self.speed)

steps += 1
self.x = self.position.getX
self.y = self.position.getY


print 'Current position is x,y ', self.x, self.y
print 'Current direction is ' , self.direction + 'Degrees.'

self.room.cleanTileAtPosition((self.x, self.y))

print self.room.isTileCleaned((self.x, self.y))

I do not understand how one extracts a number from an instance method 
that should return a number.

I need to convert this number a float to an int to avoid the key error.
But I do not know how to extract the float.

This conclusion I derive from this earlier error message;

Traceback (most recent call last):
  File "/Users/Sydney/Documents/6.00x Files/Problem 
Sets/ProblemSet7/ps7 copy.py", line 303, in 

testRobotMovement(StandardRobot, RectangularRoom)
  File "ps7_verify_movement.py", line 12, in testRobotMovement
  File "/Users/Sydney/Documents/6.00x Files/Problem 
Sets/ProblemSet7/ps7 copy.py", line 285, in updatePositionAndClean

while self.room.isPositionInRoom(self.position) == False:
  File "/Users/Sydney/Documents/6.00x Files/Problem 
Sets/ProblemSet7/ps7 copy.py", line 163, in isPositionInRoom

return self.room[(int(x), int(y))] in self.room
TypeError: int() argument must be a string or a number, not 'instancemethod'
>>>  RESTART 



I would be grateful for any guidance.






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


Re: [Tutor] if statement problems(noob question)

2013-04-08 Thread Dave Angel

On 04/08/2013 07:55 AM, Steven D'Aprano wrote:

On 08/04/13 20:45, Dave Angel wrote:


will always fail, since the integer 2 is not equal to the type int.
In Python 2.x, when you compare objects of different types, you
generally get unequal.  (Exception, if you define your own classes,
you can define how they compare to others)  In Python 3, you'd get an
exception.



Not so. Equality testing is always allowed.



Right.  Thanks for the correction.  I should know better by now.

I knew I should have left that part off, since the OP is using version 2 
anyway.


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


Re: [Tutor] Socket Programming

2013-04-08 Thread Steven D'Aprano

On 08/04/13 17:06, Mousumi Basu wrote:

I want to perform binding between two computers having ip addresses
172.18.2.11 and  172.18.2.95.So i wrote the following code(on the computer
having IP address 172.18.2.95):-


import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
 s=s.bind(('172.18.2.11',2213))
 print 'socket bind is complete'
except socket.error,msg:
 print 'bind failed'
 sys.exit()




Why are you throwing away the useful information Python gives you to debug the
problem? That's like trying to find your lost keys by deliberately putting on a
blindfold.

Never, never, never catch an exception only to print a useless message that
tells you nothing. "Bind failed"? Why did it fail? Who knows! You threw that
information away. You might as well have said "An error occurred".

Instead, use this code:


import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s = s.bind(('172.18.2.11', 2213))
print 'socket bind is complete'


If an error occurs, Python will automatically print a full traceback showing
you exactly:

* what went wrong
* where it went wrong
* what line of code caused the problem
* what type of problem it was

instead of just a useless "an error occurred" message.


Once you have a proper traceback, and you can see the actual error message,
then and only then can we begin to solve the problem.




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


Re: [Tutor] if statement problems(noob question)

2013-04-08 Thread Steven D'Aprano

On 08/04/13 20:21, Max Smith wrote:

Hi, everyone whom might read this, im Max and i am really new to coding,
been going at it for about two weeks using codeacademy and the book "think
python".
when i decided to experiment a little with if statements i ran into the
following problem:

def plus(x,y):
 if x and y == int or float:
 print "The answer is: " + str(x+y)
 else:
 print "not a number"



Your problem here is that you've let Python's friendly syntax fool you into
thinking you can program in English. Python's good, but it's not that good!


In English, I might say to you:

"If x and y are ints, or floats, then do this ..."

which you've translated into Python as:

if x and y == int or float: ...


But that's not what it means to Python. What Python does is quite different.
Translating the above:

if x is a truthy value,
   and y equals the function "int",
   or the function "float" is a truthy value,
then ...


What do I mean by "is a truthy value"?

Python has a rule that *every* value, numbers, strings, lists, etc, everything,
can be considered "true-like" or "false-like". The rules are:

- values which are considered "empty" or "nothing" in some sense are treated
  as false-like:

  zero numbers: 0, 0.0
  empty string: ''
  empty containers (lists, dicts, tuples)
  None
  False

  are all "false-like" or "falsey" values.


- values which are considered non-empty, or "something", are treated as
  true-like:

  every number except zero, e.g. 0.0001, 42, -17.4
  every non-empty string, e.g. "hello world"
  every non-empty container, e.g. [1, 2, 3]
  True
  functions
  modules
  etc.

  are all "true-like" or "truthy" values.


So let's go back to your code:

if x and y == int or float: ...


1) First, Python checks whether x is a truthy value. In the examples you give,
it is, so the if-clause starts to look like this:

if True and y == int or float: ...


2) Next, Python checks whether y equals the int function. In this case, it does
not, so the if-clause looks like this:

if True and False or float: ...

and Python can simplify "True and False" to just False:

if False or float: ...


3) Next, Python checks whether the float function is a truthy value. Since
functions are "something" rather than "nothing", it is considered truthy, and
the if-clause looks like this:

if False or True: ...

which Python simplifies to:

if True: ...


and so the if-clause *always* evaluates as True.



So what do you have to write instead?

Firstly, you can't check the type of a value with equals. You use the isinstance
function instead. To check if x is an int:

if isinstance(x, int): ...


To check if x is an int, or a float, make the second argument a tuple of types:


if isinstance(x, (int, float)): ...


To check is x *and* y are ints, or floats, you have to check each one 
separately:


number = (int, float)  # tuple of two types
if isinstance(x, number) and isinstance(y, number): ...




Try that, and see how your code works.





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


Re: [Tutor] if statement problems(noob question)

2013-04-08 Thread Steven D'Aprano

On 08/04/13 20:45, Dave Angel wrote:


will always fail, since the integer 2 is not equal to the type int.  In Python 
2.x, when you compare objects of different types, you generally get unequal.  
(Exception, if you define your own classes, you can define how they compare to 
others)  In Python 3, you'd get an exception.



Not so. Equality testing is always allowed.

In Python 3:


py> 25 == "hello world"
False


exactly the same as in Python 2. It's only ordering comparisons, < <= >= > that 
raise an error with incompatible types:

py> 25 <= "hello world"
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unorderable types: int() <= str()



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


Re: [Tutor] if statement problems(noob question)

2013-04-08 Thread Dave Angel

On 04/08/2013 06:37 AM, Woody 544 wrote:

Max,

You've made the arguments a string (so never a number) in:

print "The answer is: " + str(x+y)

MJ


That has nothing to do with the issue.  The str() function call is 
unnecessary, but harmless.  If the two values x and y are ints or 
floats, they will get added in the obvious way long before being 
converted to str().


If they are already strings, they'll be concatenated long before str() 
does a null conversion on the resultant string.


The problem, as I stated in my own response, is that the if statement 
does nothing useful for checking of types.



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


Re: [Tutor] if statement problems(noob question)

2013-04-08 Thread Dave Angel

On 04/08/2013 06:21 AM, Max Smith wrote:

Hi, everyone whom might read this, im Max and i am really new to coding,
been going at it for about two weeks using codeacademy and the book "think
python".
when i decided to experiment a little with if statements i ran into the
following problem:

def plus(x,y):
 if x and y == int or float:


What is it you expect this to do?  Have you tried a similar, simpler 
expression to get acquainted?

x = 2
if x == int:

will always fail, since the integer 2 is not equal to the type int.  In 
Python 2.x, when you compare objects of different types, you generally 
get unequal.  (Exception, if you define your own classes, you can define 
how they compare to others)  In Python 3, you'd get an exception.


The way you should test an object to see if it's an instance of a class 
is to use isinstance()

   http://docs.python.org/2/library/functions.html#isinstance

isinstance(x, int)
And if you need to test more than one type or class, use a tuple of the 
types:


isinstance(x, (int, float))

Now you can combine more than one such test:

if  isinstance(x, (int, float)) and isinstance(y, (int, float)):



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


Re: [Tutor] if statement problems(noob question)

2013-04-08 Thread Woody 544
Max,

You've made the arguments a string (so never a number) in:

print "The answer is: " + str(x+y)

MJ
On Apr 8, 2013 6:23 AM, "Max Smith"  wrote:

> Hi, everyone whom might read this, im Max and i am really new to coding,
> been going at it for about two weeks using codeacademy and the book "think
> python".
> when i decided to experiment a little with if statements i ran into the
> following problem:
>
> def plus(x,y):
> if x and y == int or float:
> print "The answer is: " + str(x+y)
>
> else:
> print "not a number"
>
>
> plus(2,2)
> plus("one","two")
>
> concatenates one and two insted of printing "not a number"
>
> i have tried to simplify by not cheacking for float values an i have also
> tried.
>
> def plus(x,y):
> if x and y == int or float:
> print "The answer is: " + str(x+y)
>
> elif x or y != int or float:
> print "not a number"
>
> Am i perhaps missing something with the logic gates?
>
> Any help is greatly appreciated !
>
> ___
> 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] if statement problems(noob question)

2013-04-08 Thread Max Smith
Hi, everyone whom might read this, im Max and i am really new to coding,
been going at it for about two weeks using codeacademy and the book "think
python".
when i decided to experiment a little with if statements i ran into the
following problem:

def plus(x,y):
if x and y == int or float:
print "The answer is: " + str(x+y)

else:
print "not a number"


plus(2,2)
plus("one","two")

concatenates one and two insted of printing "not a number"

i have tried to simplify by not cheacking for float values an i have also
tried.

def plus(x,y):
if x and y == int or float:
print "The answer is: " + str(x+y)

elif x or y != int or float:
print "not a number"

Am i perhaps missing something with the logic gates?

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


Re: [Tutor] Socket Programming

2013-04-08 Thread Timo

Op 08-04-13 09:06, Mousumi Basu schreef:
I want to perform binding between two computers having ip addresses  
172.18.2.11 and 172.18.2.95.So  i wrote the 
following code(on the computer having IP address 172.18.2.95):-



import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
s=s.bind(('172.18.2.11',2213))
print 'socket bind is complete'
except socket.error,msg:
print 'bind failed'
sys.exit()

You are hiding the error message here. Just print msg and see why it fails.

Timo




The output is :-
bind failed


but when i am writting;-

s=s.bind(('localhost',2213))

the output is;-
socket bind is complete



Can you please tell me where i am making the error?
(The computers are connected as the ping operation is executing 
properly in command prompt)








___
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] Socket Programming

2013-04-08 Thread Mousumi Basu
I want to perform binding between two computers having ip addresses
172.18.2.11 and  172.18.2.95.So i wrote the following code(on the computer
having IP address 172.18.2.95):-


import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
s=s.bind(('172.18.2.11',2213))
print 'socket bind is complete'
except socket.error,msg:
print 'bind failed'
sys.exit()


The output is :-
bind failed


but when i am writting;-

s=s.bind(('localhost',2213))

the output is;-
socket bind is complete



Can you please tell me where i am making the error?
(The computers are connected as the ping operation is executing properly in
command prompt)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urllib.urlretrieve ?

2013-04-08 Thread Alan Gauld

On 07/04/13 19:49, Matthew Ngaha wrote:

yeah, sadly urlretrieve doesnt exist on my version of python.


I just told you where to find it on Python 3.

It's just been moved that's all.


--
Alan G
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