Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14040

Trouble with prompting the user for input until it is correct in a while loop

$
0
0
if num < 0:    input('Only  nonnegative intergervalues allowed, try again: ')elif num == 0:    print('The factorial of 0 = 1')while num > 1:    factor = factor * num    num = num -1else:    num = int(input('Only  nonnegative intergervalues allowed, try again: '))

This is what I have. I'm only trying to use the while loop for the program. It prompts the user twice if the integer is negative and then the loop breaks. I need the loop to keep prompting the user until correct input is given. I'm not sure where the error is. Do I need a nested while loop or is the problem with the else statement?

I've tried another while loop underneath the original loop but unsure if I had it done correctly. Also tried moving some things around?


Viewing all articles
Browse latest Browse all 14040

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>