Smeagol2
Smeagol2

Reputation: 313

Python: How to repeat the last input prompt before wrong input?

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?

Upvotes: 2

Views: 1907

Answers (1)

BoppreH
BoppreH

Reputation: 10173

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

Upvotes: 5

Related Questions