help_with try and except

This is the place for queries that don't fit in any of the other categories.

help_with try and except

Postby emad3li » Fri Jul 19, 2013 12:39 am

Code: Select all
try:
    value1 = float(raw_input("please type your 1st value : "))
    value2 = float(raw_input("please type your 2nd value : "))
except ValueError:
    print "you can't use text, only numbers are allowed "
else:
    result = value1 + value2
    print result



hi ,I'm new with python
I need to know how to re-prompt the user again for input if the exception happens
thank you in advance
emad3li
 
Posts: 2
Joined: Fri Jul 19, 2013 12:30 am

Re: help_with try and except

Postby micseydel » Fri Jul 19, 2013 6:22 am

Do you know loops?
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 929
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: help_with try and except

Postby Yoriz » Fri Jul 19, 2013 1:27 pm

Loop-da-loop
Code: Select all
while True:
    try:
        value1 = float(raw_input("please type your 1st value : "))
        value2 = float(raw_input("please type your 2nd value : "))
        print value1 + value2
        break
    except ValueError:
        print "you can't use text, only numbers are allowed "
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 565
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: help_with try and except

Postby emad3li » Fri Jul 19, 2013 4:03 pm

@Yoriz thank you for help :D
emad3li
 
Posts: 2
Joined: Fri Jul 19, 2013 12:30 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 5 guests