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!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1510
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!
Image
User avatar
Yoriz
 
Posts: 1182
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: legacy_programmer and 4 guests