0

I have a function to ask the user to input an integer and I am trying to put in a try function that will replace ValueError with "Input must be an integer." but I keep getting a syntax error for my except! I am using Python 3.5.2

def get_int ():
    s = int(input("Give me an integer: "))
    return(s)

while s is float:
    try:
        s = int(input("Give me an integer: "))
        except(ValueError) as "Input must be an integer."
        print("Input must be an integer.")

except ValueError as:
     ^
SyntaxError: invalid syntax
1
  • I'd suggest changing the except line to except ValueError: without the "as string" part Commented Aug 31, 2016 at 14:29

1 Answer 1

1

except should be at the same indentation level as try, needs a colon before the body, and the as clause is used to hold the error in a variable; something like this:

    try:
        s = int(input("Give me an integer: "))
    except ValueError:
        print("Input must be an integer.")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.