检查的输入值蟒蛇

代码示例

1
0

蟒蛇检查,如果输入()给出了错误

try:
    # Note: Python 2.x users should use raw_input, the equivalent of 3.x's input
    age = int(input("Please enter your age: "))
except ValueError:
    print("Sorry, I didn't understand that.")
else:
	if age >= 18: 
    	print("You are able to vote in the United States!")
	else:
    	print("You are not able to vote in the United States.")
0
0

蟒蛇的双重检查,如果想要执行功能

while True:
    data = input("Please enter a loud message (must be all caps): ")
    if not data.isupper():
        print("Sorry, your response was not loud enough.")
        continue
    else:
        #we're happy with the value given.
        #we're ready to exit the loop.
        break

while True:
    data = input("Pick an answer from A to D:")
    if data.lower() not in ('a', 'b', 'c', 'd'):
        print("Not an appropriate choice.")
    else:
        break

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................