Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Infinite loop
#1
# [ ] use a "forever" while loop to get user input of integers to add to sum,
# until a non-digit is entered, then break the loop and print sum
sum = 0

the best I was able to think up doesn't work
while True:
    sum = ''
	sum1 = input("add number: ")
	if sum1.isdigit():
	    sum = sum + sum1
	else:
	    break
		print(sum)
Suggestions are welcome.
Reply
#2
untested:
while True:
    sum = 0
    sum1 = input("add number or 'Q' to quit: ")
    if sum1 == 'Q':
        break;
    if sum1.isdigit():
        sum = sum + int(sum1)
    else:
        print('Please enter a digit!')
Reply
#3
Below sum = sum + int(sum1) I added print(sum) but it's printing only the last input, not the sum. Not sure why.
Reply
#4
show new code
Reply
#5
just to mention that sum is a built-in function and should not be used as variable name - as is your code now you overwrite sum and the built-in function is no longer available
Reply
#6
(Jan-19-2018, 12:14 PM)buran Wrote: just to mention that sum is a built-in function and should not be used as variable name - as is your code now you overwrite sum and the built-in function is no longer available

Here are all the built-in functions: https://docs.python.org/3/library/functions.html
Look at them.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(Jan-19-2018, 12:02 AM)Larz60+ Wrote: show new code

while True:
    suma = 0
    sum1 = input("add number or 'Q' to quit: ")
    if sum1 == 'Q':
        break;
    if sum1.isdigit():
        suma = suma + int(sum1)
        
    else:
        print('Please enter a digit!')
New it just repeats input request.
Reply
#8
Each time the code is repeated in the loop, suma gets set to 0. You will need to put suma = 0 outside of while loop.

Sure it prints only input request. There is no print statement in the "if" that would print anything, until next loop iteration.
Reply
#9
suma = 0
while True:
    
    sum1 = input("add number or 'Q' to quit: ")
    if sum1 == 'Q':
        break;
    if sum1.isdigit():
        suma = suma + int(sum1)
        print(suma)
         
    else:
        print('Please enter a digit!')
This code works as I wanted!
Reply
#10
Glad you did it, and thanks for posting solution!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Infinite loop Danado 4 2,369 Aug-16-2021, 05:56 PM
Last Post: deanhystad
  Help with while loop creating an infinite loop. FWendeburg 3 3,040 Jan-30-2019, 08:28 PM
Last Post: FWendeburg
  Infinite loop/ only half working anclark686 5 4,756 Sep-09-2018, 07:31 AM
Last Post: buran
  Why is this giving me an infinite loop? wlsa 4 3,907 Jul-25-2018, 10:11 PM
Last Post: cyberpatje
  Another infinite loop wlsa 7 4,667 Jul-20-2018, 12:04 AM
Last Post: wlsa
  How to stop an infinite loop in spyder if it's currently running? wlsa 3 24,717 Jun-30-2018, 03:27 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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