for and while loops

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Wed Jun 28 23:40:27 EDT 2006


Bayazee a écrit :
> hi
> 
> #Exercise 1 :
> s=0
> while 1:
>   s+=input("Enter a num : ")
>   if s>=100:
>     print "The sum is greater than 100 : ",s
>     break

Why do you manually check the condition when the construct is meant to 
take care of it ?

the_sum = 0
while the_sum < 100:
   try:
     sum += int(raw_input("please enter a num: ")
   except ValueError:
     pass


> #Exercise 1 :
> s=0
> for i in range(5):

Was supposed to be 100, not 5.

>   s+=input("Enter num #%d > "%(i+1))

idem as above. Using input() is usually a bad idea:

 >>> import sys
 >>> s = input("please hack my machine: ")
please hack my machine: sys.path.insert(0, '/path/to/malicious/stuff')
 >>> sys.path[0]
'/path/to/malicious/stuff'
 >>>




More information about the Python-list mailing list