Re: [Tutor] Socket Programming

2013-04-06 Thread Alan Gauld

On 05/04/13 11:47, Mousumi Basu wrote:



s=socket.socket(socket.AF_INIT,socket.SCK_DGRM))


There is a double )) at the end of the line.
That should give a different error so I assume this is
not cut 'n paste code, but just in case I thought
I'd point it out...


s.bind(('172.18.2.11',8032))


Because I can't see anything wrong here.


But error is occurring showing "bind not done"


Can you send the full error text via cut 'n paste please?

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


[Tutor] loop questions

2013-04-06 Thread Soliman, Yasmin
I have two questions on these simple programs:

1st why does this loop keep repeating after I enter 'Quit'?

import calendar
m = raw_input(“Enter a year: “)
while m != “Quit”:
 if calendar.isleap(int(m)):
  print “%d is a leap year” % (int(m))
 else:
  print “%d is not a leap year” % (int(m)) 


2nd  How can I make this program not crash when a user enters a non integer?

m = raw_input(“Enter an integer: “)
while not m.isdigit():
 m = raw_input(“Enter an integer: “)
 num = int(m)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] loop questions

2013-04-06 Thread Mitya Sirenef

On 04/06/2013 06:00 PM, Soliman, Yasmin wrote:

I have two questions on these  simple programs:

>
> 1st why does this loop keep repeating after I enter 'Quit'?
>
> import calendar
> m = raw_input(“Enter a year: “)
> while m != “Quit”:
>  if calendar.isleap(int(m)):
>   print “%d is a leap year” % (int(m))
>  else:
>   print “%d is not a leap year” % (int(m))
>
>
> 2nd  How can I make this program not crash when a user enters a non 
integer?

>
> m = raw_input(“Enter an integer: “)
> while not m.isdigit():
>  m = raw_input(“Enter an integer: “)
>  num = int(m)
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


In pseudocode, you need to do something like this:

- loop forever
inp = user input
if inp is Quit: break
try to convert inp to int
if success:
print if inp is leap year or not
else:
print error


I have written a small function that handles a more general case of
asking user for a certain kind of input (e.g. number, text, yes/no, etc)
and keeps asking it until the answer in right format is given. Here it
is:

import re

def simpleinp(pattern, prompt="> ", convert=None, errmsg="Invalid 
Input", blank=False):

"""Keep asking user for input until it matches `pattern`."""
if pattern == "%d": pattern = "\d+"; convert = int
if pattern == "%s": pattern = ".+"

while True:
i = input(prompt).strip()
if blank and not i: return None

if re.match('^'+pattern+'$', i):
return convert(i) if convert and i else i
else:
print(errmsg)

# print( simpleinp("%d", "integer: ") )
# print( simpleinp("%s", "string: ") )
# print( simpleinp(".*", "string or blank: ") )
# print( simpleinp("[ynYN]", "y/n: ") )


HTH, -m


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

True friends stab you in the front.
Oscar Wilde

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


Re: [Tutor] loop questions

2013-04-06 Thread Mark Lawrence

On 06/04/2013 23:00, Soliman, Yasmin wrote:

I have two questions on these simple programs:

1st why does this loop keep repeating after I enter 'Quit'?

import calendar
m = raw_input(“Enter a year: “)
while m != “Quit”:
  if calendar.isleap(int(m)):
   print “%d is a leap year” % (int(m))
  else:
   print “%d is not a leap year” % (int(m))



Already answered by Mitya Sirenef.



2nd  How can I make this program not crash when a user enters a non integer?


The program doesn't crash, it raises an exception.



m = raw_input(“Enter an integer: “)
while not m.isdigit():
  m = raw_input(“Enter an integer: “)
  num = int(m)


try:
num = int(m)
except ValueError:
doSomething

I'll leave you to restructure the loop.

--
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.


Mark Lawrence

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


[Tutor] (no subject)

2013-04-06 Thread Soliman, Yasmin
How can I fix this loop so that it multiplies the two intergers and if user 
types in 'quit' for either number it stops? if not it keeps going.



def multiply_integers(int1,int2):
print int1*int2

int1=float(input('Please enter 1st integer: '))
int2=float(input('Please enter 2nd integer: '))

while True:
multiply_integers= int("int1, int2")
print int1*int2

if multiply_integers == 'Quit':
print '\nThank you for using this program! Bye.'
break
else:
print 'Not quitting'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] loop questions

2013-04-06 Thread Steven D'Aprano

On 07/04/13 08:00, Soliman, Yasmin wrote:

I have two questions on these simple programs:

1st why does this loop keep repeating after I enter 'Quit'?


The code you give below is not valid Python code. Please copy and paste *actual* the code 
you use, do not retype it from memory. I know that it is not valid code because it uses 
fancy quotes “ ” which do not work in Python, instead of ordinary quotes (actually inch 
marks) " "



import calendar
m = raw_input(“Enter a year: “)
while m != “Quit”:
  if calendar.isleap(int(m)):
   print “%d is a leap year” % (int(m))
  else:
   print “%d is not a leap year” % (int(m))



Fixing the above error, it is *not correct* that the loop repeats after entering "Quit". If you enter Quit, 
the loop will not run at all, not even once. If you enter something else, *anything* else include "quit" or 
"QUIT" or "Quit " (notice the space at the end), then it will loop forever.

The problem is that your code only asks for a year *once*. The raw_input is 
outside of the loop, so it only happens once. To get the opportunity to enter a 
year, or quit, more than once, it needs to be inside the loop.

Also, you should be more forgiving of user input. Instead of checking whether the user enters 
exactly CAPITAL Q lower u i t you should accept anything that looks like "quit". Use 
m.strip() to remove any accidental spaces at the beginning or end of the word, and m.lower() to 
convert to lowercase, then compare against "quit".

(remember that strings are *immutable*, and re-assign the result of the method 
calls to m)





2nd  How can I make this program not crash when a user enters a non integer?

m = raw_input(“Enter an integer: “)
while not m.isdigit():
  m = raw_input(“Enter an integer: “)
  num = int(m)



There are two ways. You can either catch the exception that happens, or avoid 
calling int. Here's the first way:


# This will work with negative numbers too.
response = raw_input("Enter an integer: ")
try:
num = int(response)
except ValueError:
print "%r is not an integer, please try again" % response


Here's the second way:


# This will not work with negative numbers.
response = raw_input("Enter an integer: ")
response = response.strip()  # get rid of any leading or trailing spaces
if response.isdigit():
num = int(response)
else:
print "%r is not an integer, please try again" % response



If you want these to repeat multiple times, you need to put them inside a loop.




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


Re: [Tutor] (no subject)

2013-04-06 Thread Steven D'Aprano

On 07/04/13 09:10, Soliman, Yasmin wrote:

How can I fix this loop so that it multiplies the two intergers and if user 
types in 'quit' for either number it stops? if not it keeps going.



How would you solve this problem in real life? Right down the steps you would 
do, as if you were explaining it to a child, or an idiot. Remember that 
computers are dumber than any child, so make the steps as simple as you can.

* Ask the user for a value
* if the first value is "Quit", then stop
* Ask the user for a second value
* If the second value is "Quit", then stop
* Convert the first value to an int (instead of a string)
* Convert the second value to an int (instead of a string)
* Multiply the two ints

Now put that into Python code. Get it working. If you have *specific* 
questions, please ask, but this question is so broad and general that you're 
basically asking us to do everything.

Once it works, then put it inside a loop.

The secret to programming is to break tasks up into smaller tasks, smaller and 
simpler, until they are so small and simple that even a computer can do them. 
Then put the pieces together, in the right order.


P.S. Please use a sensible subject line.


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


Re: [Tutor] (no subject)

2013-04-06 Thread Mark Lawrence

On 07/04/2013 00:10, Soliman, Yasmin wrote:

To quote John McEnroe "You cannot be serious".  Thankfully Steven 
D'Aprano has already given you a roadmap to follow.  I'll point out a 
few things that should assist.



How can I fix this loop so that it multiplies the two intergers and if
user types in 'quit' for either number it stops? if not it keeps going.

def multiply_integers(int1,int2):
 print int1*int2


multiply_integers will return None, the Python default.



int1=float(input('Please enter 1st integer: '))
int2=float(input('Please enter 2nd integer: '))


Are you trying to get integers from the input by calling float?  Or 
could that be interest?  Or what?  The print statement later on tells me 
you're using Python 2.x.  Therefore you should be using raw_input above 
and *NOT* input, the latter is highly dangerous.





while True:
 multiply_integers= int("int1, int2")


Having defined a function you now try to reassign its name, obviously 
not what you want.  But that won't work as the call to int will fail as 
you're passing a single string "int1, int2" to it.



 print int1*int2


Why print this here when your function multiply_integers already does 
exactly that?




 if multiply_integers == 'Quit':


You now try to compare your function to a string.  Or should 
multiply_integers be an int here?



 print '\nThank you for using this program! Bye.'
 break
 else:
 print 'Not quitting'



--
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.


Mark Lawrence

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


[Tutor] While loop

2013-04-06 Thread Najam Us Saqib
Hi,

Would you please help me by explaining that why " 5 " is skipped and not 
printed in the following program?

Thank you.
Najam.

count = 0
while True:
    count += 1
    # end loop if count is greater than 10
    if count > 10:
        break # means "break out of the loop"
    # skip 5
    if count == 5:
        continue # means "Jump back to the top of the looop"
    print count

raw_input("\n\nPress the enter key to exit.")

Output:

1
2
3
4
6
7
8
9
10


Press the enter key to exit.___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While loop

2013-04-06 Thread Dave Angel

On 04/06/2013 11:23 PM, Najam Us Saqib wrote:

Hi,

Would you please help me by explaining that why " 5 " is skipped and not 
printed in the following program?



Seems to me the comments say it pretty well.  The continue statement 
causes execution to continue at the while statement, which has the 
effect of skipping the print.  Like Monopoly:  go directly to jail, do 
not pass go.




Thank you.
Najam.

count = 0
while True:
 count += 1
 # end loop if count is greater than 10
 if count > 10:
 break # means "break out of the loop"
 # skip 5
 if count == 5:
 continue # means "Jump back to the top of the looop"
 print count

raw_input("\n\nPress the enter key to exit.")

Output:

1
2
3
4
6
7
8
9
10


Press the enter key to exit.


Incidentally, this looks like a transliteration of something written for 
some other language.  An experienced Python programmer would never write 
it this way.


I'd use something like (untested):

for count in xrange(11):
if count != 5:
print count

On the other hand, a course --teaching a C programmer to use Python-- 
might well use this as an early example, showing you later how much 
elegantly it can be done.



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


Re: [Tutor] While loop

2013-04-06 Thread Dave Angel

On 04/06/2013 11:23 PM, Najam Us Saqib wrote:

Hi,

Would you please help me by explaining that why " 5 " is skipped and not 
printed in the following program?



Seems to me the comments say it pretty well.  The continue statement 
causes execution to continue at the while statement, which has the 
effect of skipping the print.  Like Monopoly:  go directly to jail, do 
not pass go.




Thank you.
Najam.

count = 0
while True:
 count += 1
 # end loop if count is greater than 10
 if count > 10:
 break # means "break out of the loop"
 # skip 5
 if count == 5:
 continue # means "Jump back to the top of the looop"
 print count

raw_input("\n\nPress the enter key to exit.")

Output:

1
2
3
4
6
7
8
9
10


Press the enter key to exit.


Incidentally, this looks like a transliteration of something written for 
some other language.  An experienced Python programmer would never write 
it this way.


I'd use something like (untested):

for count in xrange(11):
if count != 5:
print count

On the other hand, a course --teaching a C programmer to use Python-- 
might well use this as an early example, showing you later how much 
elegantly it can be done.



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


Re: [Tutor] While loop

2013-04-06 Thread Sayan Chatterjee
# skip 5
if count == 5:
continue # means "Jump back to the top of the looop"

It is the reason. You yourself have mentioned it right inside the code!


On 7 April 2013 09:40, Dave Angel  wrote:

> On 04/06/2013 11:23 PM, Najam Us Saqib wrote:
>
>> Hi,
>>
>> Would you please help me by explaining that why " 5 " is skipped and not
>> printed in the following program?
>>
>>
> Seems to me the comments say it pretty well.  The continue statement
> causes execution to continue at the while statement, which has the effect
> of skipping the print.  Like Monopoly:  go directly to jail, do not pass go.
>
>
>  Thank you.
>> Najam.
>>
>> count = 0
>> while True:
>>  count += 1
>>  # end loop if count is greater than 10
>>  if count > 10:
>>  break # means "break out of the loop"
>>  # skip 5
>>  if count == 5:
>>  continue # means "Jump back to the top of the looop"
>>  print count
>>
>> raw_input("\n\nPress the enter key to exit.")
>>
>> Output:
>>
>> 1
>> 2
>> 3
>> 4
>> 6
>> 7
>> 8
>> 9
>> 10
>>
>>
>> Press the enter key to exit.
>>
>
> Incidentally, this looks like a transliteration of something written for
> some other language.  An experienced Python programmer would never write it
> this way.
>
> I'd use something like (untested):
>
> for count in xrange(11):
> if count != 5:
> print count
>
> On the other hand, a course --teaching a C programmer to use Python--
> might well use this as an early example, showing you later how much
> elegantly it can be done.
>
>
> --
> DaveA
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 


--
*Sayan  Chatterjee*
Dept. of Physics and Meteorology
IIT Kharagpur
Lal Bahadur Shastry Hall of Residence
Room AB 205
Mob: +91 9874513565
blog: www.blissprofound.blogspot.com

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