[Tutor] How to make to exit the loop, while typing Enter

John Joseph jjk_saji at yahoo.com
Wed Jan 4 08:22:51 CET 2006


Hi All  
    I was able to exit the while loop , using sys
module 
     also I was able to find the maximum no of marks
entered ,  I am adding my code , for comments and
suggestions
                   Thanks for the support 
                               Joseph John 

*********************************************************************

""" This progorm was to learn
     1> How to find which is the maximum mark  in list
     2> How to exit the while loop, while typing enter


"""

import sys
maxmark = []

counter = 0

while 1:
    try:
        mark = int(raw_input("Enter the  Values for 
The Array: "))
        maxmark.append(mark)
        print maxmark
    except ValueError:
        #print "From Here to Where "
        counter = 0
        length = len(maxmark)
        #print length
        max = maxmark[counter]
        #print max
        for counter in  range(length):
                if maxmark[counter] > max:
                        max = maxmark[counter]
                        # print " Ya U are in the if
loop and the maximum no is ", max
                #else:
                        #print "The  ", length

        print "At last the Maximum Marks U got is  ",
max
        sys.exit()
~
~
~



--- Adam <adam.jtm30 at gmail.com> wrote:

> > array = []
> > m = 0
> > print "Enter  to exit"
> > m = int(raw_input("Enter the  Values for  The
> Array :
> > "))
> > array.append(m)
> > print array
> > while m != "":
> >         m = int(raw_input("Enter the  Values for 
> The
> > Array :  "))
> >         array.append(m)
> >         print array
> 
> 
> The problem is that if a value is entered that can't
> be converted to an
> integer type, like "" or a letter then an exception
> is raised. What you
> could do is use a try, except.
> while 1:
>     try:
>         m = int(raw_input("Enter the  Values for 
> The Array: "))
>         array.append(m)
>         print array
>     except ValueError:
>         sys.exit()
> 
> alternatively you could do something like this:
>  m=raw_input("Enter the  Values for  The Array: ")
> if m == "": sys.exit()
> try:
>     int(m)
>     array.append(m)
>     print array
> except ValueError:
>     print "Only integer values can be entered into
> the array, or press the
> enter key to exit."
> 



		
___________________________________________________________ 
To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre. http://uk.security.yahoo.com


More information about the Tutor mailing list