2

I have an for statement which prompts the user to input 5. numbers. Like this:

"Input 1. number:

input 2. number:

..
..
.."

I want to repeat the last prompt the user gets before he makes a wrong input (number too big). But my program skips the wrong one:

like this

"Input 1. number:
5
Accepted

input 2. number:
999
Wrong! Retry
(here I use *continue* for the loop)

input 3.number:

---"

What should I do to re-ask the second question?

Rohit Jain
  • 203,151
  • 43
  • 392
  • 509
user1509923
  • 189
  • 1
  • 2
  • 11
  • 1
    Can you post your current code? – BoppreH Nov 21 '12 at 18:11
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – ivan_pozdeev Jul 01 '19 at 01:04

1 Answers1

5

By using continue you are probably continuing ahead to the next input number. Try something like this:

number_of_inputs = 10
max_input = 99
for i in range(number_of_inputs):
    answer = 0
    while not answer or answer > max_input:
        try:
             answer = int(raw_input('Input {}. number: '.format(i)))
        except ValueError:
             continue
    print 'The user selected', answer, 'for input', i
ivan_pozdeev
  • 31,209
  • 16
  • 94
  • 140
BoppreH
  • 6,177
  • 3
  • 31
  • 63