-1

So I'm creating a part of my program that raises a error when the user doesn't input a integer. Below is my code...

try:
    pw_length_ask = int(raw_input("How long would you like your password to be? "))
    final_pw_length = pw_length + int(pw_length_ask.strip())
    print("Pass length set to " + str(final_pw_length))
except ValueError:
    print("You must enter a number.")
    int(raw_input("How long would you like your password to be? "))

It works great until the user is asked to enter a number again...

ValueError

It just throws a ValueError then crashes. So how do I make it so that instead of giving the Value Error it repeats the question until the user gives correct input?

sublinial
  • 5
  • 7
  • 3
    Please post error traceback here instead of linking to a screenshot – yash Dec 27 '17 at 00:16
  • See https://meta.stackoverflow.com/a/285557/14122 re: why we don't welcome images of code (or errors). They're not searchable, they're unfriendly to assistive technologies like screen readers, they make the question less useful to others if/when the links eventually rot, etc. – Charles Duffy Dec 27 '17 at 00:24

1 Answers1

0

You could put it in a while loop, like this:

while True:
    try:
        pw_length_ask = int(raw_input("How long would you like your password to be? "))
        final_pw_length = pw_length + int(pw_length_ask)
        print("Pass length set to " + str(final_pw_length))
        break
    except ValueError:
        print("You must enter a number.")

The error likely came from the input you placed in the except block.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80