2013-10-03 178 views
0

我正在創建需要用戶輸入大於2的整數的代碼,然後才能繼續。我使用Python 3.3。這是我到目前爲止有:Python - 輸入驗證

def is_integer(x): 
    try: 
     int(x) 
     return False 
    except ValueError: 
     print('Please enter an integer above 2') 
     return True 

maximum_number_input = input("Maximum Number: ") 

while is_integer(maximum_number_input): 
    maximum_number_input = input("Maximum Number: ") 

    print('You have successfully entered a valid number') 

什麼我不知道的是如何最好地把在我剛開始學習蟒蛇的條件,即整數必須大於2,但想要得到養成良好的習慣。

+0

int(x)'可以爲整數值和非整數值都成功。即使如此,您的函數應該被定義爲真正被稱爲'is_not_integer'。 – chepner

回答

1
def take_user_in(): 
    try: 
     return int(raw_input("Enter a value greater than 2 -> ")) # Taking user input and converting to string 
    except ValueError as e: # Catching the exception, that possibly, a inconvertible string could be given 
     print "Please enter a number as" + str(e) + " as a number" 
     return None 


if __name__ == '__main__': # Somethign akin to having a main function in Python 

    # Structure like a do-whole loop 
    # func() 
    # while() 
    #  func() 
    var = take_user_in() # Taking user data 
    while not isinstance(var, int) or var < 2: # Making sure that data is an int and more than 2 
     var = take_user_in() # Taking user input again for invalid input 

    print "Thank you" # Success 
5

這應該做的工作:

def valid_user_input(x): 
    try: 
     return int(x) > 2 
    except ValueError: 
     return False 

maximum_number_input = input("Maximum Number: ") 

while valid_user_input(maximum_number_input): 
    maximum_number_input = input("Maximum Number: ") 
    print("You have successfully entered a valid number") 

或者更短:

def valid_user_input(): 
    try: 
     return int(input("Maximum Number: ")) > 2 
    except ValueError: 
     return False 

while valid_user_input(): 
    print('You have successfully entered a valid number') 
+0

如果'int(x)'不會引發錯誤,'x'不一定是整數。 – chepner

+1

@chepner你錯了。 'int('3.0')'導致'ValueError'。 –

+1

確實如果函數總是接受一個字符串。時間做一些編輯undownvote ... – chepner

0
def check_value(some_value): 
    try: 
     y = int(some_value) 
    except ValueError: 
     return False 
    return y > 2 
0

這可以驗證輸入是一個整數,但拒絕值是外觀 like整數(如3.0):

def is_valid(x): 
    return isinstance(x,int) and x > 2 

x = 0 
while not is_valid(x): 
    # In Python 2.x, use raw_input() instead of input() 
    x = int(input("Please enter an integer greater than 2: ")) 
1

我的看法:

from itertools import dropwhile 
from numbers import Integral 
from functools import partial 
from ast import literal_eval 

def value_if_type(obj, of_type=(Integral,)): 
    try: 
     value = literal_eval(obj) 
     if isinstance(value, of_type): 
      return value 
    except ValueError: 
     return None 

inputs = map(partial(value_if_type), iter(lambda: input('Input int > 2'), object())) 

gt2 = next(dropwhile(lambda L: L <= 2, inputs)) 
0

希望這有助於

import str 
def validate(s):  
    return str.isdigit(s) and int(s) > 2 
  • str.isdidig()將消除包含非整數的所有字符串,花車( '')和底片(' - ')(小於2)
  • int(user_input)確認它是一個大於2的整數
  • 如果兩者都爲真,則返回True