Python Forum
Dealing with errors in input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dealing with errors in input
#8
(Apr-09-2019, 07:58 PM)ichabod801 Wrote: You are converting on line 4, you want that in the try block. An error on either one will trigger the except part, so you just need one of those. You also need a break statement.

try:
    val1, val2 = [int(x) for x in input("Please enter two numbers: ").replace(",", " ").split()]
    break
except ValueError:
    print('Please enter valid numbers.')
If the error happens on the second line, it will skip to the except. If it doesn't happen (valid input), it will break out of the loop.

That worked perfectly! I forgot that the input line actually tries to convert to integer so checks it already! Thanks for your help! much up votes :)
Reply


Messages In This Thread
Dealing with errors in input - by souprqtpie - Apr-09-2019, 11:55 AM
RE: Dealing with errors in input - by ichabod801 - Apr-09-2019, 12:08 PM
RE: Dealing with errors in input - by souprqtpie - Apr-09-2019, 12:37 PM
RE: Dealing with errors in input - by perfringo - Apr-09-2019, 01:23 PM
RE: Dealing with errors in input - by ichabod801 - Apr-09-2019, 01:54 PM
RE: Dealing with errors in input - by souprqtpie - Apr-09-2019, 02:48 PM
RE: Dealing with errors in input - by ichabod801 - Apr-09-2019, 07:58 PM
RE: Dealing with errors in input - by souprqtpie - Apr-10-2019, 11:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Dealing with duplicates to an Excel sheet DistraughtMuffin 6 3,316 Oct-28-2020, 05:16 PM
Last Post: Askic
  Dealing with list coja56 2 2,865 Sep-18-2020, 09:32 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020