0
def first_stage():                                     
    digcode = []                     #stores the GTIN sequence                           
    the_list = [3,1,3,1,3,1,3]                      
    for counter in range(7):                        
        digit = input("digit:")                     
        if len(digit) > 1 or len(digit) == 0 or digit[0] == "-":
            print("please enter one positive integer")
            first_stage()   #restarts program to acquire all the numbers                           
        try:                                        
            digit = int(digit) 
        except:
            print("please enter an integer")
            first_stage()                           
        calculation = digit*(the_list[counter])     
        digcode.append(calculation)                 
    the_sum = sum(digcode)                          
    check_digit = 10-(the_sum%10)   #modular division to calculate the eighth digit                  
    if check_digit == 10:                          
        digcode.append(0)                           
        second_stage(digcode)                      
    else:
        digcode.append(check_digit)                 
        second_stage(digcode)

def second_stage(digcode):
    the_list3 = []                                 
    the_list2 = [3,1,3,1,3,1,3,1]                   
    for counter in range(8):                        
        calculation2 = digcode[counter]*the_list2[counter]
        the_list3.append(calculation2)              
    the_sum2 = sum(the_list3)                       
    validate = the_sum2/10                          
    if validate == int(validate):                   
        print("VALID")                              
    else:
        print("INVALID")                            
    first_stage()

after typing in an incorrect input it checks the validity of the sequence by outputting "VALID" or "INVALID" so it works until this point. But after this the program restarts again for some reason. If the inputs are all integers and they pass the validation checks then the program outputs "VALID" or "INVALID" and it ends there but when a wrong input is entered it restarts itself.

digit:1
digit:2
digit:a
please enter an integer
digit:1
digit:2
digit:3
digit:4
digit:5
digit:6
digit:7
INVALID
digit:

Even after the input is received it continues to restart. The number of times it restarts varies so it might depend on my input. At some point it does stop restarting and it outputs a long error message about TypeError.

zondo
  • 19,901
  • 8
  • 44
  • 83
Sam
  • 1
  • 1
  • Please provide the traceback in your question. – zondo Mar 05 '16 at 14:00
  • Well, you _do_ have several recursive calls in your code. Please see [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658/4014959) for better methods of validating user input that do not use recursion. – PM 2Ring Mar 05 '16 at 14:26

0 Answers0