0

Question: Write a program to accept integer inputs until the user inputs a non‐integer. Then, the program prints the summation of the inputted numbers.

My code:

Total = 0
Count = 0
while True:
    n = input ('Enter integers')

    if (n.isdigit() == True):
        print (n)
    else:
        break
    n = int(n)    
    total = total + n
    count = count + 1
    sum = sum(total)

I could run the integers, but it fails to break when digits are typed... Anyone knows why? Also I am expecting the sum function to work but it couldn't add together the integers I input.

Thanks!

chepner
  • 497,756
  • 71
  • 530
  • 681

3 Answers3

0

You don't need the sum line and it works fine:

total = 0
count = 0
while True:
    n = input('Enter integers')
    if n.isdigit():
        print(n)
    else:
        break
    total += n
    count += 1
print(total)
oldxavier
  • 66
  • 6
  • == True not required. – Kanishk Tanwar Feb 12 '20 at 16:53
  • Good one, simplified it. – oldxavier Feb 12 '20 at 16:55
  • Thanks @Xavier! But I still couldn't get the summation going.. When I input 3,4,5, supposed I wanna receive 12 as the output. Not sure where exactly goes wrong. It could just run the integers I type – Ching Long LEE Feb 12 '20 at 16:55
  • it would be nice to explain to the op why their code is failing in the first place (which probably has to do with overwriting the built-in `sum`) – njzk2 Feb 12 '20 at 16:56
  • @ChingLongLEE you have indentation error, while true and then only input statement is working rest of your code is not accessible by python – Kanishk Tanwar Feb 12 '20 at 16:57
  • @njzk2 I don't think the code ever reached matching the built-in sum - sum(total) would always fail in the original code with TypeError - int object is not iterable. But I could be wrong. OP, if you stop your session, start a new one and run this code, it should definitely work and output 12 for 3-4-5. – oldxavier Feb 12 '20 at 17:05
  • @oldxavier good point, I assumed sum would accept a single value, but it doesn't, and it does throw a TypeError – njzk2 Feb 12 '20 at 17:09
0
total = 0
count = 0
while True:
    print('Enter integers')
    n = input ()
    if (n.isdigit() == True):
        print (n)
        n = int(n)    
        total = total + n
        count = count + 1
    else:
        print('The total is ' + str(total))
        print('You put ' + str(count) + ' numbers')
        break
  • Thanks @Gutemberg But still not sure why the summation still couldn't be achieved LOL again, I am expecting a 7 when I input 3 & 4 – Ching Long LEE Feb 12 '20 at 17:04
0

By delegating the input process to an iterative function you will be able to use the sum() function directly to obtain the total:

def inputIntegers():
    while True:
        result = input("Enter integers: ")
        if not result.isdigit(): break
        yield int(result)

print("total:",sum(inputIntegers()))


Enter integers: 3
Enter integers: 25
Enter integers: 6
Enter integers: 4
Enter integers: 
total: 38
Alain T.
  • 40,517
  • 4
  • 31
  • 51