我是靠谱客的博主 清新月饼,这篇文章主要介绍python多次输入数据_Python:如何在输入错误之前重复最后输入提示?,现在分享给大家,希望可以做个参考。

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?

解决方案

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多次输入数据_Python:如何在输入错误之前重复最后输入提示?的全部内容,更多相关python多次输入数据_Python:如何在输入错误之前重复最后输入提示内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部