2

What i am trying to do do is this.

1.User Inputs a number (for example a SSN or any Identification number) 2.If the user is not 14 digits, return to the input and try again 3.If Input is 14 digits, continue in program. 4.Check if SSN starts with 1 or 2 (in my scenario all male ID start with 1 and all female ID start with 2 5. If 1 print male if 2 print female.

My last code is:

ssn=str(input("SSN: "))
x=len(ssn)
while x != 14:
        print("Wrong digits")
        break
else:
        print("Thank you")
y=str(ssn[0])
if y == 1:
        print("Male")
else:
        print("ok")*

In execution i get:

SSN: 12345 Wrong digits ok

The problem is that this does not break if i input 12 digits. It does say wrong digits but continues to in the execution. Second problem is that it prints ok even though i added 1234 which starts with 1 and it should be male.

Any ideas on this please?

Rob
  • 14,746
  • 28
  • 47
  • 65
  • 1
    `1` is an integer. `y=str(ssn[0])` is a string. A string will _never_ be equal to an integer. You should convert the integer to a string or try to convert the string to an integer. – ForceBru Feb 04 '21 at 18:08
  • 1
    You should have your `input()` _inside_ the `while`, so that the user can enter in another number if it's not 14 digits. Also `y` is a string, and `1` is not – gen_Eric Feb 04 '21 at 18:10
  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Tomerikoo Feb 04 '21 at 18:11
  • Thank you guys. Your responses totally made me realize what i was missing. I know i was doing an obvious mistake but i have to start somewhere :) – portocala123 Feb 04 '21 at 18:30

2 Answers2

0

First str(input(...)) is unnecessary, as input already returns a string (why convert string to a string ?). Second, you update neither ssn nor x. Third, while x != 14 will run only as long as x is unequal 14. But you want it to do some processing when x is equal 14. Fourth, 1 == "1" will always be False. Following code should work:

while True: # endless loop
    ssn = input("SSN : ")
    if not ssn.isdigit(): # if the ssn is not a number, stop
        print("not a number")
        continue # let the user try again
    l = len(ssn)
    if len(ssn) != 14:
        print("Wrong digits")
        continue # let the user try again
    else:
        print("Thank you")
        if ssn[0] == "1":
            print("Male")
        else: # This will print Female even if first ID digit is 3 or 4 or whatever, should use elif ssn[0] == "2"
            print("Female")
        break # stop asking again
TheEagle
  • 5,808
  • 3
  • 11
  • 39
  • wouldnt it be better to change the breaks to continues, so that the loop will start again and prompt the user for another input. (ofcourse an option should be provided to allow the user to escape/exit) – Chris Doyle Feb 04 '21 at 18:16
  • @ChrisDoyle no because it doesn't make sense to ask the user for the SSN multiple times. This code does already continue when the SSN was invalid. – TheEagle Feb 04 '21 at 19:07
0

another shorter and cleaner way to do this is by using regular expressions:

import re
while True:
  SSN = input('Enter number:')
  if re.match('1\d{13}',SSN):  # 1 + 13 digits
    print('Male')
    break
  elif re.match('2\d{13}',SSN):# 2 + 13 digits
    print('Female')
    break
  elif re.match('\d{14}',SSN):  #    14 digits
    print('Thank you')
    break
  print('Try again')