Python Forum
Dealing with errors in input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dealing with errors in input
#1
Hello

I am currently trying to create a small program which takes two numbers as given by the user and tells you if one can be divided by the other.
It works perfectly fine, but as a side subject I wanted to work out how to loop my program back to ask them to re-enter the digits if they type
something invalid such as letters.

Here is my program thus far:

# input asking the user for 2 numbers and assigning these numbers to variables as integers.
num, check = [int(x) for x in input("Please enter two numbers: ").split()]

# my function which determines whether one is divisible by the other
def num_divide_by_check(x, y):
	if x%y == 0:
		print(str(x) + " is divisible by " + str(y))  
	else:
		print(str(x) + " is not divisible by " + str(y))

# putting the collected values into my function 
num_divide_by_check(num, check)
input("Press enter to close")
The second problem I face is, if the user uses anything but spaces to split up the two numbers I ask for e.g. "+" or ",". Is there a away to make my program ignore these values, or must I just add into my input asking them to split it by a space, and have any other method again recognised as an error and looped back to be re-typed.

Many Thanks
Soup
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,321 Oct-28-2020, 05:16 PM
Last Post: Askic
  Dealing with list coja56 2 2,868 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