Apr-14-2020, 04:20 AM
Current code and output
# Creat main func. to process input int. def main(): # Create list that holds multiple processed inputs to output together. output = [] # Start try/except block to deal with error codes. try: # Create user input with direction for int. user_input = input('Enter a sequence of digits with nothing separating ' 'them (no input terminates): ') # Create while loop with with sentinel to iterate user input. while user_input != '': # Create variable that contains message with input and calls # stringTotal passing user input. output_accumulator = ('The sum of input string', user_input, 'is:', stringTotal(user_input)) # Add output_accumulator to output list. output.append(output_accumulator) # Repeat user input to complete loop user_input = input('Enter a sequence of digits with nothing ' 'separating them (no input terminates): ') if user_input == '': # Create if statement with no input to raise EOFError # raise EOFError. except ValueError: # Create exception clause for ValueError # Create message for ValueError print('ERROR enter only numbers with no spaces.') except EOFError: # Create exception clause for EOFError # Create header + what program does. print ('\n\n\nWelcome to the Sum of the Digits Program \n\n' 'This program will sum the digits of an input string.\n') # Call output and iterate its list. for x in output: print(x) # Create end message. print('\n\n*** Program has completed successfully***') def stringTotal(user_input): # Create strinTotal() recieving user_input. total = 0 # Create accunulator variable. for num in user_input: # Create for loop to iterate user_input. # Create variable that holds user_input string turned into int. numbers = int(num) total += numbers # Add int. in total variable. return total # Return total. main() # Call main
Output:Enter a sequence of digits with nothing separating them (no input terminates): 11
Enter a sequence of digits with nothing separating them (no input terminates): 22
Enter a sequence of digits with nothing separating them (no input terminates): 33
Enter a sequence of digits with nothing separating them (no input terminates): 44
Enter a sequence of digits with nothing separating them (no input terminates): 55
Enter a sequence of digits with nothing separating them (no input terminates):
Welcome to the Sum of the Digits Program
This program will sum the digits of an input string.
('The sum of input string', '11', 'is:', 2)
('The sum of input string', '22', 'is:', 4)
('The sum of input string', '33', 'is:', 6)
('The sum of input string', '44', 'is:', 8)
('The sum of input string', '55', 'is:', 10)
*** Program has completed successfully***