0

I'm relatively new to program and I'm creating a credit calculator program for assignment. Now, in order to validate the inputs, I was asked to include a function in the program that will only allow integers for the user to input. If the input is a letter, they should get an error, asking them to try again. Now the problem with my program is that it will move on to the next question after asking the user to try again. How can I make sure the program asks the same question again after the wrong value is entered until the user inputs the right type of input?

passCR = input("Enter your pass credits")
try:
    passCR = int(passCR)
except ValueError:
    print("Not an integer! Try again.")
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • 1
    Does this answer your question? [How can i make the program to rerun itself in python?](https://stackoverflow.com/questions/56717711/how-can-i-make-the-program-to-rerun-itself-in-python) – Corentin Pane Nov 10 '19 at 14:40
  • Possible duplicate of [What is the best (idiomatic) way to check the type of a Python variable?](https://stackoverflow.com/questions/378927/what-is-the-best-idiomatic-way-to-check-the-type-of-a-python-variable) – agtoever Nov 10 '19 at 14:52

1 Answers1

0

This will help:

while True:
    passCR = input("Enter your pass credits")
    if passCR.isdigit():
        passCR = int(passCR)
        break
    else:
        print("Not an integer Value")
Bendang
  • 301
  • 1
  • 8
  • You should use [`isinstance`](https://docs.python.org/3/library/functions.html#isinstance) to check type... See [this SO question and answer](https://stackoverflow.com/questions/1549801/what-are-the-differences-between-type-and-isinstance). – agtoever Nov 10 '19 at 14:56
  • `isdigit()` checks if the given input string is a digit or not. Does this count as `type` checking too? – Bendang Nov 10 '19 at 15:04