1

I'm trying to check to see wether or not an input is an integer but I keep on getting a syntax error. The code is as follows, thanks!

try:
    offset=int(input("How Much Would You Like To Offset By?\n"))
    except ValueError:
        while offset!=int:
            print("Please Enter An Integer!")
            offset=int(input("How Much Would You Like To Offset By?\m"))
            except ValueError
2
  • 5
    Please fix your indentation. Also, if your code looks exactly like that. That is actually your problem. You need to make sure your code is properly aligned. Commented Mar 7, 2016 at 18:20
  • 1
    you probably should use a text editor or IDE which supports python's syntax and formats it automatically so this won't be an issue for you. Commented Mar 7, 2016 at 18:22

2 Answers 2

2

As stated in the comments, you need to indent your code correctly. You could for example use an IDE like Spyder which is free and relatively lightweight. In addition you also have an except at the end of your code that shouldn't be there. However, looking at your code in further detail, there are additional issues. Your while loop is currently not doing what you expect it to do and if you are using Python 2.x you will need to replace input with raw_input

try:
    offset=int(raw_input("How Much Would You Like To Offset By?\n"))
except ValueError:
    while offset!=int:
       print("Please Enter An Integer!")
       offset=int(input("How Much Would You Like To Offset By?\m"))

I suspect you want to do something like this, where you keep asking the user for an input until a valid integer has been entered:

offset = None

while not(offset):
    try:
        offset=int(input("How Much Would You Like To Offset By?\n"))
    except ValueError:
            print("Your input was not a valid number, please try again")
            offset = None
Sign up to request clarification or add additional context in comments.

2 Comments

Note that if the OP is using Python 3, then input() would be correct. My initial assumption was that he is, due to his use of the Python 3-style print() function rather than the Python 2-style print statement.
Thanks, I have updated my post to make this clear. Unfortunately we are still stuck on Python 2.7 at my work.
0

You're almost there, just not quite. As noted in the comments, one issue you have in your original code is the indentation for the first except.

Once you fix this, you've then got a second issue:

>>> def get_offset():
...     try:
...         offset=int(input("How Much Would You Like To Offset By?\n"))
...
...     except ValueError:
...         while offset!=int:
...             print("Please Enter An Integer!")
...             offset=int(input("How Much Would You Like To Offset By?\m"))
...             except ValueError
  File "<stdin>", line 9
    except ValueError
         ^
SyntaxError: invalid syntax
>>>

You're getting this issue from the second except ValueError, since there's no try associated with it. If you fixed this, you'd then get an error because the following line references the value of offset before it's actually assigned:

while offset!=int:

If the initial attempt to convert the value entered in the input() statement fails, then no value is actually assigned to offset, which is what causes this error.

I would approach it as follows:

>>> def get_offset():
...     while True:
...         try:
...             offset = int(input("How Much Would You Like To Offset By?\n"))
...             break  # Exit the loop after a valid integer has been entered.
...
...         except ValueError:
...             print("Please enter an integer!")
...
...     return offset
...
>>> x = get_offset()
How Much Would You Like To Offset By?
5
>>> print(x)
5
>>> x = get_offset()
How Much Would You Like To Offset By?
spam
Please enter an integer!
How Much Would You Like To Offset By?
eggs
Please enter an integer!
How Much Would You Like To Offset By?
27
>>> print(x)
27
>>>

This allows you to keep querying for the offset value until a valid integer is entered.

1 Comment

while True: with a break makes my eyelid twitch.

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.