-2

my code gives me the correct result, but will not stop looping. Newbie question. Any help is greatly appreciated

#Gets user integer input
while True:

value = int(input(" Please print a positive integer:"))
num = value + 1

# re-prompts user for valid entry
if value <= 0:
continue

#calculates all numbers divisible by user enter
if value > 0:
print('The factors of', value, 'are:')

for i in range(1, num):
factors = value % i
if factors == 0:
print(i)
Zoder
  • 13
  • 5
  • 1
    Welcome to StackOverflow! I see you're a new contributor, so I advise you to check out [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [How to create a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Can you elaborate on what the expected behavior of your script is? When do you believe it should exit the `while` loop? – Brian61354270 Apr 07 '20 at 00:45
  • 3
    Indentation is critical in Python, please fix the code in your post. – Barmar Apr 07 '20 at 00:46
  • 1
    You have no `break` statement, so the loop will never stop. – Barmar Apr 07 '20 at 00:47

1 Answers1

0

If you only want to loop until the user gives a positive number then you only need to put that part of the code in a loop and exit the loop when a positive number is entered.

value = -1
while value < 1 :
    value = int(input(" Please print a positive integer:"))

num = value + 1

#calculates all numbers divisible by user enter
if value > 0:
    print('The factors of', value, 'are:')

for i in range(1, num):
    factors = value % i
    if factors == 0:
        print(i, end = ' ')
bashBedlam
  • 1,402
  • 1
  • 7
  • 11