Re: [Tutor] How to make to exit the loop, while typing Enter
Hi John, The code is looking better, some minor points below: > 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 " While what you have written will work, its not usually a good idea to use a try/except block to control program flow like that. In pseudo code terms what you want is: while more marks read and store marks process marks So the processing should really be outside the loop. The idiomatic way to break out of a loop in Python is to use the 'break' keyword (it has a cousin 'continue' too) while: try: loop code here if loop should end break except E: handle any exceptions either break or continue post loop processing here Hopefully that makes sense, it just makes the shape (ie the layout) of your code follow the logical structure of your design. And it doesn't confuse the error handling in the except block with the normal 'happy path' process. >counter = 0 You already did this up above so its not needed here... >length = len(maxmark) >max = maxmark[counter] >for counter in range(length): >if maxmark[counter] > max: >max = maxmark[counter] Because you only use counter to access the array you can do this more cleanly by using the for syntax directly: for mark in maxmark: if mark > max: max = mark No need for the counter, the length, or the call to range. I suspect you may have some experience in a language like C or Basic or Pascal which doesn't havce a 'foreach' kind of operation, often it can be hard to break out of the idea of using indexing to access items in a container. But the for loop is designed specifically for that purpose, using range() is just a trick to simulate conventional for loops on the rare occasions when we really do want the index.(and the new enumerate() method is reducing those occasions even more). >print "At last the Maximum Marks U got is ", max >sys.exit() Because we used break to exit the loop we no longer need the sys.exit() call here, nor by implication, the import statement at the top. HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to make to exit the loop, while typing Enter
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 <[EMAIL PROTECTED]> 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to make to exit the loop, while typing Enter
> entries for the list , But if I press Enter it should > exit the while loop without giving errors ,I have > problem in making it work , right now if I press enter > to exit , the program terminates showing error Thats because you can't convert an empty string to an int. If you defer the conversion until you add the mark to the list then it should work OK. > m = int(raw_input("Enter the Values for The Array : > ")) > array.append(m) array.append(int(m)) HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to make to exit the loop, while typing Enter
Woops, forgot to reply-all.-- Forwarded message --From: Adam <[EMAIL PROTECTED]>Date: 03-Jan-2006 15:48 Subject: Re: [Tutor] How to make to exit the loop, while typing EnterTo: John Joseph <[EMAIL PROTECTED]> array = []m = 0print "Enter to exit"m = int(raw_input("Enter the Values for The Array : "))array.append(m)print arraywhile m != "":m = int(raw_input("Enter the Values for TheArray : "))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 arrayexcept ValueError: print "Only integer values can be entered into the array, or press the enter key to exit." ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to make to exit the loop, while typing Enter
> I am trying to write a program in which it ask for entries for the list > , But if I press Enter it should exit the while loop without giving > errors Hi John, Do you have ideas why it's getting an error? Can you point at the part of the program that's receiving the "Enter"? As a related question, what result do you expect from: int("") What does Python respond with? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to make to exit the loop, while typing Enter
Hi I am trying to write a program in which it ask for entries for the list , But if I press Enter it should exit the while loop without giving errors ,I have problem in making it work , right now if I press enter to exit , the program terminates showing error I am added my script which I wrote Advice requested Thanks Joseph # # How to exit , when user Press Enter # While Entering Data # # define array 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 ___ Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor