Hoewel dit antwoord met try en catch zeker zal werken, lijkt dit me in dit geval niet de beste oplossing. Er bestaat in python een isdigit functie [1], die je kan gebruiken als test of een string enkel uit getallen bestaat. Aan de hand daarvan kan je kijken of je input valid is of niet. Voor complexere inputvalidatie is er altijd nog de regular expression, maar dat is een beetje moeilijker [2].
Just my 2 cents Ward [1] http://www.tutorialspoint.com/python/string_isdigit.htm [2] https://docs.python.org/2/library/re.html nog een beetje code: x=raw_input("geef een getal in: ") if x.isdigit(): wortel=math.sqrt(int(x)) print wortel else: print "invalid input" On Tue, Mar 17, 2015 at 01:10:53AM +0100, Dorian Hoxha wrote: > This link is better: > https://docs.python.org/2/tutorial/errors.html > > > On Tue, Mar 17, 2015 at 1:09 AM, Dorian Hoxha <dorian.ho...@gmail.com> > wrote: > > > Hi Marc. Try this: > > > > while True: > > try: > > x = int(raw_input("give a number:") > > break > > except (ValueError, KeyError): > > print "x must be a number, while you entered %s" % x > > math.sqrt root = (x) > > print x > > > > See docs to learn more: https://docs.python.org/2/library/exceptions.html > > (python2.7) > > > > > > > > 2015-03-17 0:32 GMT+01:00 Marc Stevens <ma...@ziggo.nl>: > > > >> Hoe onderschep ik een error melding als bij de invoer van een getal als > >> een letter word ingegeven? > >> Bijv: > >> > >> > >> import math > >> > >> x=int(raw_input(“geef een getal in”)) > >> wortel=math.sqrt(x) > >> print x > >> > >> _______________________________________________ > >> Python-nl mailing list > >> Python-nl@python.org > >> https://mail.python.org/mailman/listinfo/python-nl > >> > >> > > > _______________________________________________ > Python-nl mailing list > Python-nl@python.org > https://mail.python.org/mailman/listinfo/python-nl _______________________________________________ Python-nl mailing list Python-nl@python.org https://mail.python.org/mailman/listinfo/python-nl