8

I'm trying to write a code to edit a list and make it a palindrome.

Everything is working except my input still gives me one error. When I enter a non-int into get_number_2, it crashes.

def get_number():
    num = raw_input("Please enter number between 100,000 and 1,000,0000: ")
    if not num.isdigit():
        print "---------------------------"
        print "Invalid input: numbers only"
        print "---------------------------"
        my_main()
    else:
        return num

def get_number_2(n):
    num = input("Please confirm the number you have entered: ")
    if num != int(n):
        print "--------------------"
        print "Entries do not match"
        print "--------------------"
        my_main()
    else:
        return num

I use the input from get_number_2 for the rest of the code as get_number doesn't work when I check if its between two numbers.

Is there any way i can validate if input is an int in get_number_2 so that I can get rid of get_number?

4 Answers 4

6

Write program that handles exception. If user enters not valid integer, it throws ValueError exception:

try:
    a = int(b)
except ValueError:
    print "Unable to interpret your input as a number"

you must update your question like this:

def get_number_2(n):
    num = input("Please confirm the number you have entered: ")
    try:
        if num != int(n):
            print "--------------------"
            print "Entries do not match"
            print "--------------------"
            my_main()
        else:
            return num
    except ValueError:
        print "Unable to interpret your input as a number"
Sign up to request clarification or add additional context in comments.

Comments

3

You also should use raw_input and int(num):

def get_number_2(n):
    num = raw_input("Please confirm the number you have entered: ")
    if not num.isdigit() or int(num) != n:
        print "--------------------"
        print "Entries do not match"
        print "--------------------"
        my_main()
    else:
        return int(num)

Notes:

  • I assume that the parameter n is an int, or to check this you could change the if to: if not num.isdigit() or not n.isdigit() or int(num) != int(n).
  • By using isdigit we check if it is an integer before really converting it to int.

2 Comments

This still won't work because int(num) will throw an error if num is not an integer.
Not anymore, because I do the check before.
3

You can't do num != int(n) because it will attempt to call int(n) which is invalid if n is not in fact an integer.

The proper way to do this is to use try and except

try:
  n = int(n)
except ValueError:
  print 'Entry is not an integer.'
  #handle this in some way

Edit: Also in Python 2.x please use raw_input() instead of input(). input() gives very odd results if you don't know what it's doing.

3 Comments

Please don't use bare except statements. It should be except ValueError.
Ya but if I could validate if get_number_2 is an int I could discard get_number and eliminate the need to match them?
@S_Sull yes, you can simply do try: n = int(raw_input('Enter number: ')) except: ValueError: print 'Please enter an integer' that grabs the input and turns it into an integer, catching if it is not one.
-1
from operator import attrgetter
num0 = input()
if not attrgetter('isdigit')(num0)():
    print("that's not a number")

4 Comments

Please add some explanation. A code snippet only isn't a real answer.
its obvious that attrgetter('isdigit')(num0)() returns Trus if the given string at the "num0" variable is a digit and False if not so if the given string is a digit then the program will run normally if not it will print tha'ts not a number and if you like you can stop the execution or request the input agin
You shouldn't put that in the comment, but expand your answer. It is not about things being obvious, but about the quality of StackOverflow. Your answer was flagged as low quality post, that is why I commented, to give you a chance to improve your post. Otherwise it will be deleted.
@Firas Think of this from a beginner's perspective. If the answer were obvious, then they would not be asking the question. If it helps to be concrete, focus on only the OP, and try to explain the parts that the OP doen't seem to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.