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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…