0

I am writing a program in python that will calculate a students GPA. Within the program, I have an input statement which asks the user to enter their student id. Although, the student id entered must be 5 digits in length, or else the program will loop and ask the user to enter the proper number of digits for their id number again. Where I am confused is to how you can check if the input for the integer is within a certain length or number of spaces, in my case its a 5 digit student id

here is my code within the program, the while loop I have below is incorrect because it is only checking if the id number is greater than the integer 5, but not 5 digits. Any type of clarification would be appreciated, thanks

Validates Id Range

id_number = int(input(" Student ID: "))

while id_number < 5:

    id_number = int(input(" try again with Student ID: "))
red
  • 17
  • 2
  • 8
  • keep it as a string for a bit longer and do `len(id_number)` – Paul Rooney Apr 19 '16 at 01:07
  • Thank you Rooney, you answered my question, here is my code, runs like it should idnumber = str(input(" Student ID: ")) while len(idnumber) < 5: idnumber = str(input(" try again with Student ID: ")) – red Apr 19 '16 at 01:14
  • The calls to `str` aren't necessary! `input` already returns a string (I'm assuming you're on python 3, if you were on py 2 use `raw_input`). You should remember to convert it back to int though if you wanted to use it as an integer. – Paul Rooney Apr 19 '16 at 01:18
  • thanks for the insight, and yea im using python 3.51, here is it again without the calls to str, id_number = input(" Student ID: ") while len(id_number) < 5: id_number = input(" Please Enter A Correct Student Id: ") – red Apr 19 '16 at 01:22

0 Answers0