本文介绍了Python:如何在输入错误之前重复上一个输入提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个for语句,提示用户输入5.数字.像这样:
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:
像这样
"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?
推荐答案
使用continue
,您可能会继续前进到下一个输入数字.尝试这样的事情:
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
这篇关于Python:如何在输入错误之前重复上一个输入提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!