### Thread: Working program needs improvement.

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
5
Rep Power
0

#### Working program needs improvement.

Howdy everyone!
I am working on some more Python stuff! I decided I wanted to make a program that would take an integer and add each of its digits. (8659 = 8 + 6 + 5 + 9 = 28) I made a working version but I'm curious how I can improve this build! Any suggestions or ideas from your experience?! Very appreciative for all the help!

Code:
```# Program designed to take a string of digits and add each digit together
# Ie.  1234 = 10

x=1
while True:
print('Enter an integer value')
num1 = input()
numstr = str(num1)
numlistint = [int(i) for i in str(numstr)]
final = sum(numlistint)
print(final)
x += 1```
*Python version 3.3.2 on Windows
2. Code:
```# what was x for?
# use sys.stdin to avoid python2/python3 conflict with "input" function.

import sys
import string

while True:
print('Enter an integer value')
num1 = sys.stdin.readline()
if not num1.strip():                  #white space entry terminates loop
break
final = sum(d for d in num1 if d in set(string.digits))   #removed a variable.  Maybe you needed it later.
print(final)```
(untested!)

#### Comments on this post

• TheRealJR agrees : Cleared some things up, but made some more questions! :)
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
5
Rep Power
0
Originally Posted by b49P23TIvg
Code:
```# what was x for?
# use sys.stdin to avoid python2/python3 conflict with "input" function.

import sys
import string

while True:
print('Enter an integer value')
num1 = sys.stdin.readline()
if not num1.strip():                  #white space entry terminates loop
break
final = sum(d for d in num1 if d in set(string.digits))   #removed a variable.  Maybe you needed it later.
print(final)```
(untested!)
Firstly I put that into IDLE and it returned an error when I went to run it.

Code:
```    final = sum(d for d in num1 if d in set(string.digits))   #removed a variable.  Maybe you needed it later.
TypeError: unsupported operand type(s) for +: 'int' and 'str'```
Secondly, I had the x variable in because I was learning about loops and it was in the model and I though I needed it, but that was what was being looped. Furthermore, could you explain the conflict about input? I'm learning code for the first time on the version of Python I downloaded. I have no other experience so I don't know what this means! Thanks for the help!
4. mmm. As many of my posts say, my untested codes usually fail. We need int(d)
Code:
```# tested
import sys
import string

while True:
print('Enter an integer value')
num1 = sys.stdin.readline()
if not num1.strip():                  #white space entry terminates loop
break
final = sum(int(d) for d in num1 if d in set(string.digits))   #removed a variable.  Maybe you needed it later.
print(final)```
5. The input() function already returns a string, but do give an exit from the loop:
Code:
```# Program designed to take a string of digits and add each digit together
# Ie.  1234 = 10

while True:
num_str = input('Enter an integer value (q to quit) ')
if num_str.lower() == 'q':
break
print(sum(int(i) for i in num_str))```
Using sys.stdin.readline() is just plain clumsy. If you want to use the code for Python2 and Python3 do this:
Code:
```# Program designed to take a string of digits and add each digit together
# Ie.  1234 = 10

# optional to make input() work in Python2
try: input = raw_input
except: pass

while True:
num_str = input('Enter an integer value (q to quit) ')
if num_str.lower() == 'q':
break
print(sum(int(i) for i in num_str))```