Re: [Tutor] FW: null inputs
thanks very much i get it now... > From: hugo.yo...@gmail.com > Date: Thu, 15 Mar 2012 16:03:12 +0100 > Subject: Re: [Tutor] FW: null inputs > To: eire1...@gmail.com > CC: kellyadr...@hotmail.com; tutor@python.org > > On Thu, Mar 15, 2012 at 3:56 PM, James Reynolds wrote: > > > > > > You can't prevent users from entering whatever they feel like it, but you > > can prevent your program from processing that input and force them to try > > again. > > > > The typical way this is done is through a while loop: > > > > age = '' > > > > while age != '' > > age=raw_input("what age are you? ") > > > > this will continue to loop until you give it something other than ''. > > > > Of course, you can build on this, you know, something like: > > > > checker = False > > > > while checker: > > age=raw_input("what age are you? ") > > try: > > age = int(age) > > checker = True > > except TypeError: > > print "you didn't enter an integer for an age!" > > print "try again!!! > > > > > > Small correction: the exception you're looking for in this case is a > ValueError (after all, the argument provided has the correct type, but > its value can not be converted to an integer). int will throw > TypeError, but only if you supply an argument that is not a string or > number. > > Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] FW: null inputs
On Thu, Mar 15, 2012 at 3:56 PM, James Reynolds wrote: > > > You can't prevent users from entering whatever they feel like it, but you > can prevent your program from processing that input and force them to try > again. > > The typical way this is done is through a while loop: > > age = '' > > while age != '' > age=raw_input("what age are you? ") > > this will continue to loop until you give it something other than ''. > > Of course, you can build on this, you know, something like: > > checker = False > > while checker: > age=raw_input("what age are you? ") > try: > age = int(age) > checker = True > except TypeError: > print "you didn't enter an integer for an age!" > print "try again!!! > > Small correction: the exception you're looking for in this case is a ValueError (after all, the argument provided has the correct type, but its value can not be converted to an integer). int will throw TypeError, but only if you supply an argument that is not a string or number. Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] FW: null inputs
On Thu, Mar 15, 2012 at 10:32 AM, ADRIAN KELLY wrote: > > > Adrian Kelly > 1 Bramble Close > Baylough > Athlone > County Westmeath > > 0879495663 > > > -- > From: kellyadr...@hotmail.com > To: tutor@python.org > Subject: null inputs > Date: Thu, 15 Mar 2012 14:19:16 + > > Hi guys, > how do i prevent a user from leaving a blank when inputting? > > e.g. > age=int(input("what age are you? ")).. i want to stop the > user pressing enter > > if age==""? > > > > age=raw_input("what age are you? ") > if age=="": > age=int(age) > print "please enter your age" > else: > print "your age is ",age > * > i have tried this but still no luck, if anyone can help i would > apprecitate it > > > > Adrian Kelly > 1 Bramble Close > Baylough > Athlone > County Westmeath > > 0879495663 > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > You can't prevent users from entering whatever they feel like it, but you can prevent your program from processing that input and force them to try again. The typical way this is done is through a while loop: age = '' while age != '' age=raw_input("what age are you? ") this will continue to loop until you give it something other than ''. Of course, you can build on this, you know, something like: checker = False while checker: age=raw_input("what age are you? ") try: age = int(age) checker = True except TypeError: print "you didn't enter an integer for an age!" print "try again!!! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] FW: null inputs
Adrian Kelly 1 Bramble Close Baylough Athlone County Westmeath 0879495663 From: kellyadr...@hotmail.com To: tutor@python.org Subject: null inputs Date: Thu, 15 Mar 2012 14:19:16 + Hi guys, how do i prevent a user from leaving a blank when inputting? e.g. age=int(input("what age are you? ")).. i want to stop the user pressing enter if age==""? age=raw_input("what age are you? ") if age=="": age=int(age) print "please enter your age" else: print "your age is ",age * i have tried this but still no luck, if anyone can help i would apprecitate it Adrian Kelly 1 Bramble Close Baylough Athlone County Westmeath 0879495663 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor