Python Forum
Adding string numbers, while loop and exit without input.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding string numbers, while loop and exit without input.
#11
Gotcha. Change one line (17):
output_accumulator = 'The sum of input string ['+ user_input+ '] is: '+ str(stringTotal(user_input))
That fixes the format of the output.
J
Reply
#12
You're the man! no wonder you are the minister of silly walks.

So shout out to jefsummers.
I can rest this program in peace now.
# 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: '+
        str(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:
Welcome to the Sum of the Digits Program This program will sum the digits of an input string. The sum of input string [12345] is: 15 The sum of input string [123456789] is: 45 *** Program has completed successfully***
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,421 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Split string into 160-character chunks while adding text to each part iambobbiekings 9 9,607 Jan-27-2021, 08:15 AM
Last Post: iambobbiekings
  Convert list of numbers to string of numbers kam_uk 5 3,004 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Convert multiple decimal numbers in string to floats Katy8 6 3,565 Aug-16-2020, 06:06 PM
Last Post: Katy8
  How to print the docstring(documentation string) of the input function ? Kishore_Bill 1 3,552 Feb-27-2020, 09:22 AM
Last Post: buran
  Question about running comparisons through loop from input value Sunioj 2 2,406 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  In input, I want just numbers and not strings! aquerci 2 3,257 Oct-19-2018, 12:53 PM
Last Post: DeaD_EyE
  input vs string thdnkns 5 3,278 Oct-16-2018, 01:15 AM
Last Post: ichabod801
  Divide a number - Multiple levels - Sum of all numbers equal to input number pythoneer 17 8,814 Apr-20-2018, 04:07 AM
Last Post: pythoneer
  Trying to get the week number from a string of numbers fad3r 2 3,206 Apr-15-2018, 06:52 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020