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 for off-topic chat!

Please prefer not to PM members. The point of the forum is so that anyone can benefit. We don't want to help you over PMs/emails/Skype chats that others can't benefit from :)
User avatar
micseydel
 
Posts: 2026
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: 1443
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: Yahoo [Bot] and 5 guests